I’m trying to get the nav
to position to the left as relative. I’ve been encountering this issue quite a lot and can’t get my head around it.
**HTML. **
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Practice Makes Perfect</title>
<link
rel="stylesheet" href="styles.css">
</head>
<body>
<div id="main">
<div id="nav-border">
<nav class="nav-bar">
<ul>
<li><a href="#">Home</li>
<li><a href="#">Contact</li>
</ul>
</nav>
</div>
</div>
</body>
</html>
**CSS
**
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#main {
width: 100vw;
height: 100vh;
background-color: rgb(49, 49, 49);
}
nav-bar{
color: white;
}
#nav-border {
background-color: aqua;
border-radius: 10px;
padding: 5px;
}
nav-links {
display: inline;
}
li {
display:inline;
position: relative;
right: 0;
}
a {
text-decoration: none;
}
a:link {
color: black;
}
a:visited {
color: black;
}
a:hover {
color: skyblue;
}
I have attempted to create a class to contain the nav-bar to get it to align to the right.
I am also aware of how to do this via flex, absolute positioning however i cant get my mind around why it wont shift as relative.
2
Answers
You can use flexbox or grid layout to move the elements properly. these provide more control and is overall better for manual positioning
position: relative
means relative to itself. Sodoes nothing since it’s right edge is already at the far right of itself.
For moving the element to the far right, using only positioning, you need to move the element relative to the parent. which would require
position: absolute
or similar.Other options to move the menu to the right include flexbox,
text-align
, css-grid and others.