Creating a multiline centered navigation bar item using Bootstrap 4

My navigation bar is functioning properly with single-line items, but encounters alignment issues when dealing with multi-line items:

<div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav ml-auto">

                <li class="nav-item">
                    <a class="nav-link" href="#">FAQ</a>
                </li>

                <li class="nav-item">
                    <a class="nav-link profile" href="#">
                        <div class="profile-info">
                            <span class="name">Артём</span>
                        </div>
                    </a>
                </li>
            </ul>
        </div>

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

The issue arises when an item spans multiple lines, causing other items to lose vertical center alignment and align to the top instead:

<div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav ml-auto">

                <li class="nav-item">
                    <a class="nav-link" href="#">FAQ</a>
                </li>

                <li class="nav-item">
                    <a class="nav-link profile" href="#">
                        <div class="profile-info">
                            <span class="name">Артём</span>
                            <span class="balance">31337 ₽</span>
                        </div>
                    </a>
                </li>
            </ul>
        </div>

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

The SASS code responsible for styling the navigation bar:

#site-header {
  background: #2d3840;
  border-bottom: solid 1px #20272d;
  height: 83px;

  .navbar-brand {
    color: white;
  }

  .nav-item {

    &:not(:first-child) {
      margin-left: 25px;
    }

    &:not(:last-child) {
      margin-right: 25px;
    }

    // Styling for general links
    a.nav-link {
      color: white;
      font-size: 16px;
      text-align: center;

      @include transition(0.5s);
      &:hover {
        @include transition(0.3s);
        color: $color-accent;
      }

      &.active {
        padding: 6px 21px 6px 21px;
        background: $color-accent;
        border-radius: 17.5px;
        box-shadow: 0 2px 10px 0 rgba(90, 175, 238, 0.6);

        &:hover {
          color: white;
          box-shadow: none;
        }
      }

      // Styling for user profile link
      &.profile {
        .profile-info {
          display: inline-block;

          .name {
            display: block;
            font-weight: bold;
          }

          .balance {
            display: block;
            font-size: 16px;
            font-weight: bold;
            color: $color-accent;
          }
        }
      }
    }

  }
}

The desired outcome for the navigation bar can be observed in this image:

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

If you know how to maintain center alignment for multi-line items, please share your insights.

Answer №1

Get rid of the profile-info and replace it with these classes for the a tag.

  1. d-flex - to create a flex layout
  2. flex-column - to change the flex-direction property to column
  3. text-right - to align the inline children on the right side

<li class="nav-item">
  <a class="nav-link d-flex flex-column text-right" href="#">
    <span class="name">Артём</span>
    <span class="balance">31337 ₽</span>
  </a>
</li>


To vertically center the other items in the list, use align-self-center for each individual li.

<li class="nav-item  align-self-center">
  <a class="nav-link" href="#">FAQ</a>
</li>


<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/js/bootstrap.js"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav ml-auto">
      <li class="nav-item active  align-self-center">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item  align-self-center">
        <a class="nav-link" href="#">Link</a>
      </li>

      <li class="nav-item align-self-center">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
      <li class="nav-item  align-self-center">
        <a class="nav-link" href="#">FAQ</a>
      </li>

      <li class="nav-item ">
        <a class="nav-link d-flex flex-column text-right" href="#">
          <span class="name">Артём</span>
          <span class="balance">31337 ₽</span>
        </a>
      </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

The image is taking up the entire div instead of just filling the area below the header

I'm currently facing an issue with positioning an image within a container below a header. The problem lies in the fact that the image extends beyond the header's position, filling up the entire container. Below is the code snippet I'm work ...

Items that share identical height and margin values will appear with divergent visual presentations

I am trying to align three horizontal lines so that they are the same height and have an equal spacing between each other. However, due to variations in height and margins, some lines appear larger or smaller than others. How can I ensure they all have th ...

The website is failing to adapt properly to smaller screen sizes

I would share some code here, but it might be easier for you to just check out the URL instead. The issue is that the website was responsive across different screen sizes at first, but after making some changes in the CSS and HTML, it's not working pr ...

Do custom Bootstrap 4 variables fail to cascade downwards?

