How do I arrange <ul> items in a navbar horizontally and responsive to screen size using Bootstrap?

On my website, I have successfully arranged the elements in the navbar to be fixed. However, when the screen size is reduced, the elements stack vertically.

1
2
3
4
5
6

I attempted to decrease the size of the navbar, but this caused the elements to align vertically. My goal is to have the li elements aligned horizontally within the navbar with a smaller font size. Below is the code snippet I used for resizing the navbar:

@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>

Answer №1

There are a few issues at play here, primarily with not properly overriding the base styles set by Bootstrap.

  1. The default setting for .navbar .nav is to be displayed as a flex item, with its flex-direction changing to column on smaller screens - I adjusted this to row
  2. Similarly, .navbar .nav ul also defaults to being a flex parent with a column direction, so I updated it to row. To ensure specificity over Bootstrap styles, I included the class selector ul.navbar-nav.
  3. Since ul.navbar-nav is a flex child, you can apply flex properties to it using flex: 1 1 100%.
  4. I added some margin to the li elements for better visibility of changes.
  5. The min-width: 400px declaration seemed unnecessary unless there are specific styling needs below that threshold. I removed it in my example but feel free to re-implement if needed.

@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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the proper way to showcase an image within an <li> tag?

So, here's the issue. I initially set up a standard "Home" Button that links to the Home Page of the website, but I felt that the word "Home" was too plain. As a solution, I attempted to replace the word with an icon, but unfortunately, it's not ...

How can we expand the functionality of bootstrap classes?

As I navigate my first steps with Bootstrap Twitter, I find myself in need of extending components. Specifically, I am looking to customize the navbar background without altering the original css file. My attempt involved creating a new class called .test ...

What's the best way to ensure my side navigation floats on top of my full-width slider?

I am currently working on implementing my side navigation to overlay on top of my full-width slider at the beginning of the webpage. However, when I add the slider, the navigation buttons in my side nav become unclickable. I have tried using the z-index ...

Adjust the dimensions of the HTML button to match that of its background

Typically, buttons are created with specific dimensions like this: <button style="width: 120px; height: 40px;"> Mememe <button> Afterwards, a background is added that matches the size of the button: button { background-size: 100% 100%; } ...

What is the best way to ensure that deck.gl and react-map-gl are displaying properly on my screen?

Check out this code sandbox example that demonstrates my attempt to display a map within a specific component. However, I'm facing an issue where the deck.gl and react-map-gl elements are overflowing outside their parent container and extending to the ...

Determine the total quantity of colored cells within a row of an HTML table and display the count in its own

I need assistance with a table that has 32 columns. From columns 1 to 31, I am able to fill in colors by clicking on the cells. However, I now want to calculate and display the number of cells that are not colored in the last column. This task must be co ...

Feature for jotting down notes, creating a break between lines without any information present

I'm currently working on implementing a note-taking feature for my web application. However, I have encountered two issues: Firstly, I am struggling to identify line breaks within the text. While I can hardcode text with line breaks, when a user clic ...

"Rearrange elements on a webpage using CSS and HTML to position one

Can we utilize CSS/HTML to visually move a full width element that is positioned below another one so that it appears above without altering the markup order? <div id="first">first</div> <div id="second">second</div> #first {…} ...

Issues have been reported with the SVG filter URL functionality in Angular 4

Hey there! I'm currently working on drawing a line using SVG in Angular and incorporating some filters for glow effects. However, I've run into an issue where the filter URL is not functioning properly in the browser - nothing is being displayed ...

tips for implementing word-breaking in css

I encountered a problem on my support ticket page where the message text was overflowing out of the designated grey container. Despite trying to utilize the "word-wrap" CSS class, the issue persisted. Here's the code snippet with placeholders for vari ...

What causes images to spill over in CSS Grid layouts?

For the layout, the first image should take up 3/4 or 2 columns of available space, while the remaining two images would be stacked vertically in the last column. <div class="wrapper1"> <div class="one"> <img width="1200px"height="500" ...

Align text vertically within labels and links styled with bootstrap

When using bootstrap button styled labels and links with a larger height than default, the text tends to be vertically aligned at the upper side of the button. However, when using an actual button element, the text is centered vertically. Is there a way t ...

Displaying image in a Bootstrap Carousel

Can you help me achieve my goal of having the image pop up when a user clicks on it in the carousel? I'm not sure how to do it. Any suggestions? <div id="myCarousel" class="carousel slide" data-interval="false" > ...

Trouble activating Bootstrap 4 checkbox through JavaScript integration

When clicking on a table row, I am able to add the class active with JavaScript. However, when trying to click on a checkbox, an error occurs. Check out the live Codepen link here Below is the custom code snippet: $('.dashboard-table-tbody tr&apo ...

What is the process for implementing hover transitions on link elements?

I am trying to incorporate a transition effect for my links. Essentially, I want the text-decoration (which is currently set to underlink) to gradually appear. Unfortunately, my previous attempt at achieving this has been unsuccessful: #slink{ transit ...

show a pair of input buttons within a form

Seeking assistance with a contact form I created using bootstrap. The issue lies in the placement of the submit and clear buttons, which are currently stacked on top of each other instead of being displayed inline next to each other. The form contents are ...

Issues with the proper functioning of the mr-auto class in Bootstrap 4

I am currently working with a code snippet: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin=" ...

If the HTML DOM is unresponsive on the initial click of an element, consider using JavaScript to troubleshoot the issue

I recently encountered an issue while working on a school webpage. I implemented some JavaScript code to show and hide certain elements upon click, but ran into trouble with external CSS properties not being recognized by the script. function grabClassNam ...

css: labeling printed pages in Chrome

I need to make sure my project works on Chrome since I am using electron, so using any other browser is not an option. After searching for a solution to this issue without success, I attempted the following approach (taken from CSS page x of y for @media ...

Theming for Atom and Electron developer tools

After switching from the netbeans IDE to github's atom, I realized that some necessary features were missing. Unable to find a suitable package, I decided to try customizing it myself, gaining valuable insight into the editor in the process. However, ...