What is the best way to showcase my Bootstrap navigation menu items below my title in a horizontal layout on mobile devices?

When viewing the website on a desktop, I like how the navigation bar looks. However, when resized to a mobile size, it aligns itself vertically under the heading instead of horizontally, which is what I want.

I attempted using "display: inline" but it did not resolve the issue.

I am familiar with media queries, but I need assistance in ensuring that each navigation item displays next to each other under the heading.

document.body.classList.add('fade');

document.addEventListener("DOMContentLoaded", function(e) {
  document.body.classList.remove('fade');
});
body {
  opacity: 1;
  transition: 0.7s opacity;
}

body.fade {
  opacity: 0;
  transition: none;
}

.header {
  padding-left: 50px;
}

#nav {
  padding-right: 50px;
}

.nav {
  transition: linear 0.4s;
}

.nav:hover {
  color: #5a5f61;
  text-decoration: underline;
}

.mt-5 {
  margin-top: 5% !important;
}

.mt-6 {
  margin-top: 2.5% !important;
}

.mt-7 {
  margin-top: 2.5% !important;
}

.bgcolor {
  background-color: lemonchiffon;
}

@media only screen and (min-device-width: 320px) and (max-device-width: 486px) {
  .mt-5 {
    margin-top: 10% !important;
  }
  .mt-6 {
    margin-top: 2.5% !important;
  }
  .mt-7 {
    margin-top: 2.5% !important;
  }
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.jss"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<!-- Nav Bar -->
<nav class="navbar navbar-expand-sm bg-dark container-fluid" id="nav">
  <h2 class="header text-light"> Main Brand Heading </h2>
  <ul class="navbar-nav ml-auto">
    <li class="nav-item">
      <a class="nav-link nav font-weight-bold text-light" href="#">Portfolio</a>
    </li>
    <li class="nav-item">
      <a class="nav-link nav font-weight-bold text-light" href="about.html">About</a>
    </li>
    <li class="nav-item">
      <a class="nav-link nav font-weight-bold text-light" href="contact.php">Contact</a>
    </li>
  </ul>
</nav>
<!-- END Nav bar -->
<div class="jumbotron jumbotron-fluid mt-5 bg-dark text-light">
  <div class="container">
    <h1 class="display-4">Fluid jumbotron</h1>
    <p class="lead">This is a modified jumbotron that occupies the entire horizontal space of its parent.</p>
  </div>
</div>
<div class="jumbotron jumbotron-fluid mt-6 bg-dark text-light">
  <div class="container">
    <h1 class="display-4">Fluid jumbotron</h1>
    <p class="lead">This is a modified jumbotron that occupies the entire horizontal space of its parent.</p>
  </div>
</div>
<div class="jumbotron jumbotron-fluid mt-7 bg-dark text-light">
  <div class="container">
    <h1 class="display-4">Fluid jumbotron</h1>
    <p class="lead">This is a modified jumbotron that occupies the entire horizontal space of its parent.</p>
  </div>
</div>

Answer №1

To enhance the layout, consider incorporating a media query to adjust the flex-direction property and then fine-tune margins and paddings. Here is an example:

UPDATE: Additional lines of code have been included to maintain the nav elements in flex-direction: row.

body {
    opacity: 1;
    transition: 0.7s opacity;
}

body.fade {
    opacity: 0;
    transition: none;
}

.header {
    padding-left: 50px;
}

#nav {
    padding-right: 50px;
}

.nav {
    transition: linear 0.4s;
}

.nav:hover {
    color: #5a5f61;
    text-decoration: underline;
}

.mt-5 {
    margin-top: 5% !important;
}

.mt-6 {
    margin-top: 2.5% !important;
}

.mt-7 {
    margin-top: 2.5% !important;
}

.bgcolor {
    background-color: lemonchiffon;
}

