Numerous navigation bar buttons extend the width of the screen rather than wrapping around

My application generates navbar buttons dynamically, and sometimes there are too many buttons causing them to stretch beyond the screen width instead of wrapping to the next line. This behavior used to work correctly in bootstrap v4.0.0-alpha but not in v4.1.3. Is there a way to make the buttons wrap onto the next line instead of overflowing and triggering a scroll bar?

Sample code snippet:

<!DOCTYPE html>
<html xml:lang="en" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Many navbar buttons</title;

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

</head>
<body>

<div class="container-fluid">

<h1>Many navbar buttons</h1>

<nav class="navbar navbar-expand-md navbar-light">
  <span class="navbar-brand"></span>
  <button class="navbar-toggler" type="button" data-toggle="collapse" 
          data-target="#navbarSelect" aria-controls="navbarSelect" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarSelect">
    <ul class="navbar-nav mr-auto">

      <!-- Button Items -->
      
    </ul>
  </div>
</nav>

</div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"> </script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"> </script>

</body>
</html>

Answer №1

To make the navbar buttons wrap, simply include flex-wrap: wrap in the .navbar-nav CSS class.

.navbar-nav {
  flex-wrap: wrap;
}
<!DOCTYPE html>
<html xml:lang="en" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Many navbar buttons</title>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

</head>
<body>

<div class="container-fluid">

<h1>Many navbar buttons</h1>

<nav class="navbar navbar-expand-md navbar-light">
  <span class="navbar-brand"></span>
  <button class="navbar-toggler" type="button" data-toggle="collapse" 
          data-target="#navbarSelect" aria-controls="navbarSelect" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarSelect">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item">
        <input class="nav-link btn btn-primary" type="submit" name="button1" title="button1" value="Button Text">
      </li>
      <li class="nav-item">
        <input class="nav-link btn btn-primary" type="submit" name="button2" title="button2" value="Button Text">
      </li>
      <li class="nav-item">
       // Add more button items here...
    </ul>
  </div>
</nav>

</div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"> </script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"> </script>

</body>
</html>

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

Update the link's name and href using jQuery

i have a link with this href <div id="site_change"> <a id="mysite" href="https://halloweentorino.com/">halloween torino</a> </div> but I want to change the href and the name of the link within it <div id="site_change"> ...

Challenges encountered when integrating images with Bootstrap's grid system

I have implemented a simple Grid System to create a small "Portfolio" showcasing images (4 columns and roughly 6 rows), however, the display looks like this <div class="container"> <div class="row"> <div class=" ...

The anticipated input was a string type, but a BoundField was encountered in Django

Django version: 2.0 python: 3.7 jquery : 3.3.1 bootstrap: 3.3.7 I have integrated ajax into my Django forms by following a tutorial found here. However, I encountered an unusual error when attempting to create a new book or update an existing one. "Expec ...

Is it possible to conceal specific HTML elements using only CSS?

Recently I encountered some HTML code that caught my attention: <address>telephone no <span>-</span> Some Street 2 <span>-</span> 9999 ZZ Place <span>-</span> Something else</address> This particular p ...

customizable path settings for sprites using css

Currently, I have a situation where I am using many sprites in my CSS file, and all the image locations are hardcoded within the CSS itself. Now, I am considering moving all these images to a CDN. The problem is that it's quite cumbersome to change th ...

Angular Highcharts and NG Grid headers are failing to adhere to the specified frame widths

My app features multiple charts using the highcharts plugin and directive, as well as tables utilizing ng-grid. An issue arises when transitioning from the homepage to a page with a chart or table directly; the elements do not adhere to their designated fr ...

AngularJS application is throwing an error indicating provider $q is not recognized

Could someone please advise on what might be the issue with my code snippet below: var app = angular.module('app', [ 'angular-cache', 'angular-loading-bar', 'ngAnimate', 'ngCookies', &a ...

Locating a button element using Selenium on a website

Searching for a troublesome button on my website has become quite the challenge. Everything else seems to be found easily except for this one elusive element that's causing me frustration. The HTML code is as follows: <table class="d_FG" role="pr ...

Populate User Interface Popover - Placement on Compact Display

I have been grappling with finding a solution to position the material ui popover beneath my anchor consistently, even on smaller screens. Take a look at this sandbox example. The current setup is working alright, but the issue is that the scroll is cont ...

The data within my <ui:define name="content"> element is not displaying on the JSF view

I'm currently working on a project that involves JSF/Facelets and a Style Sheet for styling my JSF view. I'm trying to add some graphical components like "h:inputText" and "h:commandButton" tags to my view XHTML, but for some reason, they are not ...

Guide on how to press the enter key to submit a form instead of using a submit button

I am working on a form that has a button with the submit type, and I want it to be able to submit when the enter key is pressed. However, I'm unsure of how to achieve this functionality with the JavaScript function that I currently have in place for s ...

Using the PHP mail() function to send an email with an attachment

I have developed two scripts for this specific HTML code along with a corresponding PHP script. Below is the HTML markup: <html> <body> <form action="http://senindiaonline.in/admar/non-series-numbers-db.php" method="post" target="_bl ...

Unexpected Results from Div Positioning

Here is the PHP code snippet I am working on: <?php include 'header-adminpanel.php'; ?> <div class="container"> <div class="body-content"> <div class="side-left"><?php include 'adminproduct_sidebar.ph ...

Automatically integrating Nivo Lightbox into your website

I incorporated Nivo Lightbox into my website, seamlessly integrating all images with the plugin using the following code: <div class="portfolio-item"> <div class="portfolio-thumb "> <a class="lightbox" data-lightbox-type="ajax" title="Strat ...

Tips for passing variables through onclick to JavaScript file

My task involves querying a database and implementing a filter based on country selection from a map. After writing the JavaScript and PHP code, everything seems to work fine except for one issue - passing a name with an onclick function to initiate the qu ...

Adding a new entry to a database that involves many-to-many relationships

I recently inquired about executing a fetch query on a database with a many-to-many relationship. As I delve deeper into learning SQL + PHP, I encountered a rather intricate scenario: Within my database, I have the following tables: Songs Link ...

What is the best way to ensure a specific section of a website remains visible and fixed at the bottom of the page

I want to set up a simple toolbar with divs and uls containing both anchors and tabs. The position of the toolbar needs to be fixed at the bottom of the page. <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...

Minimize the width to display a scrollbar

Check out this page: where you'll find the same code snippet displayed twice - once through GitHub Gist embedding and the second using Jekyll's internal syntax highlighter Rouge. I'm currently working on styling the second code snippet to m ...

Tips for achieving a uniform padding when a line is split into two sections?

Upon reviewing the code snippet below, I've observed that incorporating left and right padding results in a more evenly balanced color highlighting effect, as demonstrated in point 2 for further clarification: .a { font-size: 4em; font-fam ...

Check to see if a value is present in JavaScript and proceed with submitting the form

I have a situation where I am trying to capture the browser location and post it to another page. The problem I'm facing is that JavaScript takes some time to retrieve the browser location, causing the form to be submitted before the location is obtai ...