1
I’m using the codes below to create a navbar, but I would like, when viewed on desktop, the items highlighted in green to be centralized, but when viewed on mobile, remain exactly as in gif.
Code CSS
ul.bar-secondary {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #b5121b;
}
ul.bar-secondary li {float: left;}
ul.bar-secondary li a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
text-transform: uppercase;
font-weight: 300;
}
ul.bar-secondary li a:hover {background-color: #970008;}
ul.bar-secondary li.bar-secondary-icon {display: none;}
@media screen and (max-width:680px) {
ul.bar-secondary li:not(:first-child) {display: none;}
ul.bar-secondary li.bar-secondary-icon {
float: right;
padding-bottom: -5px;
display: inline-block;
}
ul.bar-secondary li.bar-secondary-icon a {
padding-bottom: 17px;
}
}
@media screen and (max-width:680px) {
ul.bar-secondary.bar-secondary-responsive {position: relative;}
ul.bar-secondary.bar-secondary-responsive li.bar-secondary-icon {
position: absolute;
right: 0;
top: 0;
}
ul.bar-secondary.bar-secondary-responsive li {
float: none;
display: inline;
}
ul.bar-secondary.bar-secondary-responsive li a {
display: block;
text-align: left;
}
}
Code HTML
<ul class="bar-secondary" id="bar-secondary">
<li><a class="active" href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
<li class="bar-secondary-icon">
<a href="javascript:void(0);" style="font-size:15px;" onclick="menu()">☰</a>
</li>
</ul>
Code Javascript
function menu() {
var x = document.getElementById("bar-secondary");
if (x.className === "bar-secondary") {
x.className += " bar-secondary-responsive";
} else {
x.className = "bar-secondary";
}
}