How to align navbar elements of different sizes using only CSS

Apologies if this question has already been asked. I've searched for solutions to this problem, but they all involve Bootstrap or similar frameworks, not pure CSS.

The issue I'm facing is with a navbar that contains three elements: https://i.sstatic.net/xJHlQ.jpg

I used flexbox to center everything, which worked well, except the left and right elements have different widths. As a result, the logo is centered between them, causing it to be slightly off to the right relative to the container. How can I make the logo centered relative to the container regardless of width and still keep it responsive?

I've spent hours trying to find a solution, but so far, my only idea was to add an invisible element to create extra margin on the right. Is there a simpler solution that I'm missing?

Here's the part of the code in question:

.nav {
  display: flex;
  border: 2px solid red;
  /* justify-content: space-between; */
  align-items: center;
  
}

.nav__list--primary {
  gap: 1.5rem;
}

.nav__list--secondary {
  gap: 6rem;
  border: 1px solid blue; 
}

.navbar__item--icon {
  margin-top: 0.5rem;
}

.logo {
  margin: 0 auto;
}

header {
  margin-bottom: 6rem;
}
<header>
  <div class="container">
    <nav class="nav">

      <ul class="nav__list nav__list--primary row">
        <li class="navbar__item">
          <a class="text--black" href="#">SHOP</a>
        </li>
        <li class="navbar__item">
          <a class="text--black" href="#">VISIT US</a>
        </li>
        <li class="navbar__item">
          <a class="text--black" href="#">EVERYTHING ELSE</a>
        </li>
      </ul>

      <a href="#" class="navbar__item logo text--black">
        GRIND
      </a>

      <ul class="nav__list nav__list--secondary row">
        <li class="navbar__item--icon">
          <a class="text--black" href="#">
            <i class="bx bx-user-circle"></i>
          </a>
        </li>
        <li class="navbar__item--icon">
          <a class="text--black" href="#">
            <i class="bx bx-shopping-bag"></i>
          </a>
        </li>
      </ul>

    </nav>
  </div>
</header>

Answer №1

To align the content of the nav element left, center, and right respectively, one approach is to utilize grid. Here is the updated code with this modification:

