Stop hyperlinks from wrapping on the navigation bar in Bootstrap 4

Is there a way to prevent the links in the navbar from breaking lines when zooming in? I want them to remain on the same line until the navbar collapses.

In the example below (click to view full size), there are 6 links. When you zoom in, at a certain point the links start breaking lines before the navbar collapses.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-light fixed-top">
    <div class="container">
        <a class="navbar-brand mx-auto" href="#">
Logo
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse text-right" id="navbarTogglerDemo02">         
            <ul class="navbar-nav ml-auto mt-2 mt-lg-0">
                <li class="nav-item main-nav">
                    <a class="nav-link" href="#">Navbar Link Number 1</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Navbar Link Number 2</a>
                </li>
                <li class="nav-item">
                    <a  class="nav-link" href="#">Navbar Link Number 3</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Navbar Link Number 4</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Navbar Link Number 5</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Navbar Link Number 6</a>
                </li>
           </ul>
        </div>
    </div>
</nav>

Answer №1

Implement the text-nowrap utility class on all the links...

https://www.example.com/go/randomlink

<nav class="navbar navbar-expand-lg navbar-light fixed-top">
    <div class="container">
        <a class="navbar-brand mx-auto" href="#">
            My Custom Logo
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse text-right" id="navbarTogglerDemo02">         
            <ul class="navbar-nav ml-auto mt-2 mt-lg-0">
                <li class="nav-item main-nav">
                    <a class="nav-link text-nowrap" href="#">Link Number One</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link text-nowrap" href="#">Link Number Two</a>
                </li>
                <li class="nav-item">
                    <a  class="nav-link text-nowrap" href="#">Link Number Three</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link text-nowrap" href="#">Link Number Four</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link text-nowrap" href="#">Link Number Five</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link text-nowrap" href="#">Link Number Six</a>
                </li>
           </ul>
        </div>
    </div>
</nav>

Answer №2

If you're arriving from a search engine like Google, I recommend avoiding the use of &nbsp; as suggested in another solution. This can make your content difficult to manage and relies on markup for presentation.

An alternative approach is to follow Bootstrap's example by using a simple class like this:

.text-nowrap {
    white-space: nowrap !important;
}

Since this is a utility class, it's acceptable to use !important.

I hope this information proves useful!

Answer №3

If there are only a few users who zoom in, it may be better to hide overflown links instead of wrapping the menu to 2 lines:

<style>
   @media (min-width: 1201px) {
        .navbar-nav {
            overflow: hidden !important;
            max-height: 50px;
        }
   }
</style>

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

Ensure that the placeholder remains visible as you type in the input field

Is there a way to implement an input text field in React with the placeholder "DD-MM-YYYY," that partially disappears as I start typing? For example, when I type "02-," only "-MM-YYYY" should remain visible as part of the placeholder. ...

Positioning JQuery sliders

I've been working on a jQuery slider for my header, but I'm encountering an issue where the previous image drops down to the next container instead of staying in place and transitioning smoothly. It seems like there might be an error in my HTML/C ...

Troubleshooting Issue: Unable to Collapse Bootstrap Responsive Navbar Button with Custom CSS Modifications

I recently created a responsive HTML page using Bootstrap, then converted it to WordPress and made some custom CSS adjustments. In the original HTML version of the page, the navbar collapse button worked perfectly when the screen was resized. However, afte ...

What could be causing my Time Picker MUI to malfunction in my React project?

I am currently working on a React and Rails project where I am attempting to style my React components. However, I have encountered an issue with the MUI component Time and Date picker. Whenever I try to implement the time picker, I get a strange error reg ...

Unable to generate two tables

I have a dynamic table creation issue. The tables are meant to be created based on the user's input for the "super" value. For example, if the user inputs "2" for super, then it should create 2 tables. However, this is not happening as expected becaus ...

When overflowY is set to auto, the dropdown may be cut off if there are

https://i.sstatic.net/Twcnp.png My col-4 has overflow-y set to auto, but my dropdown gets cut off behind another col when it's open. If I change overflow-y to visible, the dropdown is no longer cut off but the max-height parameter is ignored. const ...

How can you center a div at the top of another div?

I am working with a nested div structure and trying to center the inner content of one div within another. Is it possible to achieve this using CSS? <div id="outer"> bla, bla....... <div id="inner"> <p>Test</p> </div> ...

Overlap and cover additional elements within a DIV

I am looking to create a versatile function that can effortlessly overlay various elements such as selects, textfields, inputs, divs, tables, and more with a partially transparent div matching their exact height and width. I have managed to obtain the pos ...

Navigating a dropdown menu in an Asp.net application: choosing an option

I am currently working on an ASP.net project that incorporates Bootstrap controls. The issue I am facing involves a Bootstrap dropdown menu that dynamically populates its list items from a database. The problem arises when I click on the menu and select a ...

Arranging a pair of buttons from separate forms to be perfectly aligned on the same line

I have a page called edit.ejs for editing camps. This page contains two forms, one for submitting edits and the other for deleting the camp. Each form has a button, and they are currently displayed below each other. How can I align the buttons so that they ...

Update the button color to transform it from a dull grey when disabled to a vibrant shade

I am trying to modify the button color in my script from grey to green when it transitions from disabled to enabled status. Despite using both the disabled and enabled properties in my CSS and incorporating vue.js (which I am not very familiar with), the c ...

Is it possible to create a single button that, upon clicking, fades in one image while simultaneously fading out another?

My goal is to have the blue square fade in on the first button click, then fade out while the red square fades in on the second click. Unfortunately, it seems that my current code is not achieving this effect. I'm open to any suggestions or help on h ...

Achieving vertical alignment of content within a card using Bootstrap

Can someone help me create a card similar to this design? Example Card I'm having trouble aligning the items at the bottom like in this example: My Card Here is my code snippet: <div class="container"> <div class="row row ...

What is the reason for the malfunctioning of this button upon clicking?

Attempting to customize my personal website by making the sidebar width change when the sidebar button is clicked. I'm puzzled as to why it's not working as intended. I prefer to figure it out independently but any helpful tips would be appreciat ...

Discussing the Application of Rules for Tables within Block Elements

Could someone provide an explanation as to why the div with the red border is not expanding? <body style="padding:200px"> <div style="border:1px solid red"> <table> <tr> <i ...

The small screen @media query is malfunctioning and not displaying correctly

After creating an HTML file and CSS file, I have encountered a styling issue. When viewing the output on a larger screen, the text "I LOVE CODING, I WILL BECOME AN EXPERT." should be displayed in blue on a red background. However, on smaller screens, it ne ...

What is the most effective method for embedding a Kotlin program into a website?

I have created a combat simulation tool in Kotlin for an online gaming community. Users can input the combat levels of two players, choose the number of duels to simulate, and then initiate the simulation which will provide win percentages and other stats. ...

Effective method for centering a navigation bar vertically within a section element without relying on absolute positioning

Let's explore a unique way to vertically center content within a div while steering away from fixed values. Interestingly, the typical solutions don't always work as expected in a section environment. The challenge now is: How can we center the ...

Is there a rationale behind using intricate CSS classes such as page-left-column-container-box-element-header for technical purposes?

What is the reasoning behind using this specific CSS and HTML structure for styling web pages? <div class="page"> <div class="page-left-column"> <div class="page-left-column-container-box"> <div class="page-left-column-con ...

Breaking the layout using recursive components in VueJS

I am currently facing an issue where I need to dynamically load components. However, when some of these components are loaded, they appear with faulty or missing CSS styles due to a problematic first div element after the template. Removing this DIV manual ...