@media only screen and (min-device-width : 320px) and (max-device-width : 486px) {
    .mt-5 {
        margin-top: 10% !important;
    }

    .mt-6 {
        margin-top: 2.5% !important;
    }

    .mt-7 {
        margin-top: 2.5% !important;
    }
}

@media only screen and (max-width : 786px) {
    #nav {
        flex-direction: column;
        justify-content: center;
        padding-right: 0px;
    }

    .header {
        padding-left: 0px;

    }

    .navbar-nav.ml-auto {
        margin-left: 0 !important;
    }

    ul {
        flex-direction: row !important;
    }
  
    ul li a  {
        padding-right: .5rem !important;
        padding-left: .5rem !important;
    }
}
 <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <!-- Main CSS -->
    <link rel="stylesheet" type="text/css" href="css/styles.css">
    <title>Hello, world!</title>
  </head>
  <body class="bgcolor">  
      <script>
          document.body.classList.add('fade');
        </script>
    <!-- Nav Bar -->
      <nav class="navbar navbar-expand-sm bg-dark container-fluid" id="nav">
          <h2 class="header text-light"> Main Brand Heading </h2>
          <ul class="navbar-nav ml-auto">
              <li class="nav-item">
                <a class="nav-link nav font-weight-bold text-light" href="#">Portfolio</a>
              </li>
              <li class="nav-item">
                <a class="nav-link nav font-weight-bold text-light" href="about.html">About</a>
              </li>
              <li class="nav-item">
                 <a class="nav-link nav font-weight-bold text-light" href="contact.php">Contact</a>
              </li>
            </ul>
      </nav>
      <!-- END Nav bar -->
      <div class="jumbotron jumbotron-fluid mt-5 bg-dark text-light">
          <div class="container">
            <h1 class="display-4">Fluid jumbotron</h1>
            <p class="lead">This is a modified jumbotron that occupies the entire horizontal space of its parent.</p>
          </div>
        </div>
      <div class="jumbotron jumbotron-fluid mt-6 bg-dark text-light">
          <div class="container">
            <h1 class="display-4">Fluid jumbotron</h1>
            <p class="lead">This is a modified jumbotron that occupies the entire horizontal space of its parent.</p>
          </div>
        </div>
        <div class="jumbotron jumbotron-fluid mt-7 bg-dark text-light">
            <div class="container">
              <h1 class="display-4">Fluid jumbotron</h1>
              <p class="lead">This is a modified jumbotron that occupies the entire horizontal space of its parent.</p>
            </div>
          </div>
        <script>
            document.addEventListener("DOMContentLoaded", function(e) {
              document.body.classList.remove('fade');
            });
          </script>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>

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 best way to customize the appearance of only one of two identical tables using HTML and CSS?

I have two tables that look identical: <html> <head> <style> td, th { text-align: left; padding: 28px; } </style> </head> <body> <table> <tr> ...

Unable to change background image using jQuery

Snippet: const imageUrl = 'img/star.png'; $("#venueId-"+nodeId).css('background','url("'+imageUrl+'") no-repeat top right'); This piece of code is the start of a function that triggers upon clicking. The goal is to ...

Utilizing JavaScript in Protractor, explore a list to identify the selected checkboxes and those that remain unchecked

Currently, I am working on developing an end-to-end test using Protractor. My main objective is to extract the names from a list and then determine which checkboxes have been selected and which ones haven't. To check the state of the checkbox, I pla ...

Ways to adjust the distance between logo and slider

I am using this script to showcase some products. You can find the script at: http://tympanus.net/Tutorials/ItemSlider/ When resizing the web browser from maximum size to smaller, I find that the height between the logo and shoes is too large for my likin ...

Basic stacked or segmented bar in HTML

Instead of starting from scratch, I'm in search of insights on how to develop a stacked/segmented bar or find an existing control for this purpose. Here are my requirements: Horizontal bar Standard html Color-coded segments using CSS Segments propor ...

Tips for selecting an <select> option value based on an URL parameter automatically

