What could be causing my items to not be aligned horizontally in my Flexbox navbar?

I've been experimenting with the navbar design featured in the Youtube video titled "Animated Responsive Navbar with CSS" by Fireship. My goal is to have the items in my navbar aligned horizontally at the center. Here's a snippet of the HTML structure:

<nav class="navbar">
  <ul class="navbar-nav">
    <li class="logo">
      <a href="#" class="nav-link">
        <span class="link-text logo-text">Home</span>

Below is the CSS I'm using:

.navbar {
    position: fixed;
    background-color: var(--bg-primary);
    transition: width 600ms ease;
    overflow: auto;
  }
  
  .navbar-nav {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100%;
  }
  
  .nav-item {
    width: 100%;
  }
  
  .nav-link {
    display: flex;
    align-items: center;
    height: 5rem;
    color: var(--text-primary);
    text-decoration: none;
    filter: grayscale(100%) opacity(0.7);
    transition: var(--transition-speed);
  }

Based on my understanding, setting these properties should horizontally center the items:

  1. Set .navbar-nav to display:flex
  2. Set .navbar-nav to flex-direction:column
  3. Set .navbar-nav to align-items center;

However, the result I get looks like this:

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

If anyone can offer some assistance, I would greatly appreciate it.

Answer №1

In accordance with the information provided in this source (W3 Schools), it is suggested to utilize flex-direction: row. The reason being, flex-direction: column aligns items vertically as opposed to horizontally.

Answer №2

Replace flex-direction: column; with flex-direction: row;

The use of row will align items horizontally in a flex-box, while column aligns items vertically as seen in the original output.

html { /* added these rules to better match OG styles in provided picture */
  background-color: #223; color: #dde; font-family: Arial; padding-left: 2rem;
}
.navbar { position: fixed; transition: width 600ms ease; overflow: auto; }
.navbar-nav {
  list-style: none; padding: 0;
  margin: 0; display: flex;
  flex-direction: row; /* changed "column" to "row" */
  align-items: center; height: 100%;
}
.nav-item { width: 100%; }
.nav-link {
  display: flex; justify-content: center; align-items: center; height: 5rem;
  padding: 1rem; /* added padding so you can see nav items better */
  color: var(--text-primary); text-decoration: none;
  filter: grayscale(100%) opacity(0.7);
}
.logo { position: relative; } /* This and below to add logo next to nav items */
.logo::before { 
  display: block; transform: scale( 1.25 ); position: absolute; 
  top: 3rem; width: 1rem; height: 1rem; 
  background-image: url( "https://i.sstatic.net/ejyfV.png" );
  background-size: 6rem; background-position: -0.06rem -1rem;
  content: "";
}
.logo:nth-of-type( 1 )::before { background-image: none; }
.logo:nth-of-type( 3 )::before { background-position: -0.06rem -2.85rem; }
.logo:nth-of-type( 4 )::before { background-position: -0.06rem -4.7rem; }
.logo:nth-of-type( 5 )::before { background-position: -0.06rem -6.55rem; }
<nav class="navbar">
  <ul class="navbar-nav">
    <li class="logo">
      <a href="#" class="nav-link">
        <span class="link-text logo-text">Home</span>
      </a>
    </li>
    <li class="logo">
      <a href="#" class="nav-link">
        <span class="link-text logo-text">About</span>
      </a>
    </li>
    <li class="logo">
      <a href="#" class="nav-link">
        <span class="link-text logo-text">Work</span>
      </a>
    </li>
    <li class="logo">
      <a href="#" class="nav-link">
        <span class="link-text logo-text">Pricing</span>
      </a>
    </li>
    <li class="logo">
      <a href="#" class="nav-link">
        <span class="link-text logo-text">Contact</span>
      </a>
    </li>
  </ul>
</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

When a table's row is clicked, it will expand to reveal a secondary table

I am facing an issue with rendering a table using v-for in Vue. I want to display a subtable or more information section below a specific row when it is clicked on. My attempts to add a new tr within the existing tr have resulted in errors or the new row ...

Sending Profile Picture and Description to PHP using AJAX and jQuery

I have been struggling to upload user images via AJAX to a PHP database. Despite trying various tutorials and examples, nothing seems to work with my code. The codes function properly without AJAX, but I need the upload process to be seamless for users by ...

Utilizing Cache Features in the XDK Application Framework API

I recently posted a question in the Intel Developer Zone forum for XDK, but unfortunately have not received any answers yet. So I decided to seek help here: My project involves creating a simple HTML5-only periodical website that will be packaged as a sta ...

How to use plain JavaScript to extract the value from a textarea in GWT

Situation: I am currently developing a tampermonkey script to enhance certain GWT pages within a third-party application. Unfortunately, I do not have access to the source code or servers. Challenge: My task is to retrieve the value of a textarea element ...

The hamburger menu for mobile devices is not functioning properly on the website's mobile version, however it operates correctly when the screen is resized

Currently, I am facing an issue with the hamburger menu not responding on my mobile device. Oddly enough, it does work when I resize my browser window to mimic a mobile size. There seems to be a glitch happening, but I'm struggling to pinpoint the exa ...

Adjust the column font size in the table

I am currently building a table using Bootstrap and I have encountered an issue. I would like to set the font size for column 1 (both header and body) to 12px, and for column 2 (both header and body) to 21px. In essence, I need to be able to customize the ...

Blank email messages in outlook.com, microsoft exchange, and hotmail.com

After styling an email and testing it, everything seemed to work fine except in Outlook.com, Microsoft Exchange, and Hotmail. For some reason, Exchange strips all the code used (tables, tr & td tags) no matter what I do. Is there a workaround for this ...

Position fixed causing website header to hide behind content

As I work on creating a website for a school, I aim to have a fixed header similar to what can be seen on Facebook. Despite trying out some fixes mentioned in this Stack Overflow [question][1], I am struggling to make it work effectively in my design. The ...

Utilizing Ajax to Create a Form with Invisible Google Recaptcha

Here is my code: function submitForm(token) { $(document).ready(function() { $("#submit").click(function() { var name = $("#name").val(); var email = $("#email").val(); var password = $("#password").val(); var contact = $ ...

How can you add padding to an HTML page using CSS?

I've been attempting to use padding-bottom:90px; in order to shift Tweets by ‎@StackOverflow (the entire tweets section) downwards on the page. I want to move it further down using the above padding, but it doesn't seem to be working for me. H ...

How can I pass a specific value, rather than the entire array, in a ReactJS dropdown menu?

I am facing a problem where the entire array is being passed as an argument when I call onchange after getting correct values in the dropdown. Instead of only receiving the selected value, e contains the whole array. Here is the code snippet that demonst ...

Angular is throwing error TS2322 stating that the type 'string' cannot be assigned to the type '"canvas" while working with ng-particles

My goal is to incorporate particles.js into the home screen component of my project. I have successfully installed "npm install ng-particles" and "npm install tsparticles." However, even after serving and restarting the application, I am unable to resolve ...

Chrome Automatically Playing WebRTC Audio

My webapp has webrtc functionality, but I've noticed a strange issue. When connections are established between Chrome browsers, the audio is not played, while video is. However, this problem doesn't occur with Mozilla users. So... Chrome user 1 ...

Display the exclamation point symbol after the hash symbol in the URL while navigating in AngularJS

I recently started learning AngularJS and decided to test out routing with a demo. Here are the relevant parts of my code: app.js var app=angular.module("tutorialApp",["ngRoute","tutorialCtrlModule"]); app.config(function($routeProvider){ $routeProvi ...

What is the reason behind H1 tags not requiring a class or ID in CSS?

As I was reviewing my CSS code, I noticed that the h1 tag is defined like this: h1 { .... } Unlike everything else in my code, which either has an id "#" or a "." class preceding it. This made me wonder why header tags don't require this. Could it b ...

Integrating variable interpolation within media queries using Compass/Sass

Is there a method to achieve the desired result in Compass/Sass? $padding: 3em div { padding: $padding; } @media screen and (max-width: 780px) { $padding: 2em; } /* Result: div { padding: 3em; } @media screen and (max-width: 780px) { ...

Styling an HTML table dynamically using JavaScript based on certain conditions

How to dynamically set the background color of an HTML table row using only javascript based on the value of a cell in the table column? Background: - Adjusting python code to generate HTML with a table, already included a 'sort on click' funct ...

Is it possible to target a specific element within one of two classes that share the same name using CSS or XPath in Webdriver.IO?

Currently, I am using Webdriver.io and facing a challenge in selecting an element within the "text-fields-container" class. This particular element happens to be a password field, and both classes share the same name. Can someone guide me on how to go abou ...

What is the best way to place an "Add row" button directly in front of every horizontal row border within a table?

I would like to create an HTML table where I can insert rows by clicking on a centered "insert row" button placed between each pair of rows. This will help in visually indicating where the new row will be added. Is there a CSS solution to achieve this lay ...

I seem to be struggling with hiding/showing a div element. Can you figure

I am in the process of creating a gallery that will display a div with images when a button is clicked. The issue I am facing is that when I have two buttons, only the last images are clickable. It's a bit difficult to explain, but you can see it in ...