Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make my nav take up full width of parent?

I need my navbar to stretch the full width of the page. Setting the navbar’s width to 100% adds a small amount of extra space to the right for some reason.

This is my code:

@import url("https://fonts.googleapis.com/css2?family=Poppins&display=swap");
body {
  margin: 0;
  font-family: "Poppins", sans-serif;
}

.flex {
  display: flex;
  gap: 2rem;
}

.primary-header {
  border: 2px solid blue;
  justify-content: center;
}

.primary-navigation {
  list-style: none;
  padding: 1rem;
  margin: 0;
  background: dodgerblue;
  width: 100%;
  border: 2px solid red;
}

.primary-navigation a {
  text-decoration: none;
  color: #ffffff;
}

.primary-navigation a:hover,
.primary-navigation a:focus {
  color: #afafaf;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="style.css" />
  <title>Home</title>
</head>

<body>
  <header id="primary-header" class="primary-header flex">
    <nav>
      <ul id="primary-navigation" class="primary-navigation flex">
        <li><a href="index.html">Placeholder</a></li>
        <li><a href="about.html">Placeholder</a></li>
        <li><a href="projects.html">Placeholder</a></li>
        <li><a href="quals.html">Placeholder</a></li>
        <li><a href="cv.html">Placeholder</a></li>
        <li><a href="#">Placeholder</a></li>
        <li><a href="#">Placeholder</a></li>
        <li><a href="#">Placeholder</a></li>
      </ul>
    </nav>
  </header>
</body>

</html>

I bet it’s a really simple fix, but I haven’t coded in a while so I’m a bit rusty. Does anyone know how to fix this? Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Few minor correction to your styling:

  1. since nav is the only child in #primary-header, the flex can be remove
  2. primary-navigation is having the flex display, you can use the justify-content: center to centered the menu and remove the width: 100%
@import url("https://fonts.googleapis.com/css2?family=Poppins&display=swap");
body {
  margin: 0;
  font-family: "Poppins", sans-serif;
}

.flex {
  display: flex;
  gap: 2rem;
}

.primary-header {
  border: 2px solid blue;
  justify-content: center;
}

.primary-navigation {
  list-style: none;
  padding: 1rem;
  margin: 0;
  background: dodgerblue;
  border: 2px solid red;
  
  justify-content: center;
}

.primary-navigation a {
  text-decoration: none;
  color: #ffffff;
}

.primary-navigation a:hover,
.primary-navigation a:focus {
  color: #afafaf;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="style.css" />
  <title>Home</title>
</head>

<body>
  <header id="primary-header" class="primary-header">
    <nav>
      <ul id="primary-navigation" class="primary-navigation flex">
        <li><a href="index.html">Placeholder</a></li>
        <li><a href="about.html">Placeholder</a></li>
        <li><a href="projects.html">Placeholder</a></li>
        <li><a href="quals.html">Placeholder</a></li>
        <li><a href="cv.html">Placeholder</a></li>
        <li><a href="#">Placeholder</a></li>
        <li><a href="#">Placeholder</a></li>
        <li><a href="#">Placeholder</a></li>
      </ul>
    </nav>
  </header>
</body>

</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading