Is there a more effective method for positioning items in a navigation bar other than using padding-left?

Hey there, I've been working on this navigation bar for quite some time now and finally found a way to center the menu items. However, I want to make sure I'm doing it the right way. Does anyone have suggestions on how to center all the "li" elements (like Home, About...)?

Currently, I have used padding-left: 30em; to achieve the centered effect but I'm wondering if there's a more efficient way to do it automatically. I tried using padding auto but it didn't work as expected.

Thank you in advance for your assistance!

@media screen and (min-width: 780px) {
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  body {
    margin: 0;
    min-height: 100vh;
  }

  ul {
    list-style: none;
  }

  a, u {
    text-decoration: none;
  }

  .header-menu {
    background-color: grey;
    padding: 1em 1em;
    width: 100vw;
    display: inline-flex;
    align-items: center;
  }

  .header-flex, {
    display: inline-flex;
    align-items: center;
  }

  .logo {
    cursor: pointer;
  }

  .main-nav {
    cursor: pointer;
    padding-left: 30em;
  }

  .main-nav li {
    display: inline-block;
    padding: 0em 1.2em;
  }

  .main-nav a {
    color: #34495e;
    font-size: .99em;
  }

  .main-nav a:hover {
    color: #718daa;
  }
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
  <link rel="stylesheet" href="./css/home.css">
</head>

<body>

  <header>
    <div class="header-menu">
      <h1 class="logo">Logo</h1>
      <nav class="header-flex">
        <ul class="main-nav">
          <li><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Portfolio</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
      </nav>
    </div>
  </header>

</body>

</html>

Thanks for helping me solve the issue! Here's my updated code:

@media screen and (min-width: 780px) {
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      margin: 0;
      min-height: 100vh;
    }

    ul {
      list-style: none;
    }

    a, u {
      text-decoration: none;
    }

    .header-menu, .header-flex {
      background-color: grey;
      padding: 1em 1em;
      width: 100vw;
      display: inline-flex;
      align-items: center;
    }

    .logo {
      cursor: pointer;
    }

    .main-nav {
      cursor: pointer;
      margin: 0 auto;
    }

    .main-nav li {
      display: inline-block;
      padding: 0em 1.2em;
    }

    .main-nav a {
      color: #34495e;
      font-size: .99em;
    }

    .main-nav a:hover {
      color: #718daa;
    }
  }

Answer №2

To achieve this specific design, my go-to approach is utilizing css grid to create 3 equal columns even when I am only using two of them. By setting the header to display: grid and adjusting the padding, it aligns perfectly as intended.

