What is the best way to insert text between the logo and menu items using bootstrap?

I've attempted several methods without much success. Here is a snippet of the code:

<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#my-navbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#"><img src="img/logo.svg">Demo</a>
    </div>
    <div class="collapse navbar-collapse" id="my-navbar">
      <ul class="nav navbar-nav navbar-right">

Essentially, I have the logo on the left side and a few menu items on the right. There's some space between the logo and the menu where I'd like to add some text. This text should align with the same horizontal line as the logo and menu. However, I haven't been able to achieve this satisfactorily.

Answer №1

There was a lack of information regarding the positioning of text on smaller screens. It is assumed that the desired placement is at the top of #my-navbar.

To address this issue, I propose creating an additional .navbar-collapse to house the text and modifying the navbar-toggler to target the new .navbar-collapse.

HTML Structure (Bootstrap 4.1)

<nav class="navbar">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">Demo</a>
            <button class="navbar-toggler" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="navbar-toggler-icon"></span>
            </button>
        </div>
        <div class="collapse navbar-collapse">
            <div class="navbar-text">
                Navbar text
            </div>
        </div>
        <div class="collapse navbar-collapse">
            <ul class="navbar-nav">
                <li class="nav-item active"></li>
                <li class="nav-item"></li>
                <li class="nav-item"></li>
                ...
            </ul>
        </div>
    </div>
</nav>

For small screens, the existing structure effectively aligns all elements due to Bootstrap setting flex-basis: 100%; on .navbar-collapse. However, it is necessary to set display:flex; on .navbar-header for proper alignment of branding and toggler:

.navbar-header {
    display: flex;
    flex-flow: row nowrap;
    justify-content: space-between;
    flex-grow: 1;
}

https://i.sstatic.net/S9db6.png

On large screens, since Bootstrap already applies justify-content:space-between; to .container-fluid, the elements are correctly positioned. To center the text and right-align the menu, customizable CSS classes or Bootstrap utility classes like justify-content-center and justify-content-end can be used:

        <div class="collapse navbar-collapse justify-content-center">
            <div class="navbar-text">
                Navbar text
            </div>
        </div>
        <div class="collapse navbar-collapse justify-content-end">
            <ul class="navbar-nav">
                <li class="nav-item active"></li>
                <li class="nav-item"></li>
                <li class="nav-item"></li>
                ...
            </ul>
        </div>

https://i.sstatic.net/2uSUQ.png

fiddle: https://jsfiddle.net/aq9Laaew/175839/

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

Tips for implementing active pagination state that persists even after refreshing the page

Currently using the Admin LTE theme and encountering an issue with its datatable. I would like the pagination tab to remain active even after a page refresh. Can anyone provide assistance with this? Here is the link to the pagination table: The image att ...

What is the best way to include a black hover effect on an image?

I am facing a minor issue as a newbie web developer working on a Bootstrap 4 project. Here is the snippet of my code: <section class="front-banners"> <div class="container"> <div class="row" ...

What is the best way to extract values exclusively from a JSON array using jQuery?

Struggling to extract values from a JSON array, I've scoured Stack Overflow for solutions but haven't found the right one. This is what my code looks like: .done(function (data) { var jdata = JSON.stringify(data['queryBuilder']); ...

Leveraging webpack alongside url-loader for embedding images within React applications

When styling with an inline style and passing in the URL of an image file, I typically do it as shown below: let url = `url(${this.state.logo})` var cardStyle = { backgroundImage: url, backgroundSize: '200px 50px' ...

Issues with rendering images in the browser due to CSS inline-block layout are causing

I have encountered an issue with two divs that are set to 50% width and displayed inline-block. Each div contains an image. I expected both divs to stay on the same line, but sometimes the browser breaks the layout. Here is the HTML code snippet: <div ...

Guide to creating a dynamic illumination using CSS, HTML, and JS

Can you help me create a unique lighting effect for a specific section of my webpage? I'm curious about the techniques needed to achieve a similar effect as seen on another webpage. Is it feasible to create this effect using only CSS and HTML, or are ...

How can I ensure that the background color of my HTML email blends seamlessly with the color of the email client's background?

As I embark on creating an HTML email template, I am venturing into the realm of developing for emails, which I know can be quite temperamental. The challenge I'm facing is in making sure that the background of my HTML email matches that of the email ...

CSS only rendering in Firefox, not displaying in other browsers

After all the hard work I put into my website, I have encountered an issue while conducting browser compatibility checks. There seems to be something strange happening with the CSS of a specific link. I have applied a CSS class and included some PHP code ...

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

What is the best way to conceal components in a GUI immediately following the execution of the JavaFX container class?

One of the Java Projects involves creating a cash register using JavaFX. To view the GUI image, simply click here. The program presented in the GUI will prompt users to input the number of items purchased, the amount paid, and display the change due in a ...

What could be causing my HTML website to load slowly and freeze when I switch to another program?

I am in the process of creating three websites. Two of them are functioning perfectly fine, but I am encountering a speed issue with the third one. Sometimes it loads slowly initially, but if I wait a bit longer, it eventually loads and works well. However ...

creating div elements that are confined within the visible area of the browser

I'm currently attempting to append a specific number of div elements to the body without altering the width or height of the body itself. Essentially, I need the new divs to remain within the viewport. Here is the code I'm working with: divAmou ...

Elements overlapped with varying opacities and responsive to mouse hovering

In this Q/A session, we will explore a JS solution for managing the opacity of overlapping elements consistently during hover. Objective Our goal is to create two transparent and overlapping elements, similar to the red boxes showcased below. These eleme ...

Omit the element from the event listener

Is there a way to prevent the image from triggering a click event in this code snippet? $('#templtable .trhover *:not(#infoimg)').live('click', function(event) { event.stopPropagation(); $(this).toggleClass('selected&a ...

What is the most efficient method for incorporating fonts.googleapis.com into CSS files for use on both HTTP and HTTPS pages?

My website functions correctly when accessed through URLs that start with either http:// or default to the http protocol if not specified. However, an error occurs stating that the main.css file cannot be found when the URL begins with https://. The lates ...

Designing a Bootstrap table using various Angular elements

I'm currently in the process of developing a Bootstrap table that utilizes two Angular components. This is how my code is structured : componentA.html <table class="table table-bordered"> <thead> <tr> <th scope="col">#</ ...

What causes the oninput event to act differently in Angular as opposed to regular JavaScript?

As I delve into learning Angular with TypeScript, I've encountered some inconsistencies compared to JavaScript that are puzzling me. Take for example this function that works flawlessly with pure JavaScript, where it dynamically sets the min and max a ...

Keep the navigation bar in motion as the page is scrolled

I am currently designing a webpage with Bootstrap and have a fixed top navbar along with a side navbar that uses scrollspy for content navigation. However, as I scroll down the page, the side navbar remains static and does not move along. I would like both ...

"Storing a collection of PDF files in an array in TypeScript Angular - A step-by-step

Here we have an HTML code snippet that includes an input file element with Angular: <input type="file" class="btn btn-info" id="archivoPDF" #PDFfile value="Seleccionar PDF(s)" accept="application/pdf" multiple /> And this is the TypeScript code sni ...

Looking for the next jQuery class that is not a sibling to toggle the slide

By default, all elements with the class .setupData are hidden. I want to be able to toggle the immediate (next in DOM) .setupData element when I click on an element with the class .setupTitle. Even though I've tried multiple variations of the follow ...