I have a 2-step form where I am successfully passing the first name in the URL from step 1 to step 2, but I am struggling to do the same for a select field. Here's an example of what I have: In the URL: ?firstname=Bob Form Field: <input type= ...

Angular date control and its corresponding date panel are not properly aligned on the user interface

I am utilizing Angular and Angular Material for date control display. See the code snippet below: <input type="date" (change)="validateDateRange($event,true, index)" class="form-control oot-start-date align-middle" name=& ...

Achieving fixed width and automatic height for images in Next JS: a guide

I am struggling to showcase a list of images with a fixed width while utilizing the original image height set at 100%. A similar inquiry can be found here, yet it remains unanswered. I have gone through the responses shared here and attempted them. The im ...

Establishing the REM value for all CSS properties

My goal is to implement the use of rem units throughout my website. However, relying on the standard rem-to-px conversion rate doesn't feel very intuitive: 10px = 0.625rem 12px = 0.75rem 14px = 0.875rem 16px = 1rem (base) 18px = 1.125rem 20px = 1.25r ...

Struggling with z-index or float issues on Chrome and Safari with CSS styling

Check out my website at I have incorporated the Easy Slider 1.7 plugin from this source and made some custom JavaScript modifications for a banner rotator. The issue I am facing is that on webkit browsers, the navigation links (1,2,3,4,5) on the banner a ...

What is preventing Ellipsis from functioning properly on Internet Explorer 11?

Can anyone help me figure out how to display a single line of text with three dots (...) if it exceeds the width of the container? I've tried using the CSS code below, which works on Mozilla and Chrome, but for some reason, it's not working on I ...

The WebDriver encountered an error while trying to click on an element

When using Selenium WebDriver, I am facing an issue with selecting an item from a drop-down list. The element I want to click on is labeled as "game club". Despite attempting to select different elements, I keep encountering an error stating that none of ...

What causes the modal to appear and then vanish suddenly?

I've created a simple modal code with minimal JS involved. When the button is clicked, the modal appears briefly, then the page refreshes and the modal disappears. I'm not sure what could be causing this issue. Any help would be appreciated! h ...

A JavaScript code snippet that stores the total number of bytes read from a CSV file in a variable

I currently have a CSV file located on a web server that is regularly updated with numeric and string data of varying lengths. I am seeking a solution to calculate the total byte values of each row when reading the file, as the byte count is crucial due ...

Laravel - Unauthorized access to array index

I am trying to change the color of a table cell in Laravel based on its content, but I keep encountering this error: "Illegal string offset 'Disponible' (View: C:\Users\RAYLAN\Documents\CRMSAV\resources\vie ...

What steps do I need to take to develop a feature similar to Tabzilla?

Exploring the innovative navigation bar on Mozilla.org seems intriguing; commonly referred to as tabzilla, it smoothly moves down to reveal the menu at the top of the page. I wonder if there are any existing libraries or ready-made jQuery code snippets tha ...

Formatting text over an image

The challenge I am facing involves having an image and text on it in two separate divs with a background color. The length of the text in one div is long while the text in the second div is short, but I want to ensure that the width of both divs are equal. ...

How to ensure onmouseenter and onmouseleave work properly in Chrome using HTML, JavaScript, and jQuery

<div style="position: relative; left: 50px; top: 30px; width: 300px; height: 150px; background: #222222;" onmouseenter="this.style.background='#aaaaaa'" onmouseleave="this.style.background='#222222';"></div> http://jsfiddle ...

Connecting the dots between the price and the menu item

I am currently working on creating a menu with a list of dishes, and I need to include dots to separate the price from the description. I want it to look something like this: https://i.sstatic.net/h7PO4.png If the description is too long, it should look l ...

The speed of the jQuery mouse scroll script remains constant and cannot be altered

I've been searching online... I attempted to adjust the scrolling settings on my website but nothing seems to be working. Does anyone have a guide or list of mouse scroll jQuery scripts and functions? (I've cleared caches, performed cross-brow ...