Discovering a strange behavior in the custom.scss file of Bootstrap, I realized that setting a custom color and removing the !default flag doesn't cascade down as expected. For example, updating $blue to #000 without !default in _custom.scss should id ...

Issues with a responsive website not displaying correctly in the Firefox browser

I have created a carousel that is functioning correctly. However, I am encountering an issue with making it fully responsive. It appears to work properly in webkit browsers, but there are some issues in Firefox. Despite the images resizing properly, the ...

Challenge with Bootstrap4 input-group-addon

It seems that the fa icon in the input-group-addon is not working, as shown in the attached screenshot. Any ideas on how to resolve this issue? <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" /&g ...

Slate Map by Google Navigation

I have a situation here that is similar to the grey maps, except all the buttons are visible. Everything appears normal except for the Map tiles, which are all grey! It's strange because it loads nicely at zoom level 8 and then zooms in to the maximum ...

Tips for repositioning the window when linking to a different section of a website

As a beginner in HTML and CSS, I am currently working on adding a navigation bar to my one-page website. I want the navigation bar to smoothly guide users to different sections of the page when they click on a link. To achieve this, I am using anchors and ...

Protected Bootstrap Environment

Bootstrap is an amazing tool, but it tends to enforce too many opinions. The selectors used in its rules are quite broad, such as input or label. Is there a method to isolate Bootstrap's CSS so that it only impacts elements within a container with a ...

What is the best way to adjust the width of a button to match the size of its

Is there a way to make a button automatically adjust its width to the parent div it is nested in? . Below is the code (html structure) for reference: <div class="drp"> <button class="drp-btn" id="i-drp-btn">m/s</button> <div i ...

Float and tap

Can someone assist me with my code? I have 4 identical divs like this one, and when I hover over a link, all the elements receive the same code. <div class="Person-team"> <div class="profile-pic-d"> <a cl ...

Adding up rows and columns using HTML

I've designed an HTML table to function as a spreadsheet, with the goal of being able to total up rows and display the sum in a cell labeled "Total." The same calculation should be applied to columns as well, with totals displayed in the last column c ...

Designing a webpage compatible across different browsers that features an image positioned relatively over two rows of a table

I am working on creating a unique table layout for a web page that resembles the design linked below: Click here to view the image http://kelostrada.pl/upload/test.png Your assistance in achieving this would be greatly appreciated. Currently, I have imp ...

Guide to adjusting margin size according to specific screen size thresholds?

I've set up a series of reactstrap cards that are dynamically organized into rows. On "md" screen sizes and larger (bootstrap), there will be 3 cards per row, while fewer screens will display 2 cards. Here's the component: <Card outline ...

What is the best way to retrieve the content of a specific class in Selenium WebDriver?

Python Code : from selenium import webdriver from selenium.webdriver.chrome.options import Options options = webdriver.ChromeOptions() options.add_argument(r"--user-data-dir=C:\Users\bji\AppData\Local\Google\Chrome\ ...

I would like to know how to create a dropdown menu that shows the country name, flag, and code based on the

I have successfully generated the telephone input field. One requirement is to display the Country flag along with the country name as soon as the dropdown is selected. Another requirement is that once the country is selected, it should display the countr ...

How to Implement Full Height CSS for Child Elements?

How can I set the child containers "left" and "right" to be 100% height of the parent container, which is already at 100% height? Also, what's the best way to position the container "hexCont" horizontally? CSS: <style type="text/css"> html ...

Attempting to use tabs within a tab does not function correctly

Visit the CodePen link here This is the codepen example. <div class="row"> <div class="col s12"> <ul class="tabs2"> <li class="tab col s3"><a href="#test1">Test 1</a></li> <li class=" ...

Can child elements be centered using their pseudo-elements using CSS alone?

Consider the code snippet below: <div class="example-container"> <p class="example">a</p> <p class="example">bbb</p> <p class="example">cc</p> </div> and the cor ...

Removing gaps around rounded corners in jQuery UI accordions is a common issue that often arises when styling web elements

I am currently using jQuery UI Accordion to create collapsible sections that expand when clicked. In order to enhance the appearance, I have applied a background image to the header of each section like this: Check out my jQuery UI Accordion Demo here Up ...