'how to move a text to the right side of the page
I'm trying to move the navbar contents to the right side of the page but it's not aligning with the navbar title.
* {
margin: 0;
padding: 0;
font-family: Arial, Helvetica;
}
header li {
list-style: none;
float: right;
display: inline;
padding: 20px;
padding-left:3px;
}
header {
justify-content: space-between;
align-items: center;
box-shadow: 0 1px 8px #ddd;
}
<header>
<h1> Dev101 </h1>
<nav>
<ul>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
Solution 1:[1]
You can add a flex to the header:
header {
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: 0 1px 8px #ddd;
}
Solution 2:[2]
Remove the float. float's are deprecated. In order for justify-content: space-between; to work you need to use display: flex; on the same element.
* {
margin: 0;
padding: 0;
font-family: Arial, Helvetica;
}
header li {
list-style: none;
display: inline;
padding: 20px;
padding-left: 3px;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: 0 1px 8px #ddd;
}
<header>
<h1> Dev101 </h1>
<nav>
<ul>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
Solution 3:[3]
Without using any padding , basically making it responsive , try to avoid using px in your websites as it is not good for responsiveness .
Instead use % , rem , em .
* {
margin: 0;
padding: 0;
font-family: Arial, Helvetica;
}
header li {
list-style: none;
display: inline;
}
header li a{
text-decoration: none;
}
header li a:hover{
text-decoration: underline;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: 0 1px 8px #ddd;
}
header nav{
width:40%;
}
header nav ul{
display: flex;
justify-content: space-evenly;
align-items: center;
}
<header>
<h1> Dev101 </h1>
<nav>
<ul>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
Solution 4:[4]
Just use float to align nav to the right
HTML:
<header>
<h1>Dev420</h1>
<nav>
<ul>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
CSS:
nav{
float: right;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Kameron |
| Solution 3 | Hritik Sharma |
| Solution 4 | Dev-Siri |