.nav {
  /*display: flex;*/
  border: 2px solid red;
  /* justify-content: space-between; */
  align-items: center;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

.nav> :nth-child(2) {
  display: flex;
  justify-content: center
}

.nav> :nth-child(3) {
  display: flex;
  justify-content: right;
}

.nav__list--primary {
  gap: 1.5rem;
}

.nav__list--secondary {
  gap: 6rem;
  border: 1px solid blue;
}

.navbar__item--icon {
  margin-top: 0.5rem;
}

.logo {
  margin: 0 auto;
}

header {
  margin-bottom: 6rem;
}
<header>
  <div class="container">
    <nav class="nav">

      <ul class="nav__list nav__list--primary row">
        <li class="navbar__item">
          <a class="text--black" href="#">SHOP</a>
        </li>
        <li class="navbar__item">
          <a class="text--black" href="#">VISIT US</a>
        </li>
        <li class="navbar__item">
          <a class="text--black" href="#">EVERYTHING ELSE</a>
        </li>
      </ul>

      <a href="#" class="navbar__item logo text--black">
            GRIND
          </a>
      <div>
        <ul class="nav__list nav__list--secondary row">
          <li class="navbar__item--icon">
            <a class="text--black" href="#">
              <i class="bx bx-user-circle"></i>
            </a>
          </li>
          <li class="navbar__item--icon">
            <a class="text--black" href="#">
              <i class="bx bx-shopping-bag"></i>
            </a>
          </li>
        </ul>
      </div>
    </nav>
  </div>
</header>enter code here

Please note that the third child was enclosed in a div to ensure only the specific content is bordered rather than the entire 1fr column.

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

Utilize CSS to ensure footer fills remaining bottom space

My goal is to have the footer take up the entire remaining bottom space. Currently, I am using the following CSS for the footer: clear: both; float: left; background-color: #1F1102; color: #E4F2FA; min-height: 60px; font-family: Verdana, Geneva, sans-serif ...

Introducing an alternative solution for handling tasks linked to the completion of CSS3 animations

In order to incorporate smooth CSS3 animations into my website, I have implemented the animate.css library created by Dan Eden. One particular scenario involves a loading screen that features a fade-out animation. It is crucial for this animation to comple ...

Inquiring about browserify or webpack on a website using node - a few queries to consider

Seeking assistance with a website project I am currently working on. The project consists of 7 HTML documents, 3 stylesheets, 8 JavaScript files (including jQuery.min.js and some additional jQuery plugins), and various images. I am looking to bundle and mi ...

The HTML website appears to be having trouble scrolling on Android devices, however, it functions properly on both iOS and desktop platforms

Hello everyone, I need help solving an issue with my responsive HTML website. It works perfectly on a desktop browser, but when viewed on a mobile device, it gets stuck and won't scroll. Any suggestions on what might be causing this problem? Thank you ...

The Vanishing Act of Bulma Dropdowns in Vue Router

On regular pages, the dropdown feature functions flawlessly. However, when using Vue-router in a single-page application (SPA), the dropdown menu seems to vanish between the navbar-item and its child navbar-link unless the cursor is moved quickly from one ...

Tips for aligning column components in a vertical position

My task is to align vertically the elements within multiple columns that include a headline, a description, and a button. By default, each element expands based on its content size https://i.stack.imgur.com/RJJP4.png However, I need all elements to be al ...

When you hover over a Wordpress page, the entire text on the page will be underlined

Currently designing my own website using WordPress with the Elementor free plugin and Phlox theme. I've made some progress by completing a few sections which include text, buttons, and images. However, I'm encountering an issue where all the text ...

Customizing arrays in DataTables allows for creating new columns and displaying them in the table according to the specified customizations

Displaying dynamic data in a data table involves taking an array as input and customizing the fields within that array. ...

Caption image on hover

Is it possible to make an image overlap text on a horizontal menu bar when hovering with the mouse? I am designing a horror website and would like a bloody handprint to appear over the links in the menu bar when they are hovered over. I know this can be do ...

The Angular application is not displaying correctly on mobile devices due to a lack

Having an issue with my angular build. I've deployed my website at this link: It appears fine when viewed on a desktop and even in the devtools. However, when I try to access it on my phone, the layout looks terrible! Any suggestions on how to fix ...

Collect data from website submissions via email

Is it possible to receive the results via email when someone submits their details on my site using a script? My server does not allow .php or .asp files, only .css. How can I achieve this? ...

Is it possible to accomplish this using a list or a table in HTML/CSS?

I am currently working on creating a visual representation of a color catalog using hyperlinked images. My goal is to have a layout that displays the colors in a grid format, similar to this: However, the number of images per row may vary depending on the ...

Implementing grid items in CSS Grid that do not stretch the grid row and have a container that is scrollable

Here is my dilemma: I am looking to create a grid with a fixed number of columns (that can be adjusted via javascript) and a variable number of rows, all with equal heights. The rows will be determined by the number of grid items, which are represented a ...

What are some ways to prevent popups from being hidden by CSS?

I encountered a problem with my popup that I created using pure CSS. When I move my mouse to the top or bottom of the window, the popup disappears. However, if my cursor is in the body area, the popup appears as intended. I have not used any JavaScript in ...

"Implementing time delays and transitions with jQuery toggle for stylish class changes and smooth

Here is the code snippet I'm currently working with: $('[data-hook="chat"]').click(function () { $('[data-hook="case"]').toggleClass('col-sm-12 col-sm-9'); $('.chat').delay(500).fadeToggle(&apos ...

Styling two divs with borders

Hello everyone, I could really use some assistance with merging the borders of two <div>s. Here's what I'm trying to achieve compared to where I currently stand: view image here and this is my desired outcome view image here Would anyone b ...

Having trouble loading the linked CSS file in the Jade template

My directory structure is organized as follows: --votingApp app.js node_modules public css mystyle.css views test.jade mixins.jade In the file mixins.jade, I have created some general purpose blocks like 'bo ...

Padding has an impact on the widths of flexbox items

Take a look at this code snippet: http://jsfiddle.net/56qwuz6o/3/ <div style="display:flex"> <div id="a">a</div> <div id="b">b</div> <div id="c">c</div> </div> div div { ...

The entire image is unable to be shown within the div

When adding images to a carousel, I encountered an issue where only the top half of the image is displayed on the page. It seems that the image is not adjusting itself to fit the div container properly, resulting in only a portion of it being visible behin ...

Resize a container to match the height of either its children or the full height of the window

I'm seeking advice on how to properly use height: 100%. Is there a way to scale a div based on the height of the window and its content simultaneously? I want one to override the other if necessary. Here's an example of standard height 100% css ...