In my html page the elements are properly fixed in the navbar.
1 2 3 4 5 6 7
when the screen is smaller the elements are in vertical
1
2
3
4
5
6
I have managed to decrease the size of navbar
but the elements are in vertical. I need the li
elements horizontally aligned in navbar
with small font size. The code I used for decreasing the navbar is given below.
@media only screen and (min-width: 400px) and (max-width: 992px) {
.navbar {
padding-top: 0.25rem;
padding-bottom: 0.25rem;
height: 54px
}
.navbar .nav ul {
padding-top: 5px !important;
padding-bottom: 0 !important;
height: 30px;
font-size: smaller;
display: inline;
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<a class="navbar-brand">
<img src="https://via.placeholder.com/40x35" width="40" height="35" class="d-inline-block align-top">
<span class="navbar-brand mb-1 h1">Byte Program</span>
</a>
<div class="nav navbar-nav ">
<ul class="navbar-nav mx-3 mt-3 ">
<li class="nav-item ">
<p class="nav-text ">1 </p>
</li>
<li class="nav-item">
<p class="nav-text ">2</p>
</li>
<li class="nav-item">
<p class="nav-text ">3</p>
</li>
<li class="nav-item">
<p class="nav-text "> 4</p>
</li>
</ul>
</div>
</nav>
You have a few issues going on, mostly not overriding Bootstrap base styles.
.navbar .nav
is a flex item, and on smaller screens, it's flex-direction
is column
- so I set that to row
.navbar .nav ul
is also a flex parent, and the flex direction was set to column
, so I set that to row. And since ul
isn't specific enough to overwrite bootstrap styles, I added the class selector ul.navbar-nav
so it was more specific than bootstrap.ul.navbar-nav
is also a flex child so you can set it's flex
properties (flex: 1 1 100%
).margin
to your li
items so you could see it working.min-width: 400px
unless you have something going on below that for a different style. I removed it in my example, but you can certainly put it back in.@media (max-width: 992px) {
.navbar {
padding-top: 0.25rem;
padding-bottom: 0.25rem;
height: 54px
}
.navbar .nav {
flex-direction: row;
}
.navbar .nav ul.navbar-nav {
padding-top: 5px !important;
padding-bottom: 0 !important;
height: 30px;
font-size: smaller;
flex: 1 1 100%;
flex-direction: row;
justify-content: flex-end;
}
.navbar .nav ul.navbar-nav li {
margin: 0 10px;
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<a class="navbar-brand">
<img src="https://via.placeholder.com/40x35" width="40" height="35" class="d-inline-block align-top">
<span class="navbar-brand mb-1 h1">Byte Program</span>
</a>
<div class="nav navbar-nav ">
<ul class="navbar-nav mx-3 mt-3 ">
<li class="nav-item ">
<p class="nav-text ">1 </p>
</li>
<li class="nav-item">
<p class="nav-text ">2</p>
</li>
<li class="nav-item">
<p class="nav-text ">3</p>
</li>
<li class="nav-item">
<p class="nav-text "> 4</p>
</li>
</ul>
</div>
</nav>
it worked fine.thank you !