Navbar links extend offscreen

Viewed 36

I am trying to code a Navbar using HTML and CSS. When resizing the window, the links extend off the right side of the viewport. They are not moving left, but staying in their current position.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Poppins", sans-serif;
}

.navbar {
  position: fixed;
  height: 80px;
  width: 2000px;
  background-color: #000;
  line-height: 80px;
}

.navbar .container {
  justify-content: space-between;
}

.navbar__logo {
  color: rgb(34, 204, 57);
  font-weight: 600;
  font-size: 1.2rem;
}

.navbar__logo span {
  color: #fff;
}

.navbar__link {
  color: #fff;
  text-decoration: none;
  padding: 0 10px;
}

.navbar__link:hover {
  color: rgb(34, 204, 57);
  transition: all 0.3s ease;
}

.navbar__link .active a {
  color: rgb(34, 204, 57) !important;
}


/* Utilities */

.container {
  display: flex;
  min-width: 1220px;
  width: 1220px;
  margin: auto;
}
<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" />
  <title>Document</title>
  <link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&display=swap" rel="stylesheet" />
  <!-- link rel="stylesheet" href="style.css" / -->
</head>

<body>
  <header>
    <div class="navbar">
      <div class="container">
        <div class="navbar__logo">Some<span>Website</span></div>
        <div class="navbar__links">
          <a href="#" class="navbar__link active">Home</a>
          <a href="#" class="navbar__link">Home</a>
          <a href="#" class="navbar__link">Home</a>
          <a href="#" class="navbar__link">Home</a>
          <a href="#" class="navbar__link">Home</a>
        </div>
      </div>
    </div>
  </header>
</body>

2 Answers

You're hard coding widths which makes your links shunt off the right hand edge of the screen. Change .navbar to

.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 80px;

  background-color: #000;
  line-height: 80px;
}

and .container to

.container {
  display: flex;
}

and it should work.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Poppins", sans-serif;
}

.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 80px;
  background-color: #000;
  line-height: 80px;
}

.navbar .container {
  justify-content: space-between;
}

.navbar__logo {
  color: rgb(34, 204, 57);
  font-weight: 600;
  font-size: 1.2rem;
}

.navbar__logo span {
  color: #fff;
}

.navbar__link {
  color: #fff;
  text-decoration: none;
  padding: 0 10px;
}

.navbar__link:hover {
  color: rgb(34, 204, 57);
  transition: all 0.3s ease;
}

.navbar__link .active a {
  color: rgb(34, 204, 57) !important;
}

.container {
  display: flex;
}
<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" />
  <title>Document</title>
  <link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&display=swap" rel="stylesheet" />
  <!-- link rel="stylesheet" href="style.css" / -->
</head>

<body>
  <header>
    <div class="navbar">
      <div class="container">
        <div class="navbar__logo">Some<span>Website</span></div>
        <div class="navbar__links">
          <a href="#" class="navbar__link active">Home</a>
          <a href="#" class="navbar__link">Home</a>
          <a href="#" class="navbar__link">Home</a>
          <a href="#" class="navbar__link">Home</a>
          <a href="#" class="navbar__link">Home</a>
        </div>
      </div>
    </div>
  </header>
</body>

Related