@media screen and (min-width: 780px) {
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  body {
    margin: 0;
    min-height: 100vh;
  }

  ul {
    list-style: none;
  }

  a, u {
    text-decoration: none;
  }

  .header-menu {
    background-color: grey;
    padding: 1em 1em;
    width: 100vw;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
  }

  .header-flex {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .logo {
    cursor: pointer;
  }

  .main-nav {
    cursor: pointer;
  }

  .main-nav li {
    display: inline-block;
    padding: 0em 1.2em;
  }

  .main-nav a {
    color: #34495e;
    font-size: .99em;
  }

  .main-nav a:hover {
    color: #718daa;
  }
}
<header>
    <div class="header-menu">
      <h1 class="logo">Logo</h1>
      <nav class="header-flex">
        <ul class="main-nav">
          <li><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Portfolio</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
      </nav>
    </div>
  </header>

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 effectively utilizing the display: inline property in conjunction with ng-repeat

I am struggling to create a timeline with a broken structure on my website. I suspect that the issue lies in using display:inline. When attempting to apply this to my site: https://i.stack.imgur.com/4Ur7k.png the layout gets distorted: https://i.stack.i ...

Adjust font style within an HTML/CSS document

I recently obtained the source code for an online portfolio project, but I'm struggling to figure out how to modify the font style. Can anyone provide guidance on this issue? https://github.com/akashyap2013/PortFolio_Website ...

Having trouble applying styles to a component using forwardRef

I'm relatively new to React and still trying to wrap my head around the component's lifecycle. However, the issue at hand is proving to be quite perplexing. One thing that confuses me is why adding "setState(10);" causes the style of the "Test" ...

I encountered the issue: "The specified resource is not a valid image for /public/logoicon/logoOrange.png, it was received as text/html; charset=utf-8."

I encountered an issue when trying to incorporate a PNG or SVG file into my code. What steps should I take to correct this error and enable the images to display properly? import Head from 'next/head' import Image from 'next/image' impo ...

What is causing Firefox to display an additional line in this section?

After much effort, I've been attempting to eliminate the extra spacing on this page in Firefox: Do you see the menu? If you compare it to Chrome, you'll notice that the menu is positioned too low beneath the background, giving the layout a broke ...

Maximizing the width of an absolute div element inside a container

Looking at this demo, you'll notice that the header remains fixed. However, my issue lies in getting the absolute <div> inside the <th> to occupy 100% of the width of the <th> When I set width: 100% for the <div>, it expands t ...

Is there a way to retrieve all "a" tags with an "href" attribute that contains the term "youtube"?

My goal is to capture all a tags that have the href attribute containing the word youtube. This task requires the use of jquery. ...

Footer overlapping occurs on a single page

My webpage is having an issue where the footer is overlapping. It seems to be fine on all other pages except for this one. I would prefer not to make any changes to the footer since it's working correctly on other pages. Is there a way to adjust the C ...

Creating a seamless and interactive online platform

I am in the process of designing a website that has a sleek and dynamic layout, rather than just a static homepage. Let me explain further: Here is my current setup so you can understand what I am trying to achieve. By dynamic, I mean that when the page ...

Utilizing MaterialUI and CSS to craft this stunning box

Looking to create a new MaterialUI component similar to this one: original box Struggling with implementing it using the card component, but so far it looks like: poorly designed box Here's my custom styling using makeStyles: const useStyles = ma ...

Utilizing Wordpress for Effective Internal Linking

Looking to set up a Table of Content on a custom Wordpress template, I decided to hardcode the headings for internal sections in advance. However, I'm facing an issue where it doesn't scroll to the intended section in Wordpress. Take a look at th ...

Another option for replacing <BR> tags with CSS styling

I am currently utilizing the br tag to insert line breaks in my website's faq section. Although the output is accurate, I'm exploring alternatives like implementing linebreak through CSS to enhance responsiveness. During my research, I came acros ...

Click the link to capture the ID and store it in a variable

Currently working on a project involving HTML and JavaScript. Two HTML pages ("people.html" and "profile.html") are being used along with one JavaScript file ("people.js") that contains an object with a list of names, each assigned a unique ID: var person ...

What is the best way to ensure uniformity in my boxes (addressing issues with whitespace and box-sizing using flexbox)?

Below is the code I have written: body { padding-top: 20px; text-align: center; background-color:black; } h1 { display: inline-block; background: white; } h1 { font-size: 30px } p { background: yellow; } .container { display: flex; ...

Position CSS elements at the bottom of the webpage

I'm having trouble with my footer always being covered by dynamic content on my page. How can I adjust the CSS to make sure the footer stays at the bottom and adjusts to the size of the content? Here's the current CSS code for the footer: div ...

The header, main content, and footer sections expand to occupy the entire page

I am in need of HTML structure that looks like the following: <header></header> <div id='main'></div> <footer></footer> I want all elements to be positioned relatively. The header and footer will have fixed h ...

In JavaScript, generate a new column when the text exceeds the height of a div

Is it possible to create a multicolumn layout in HTML that flows from left to right, rather than top to bottom? I am looking to define the height and width of each text column div, so that when the text overflows the box, a new column is automatically ge ...

The SrollToTop function is ineffective when used with a component in Ionic 6/Angular

Recently, I implemented a fabbutton feature that allows users to scroll to the top of a page with just one click. Initially, I tested this functionality without using it as a component, and everything worked perfectly. However, now I want to turn this fabb ...

Determine the selected radio button

----EDIT---- I am developing a jQuery mobile application and I need to determine which radio button is selected. This is the JavaScript code I'm using: function filter(){ if(document.getElementById('segment1').checked) { aler ...

Hide the parent div element if the nested unordered list does not contain any items

I transformed an HTML template into a typo3 template using automaketemplate. Within the HTML code, there is a structure of divs like this <div class="bigPostItWrap">1 <div class="postit">2 <div class="p ...