Warm tip: This article is reproduced from serverfault.com, please click

How can I reorder elements horizontally aligned in navbar based on screen size (bootstrap)

发布于 2020-11-28 14:05:07

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>
Questioner
Kevin Thomas
Viewed
0
disinfor 2020-11-28 23:35:12

You have a few issues going on, mostly not overriding Bootstrap base styles.

  1. By default, .navbar .nav is a flex item, and on smaller screens, it's flex-direction is column - so I set that to row
  2. .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.
  3. ul.navbar-nav is also a flex child so you can set it's flex properties (flex: 1 1 100%).
  4. I also added some margin to your li items so you could see it working.
  5. There is no reason for your 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>