skip to Main Content

So when I try making the logo of my website smaller, which is functioning as a home button as well, it gets smaller, but the button itself stays the same making the navbar elements stay in the same place.

.navbar {
  color: white;
  background: linear-gradient(to bottom, #000000c4 90%, #1b1b1b00 100%);
  transition: background-color 0.5s ease-in-out;
}

.navbar-nav .nav-link {
  flex-grow: 1;
}

.logo {
  width: 50%;
  height: auto;
  margin-left: 5px;
  float: left;
}

body {
  background-image: url(../imgs/bg-img.jpg);
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  height: 100vh;
  overflow: auto;
  font-family: "Arial", sans-serif;
  margin: 0;
  padding: 0;
  background-color: #313436;
}

header {
  background-color: #00478f;
  color: white;
  padding: 1em;
  text-align: center;
}

h1 {
  color: white;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Gran Turismo</title>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" />
  <link rel="stylesheet" href="css/styles.css" />
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" />
</head>

<body>
  <!-- Navbar -->
  <nav class="navbar navbar-expand-lg navbar-dark">
    <div class="container-fluid">
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
      <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
        <a class="navbar-brand" href="index.html">
          <img src="imgs/logo.png" class="logo" alt="GTLogo" />
        </a>
        <ul class="navbar-nav me-auto mb-2 mb-lg-0">
          <li class="nav-item">
            <a class="nav-link active" href="#">News</a>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Spiele
              </a>
            <!-- Dropdown für Spiele -->
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <a class="dropdown-item" href="#">Gran Turismo Sport</a>
              <a class="dropdown-item" href="#">Gran Turismo 7</a>
              <a class="dropdown-item" href="#">Gran Turismo 6</a>
              <a class="dropdown-item" href="#">Gran Turismo 5</a>
              <a class="dropdown-item" href="#">Gran Turismo 4</a>
              <a class="dropdown-item" href="#">Gran Turismo 3</a>
              <a class="dropdown-item" href="#">Gran Turismo 2</a>
              <a class="dropdown-item" href="#">Gran Turismo</a>
              <!-- Weitere Spiele hinzufügen -->
            </div>
          </li>
        </ul>
      </div>
    </div>
  </nav>

  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>

</html>

Tried many ways myself, but I’m a beginner so I don’t really know the solution. Thanks in advance

Edit: https://imgur.com/a/BNlXepo This blank spot between the logo and the news button is the problem.

2

Answers


  1. Instead of applying the width to the image, I’d style the link, e.g.

    .navbar-brand {
      float: left;
      width: 30%; /* change to your desired width */
      overflow: hidden;
      margin-left: 5px;
    }
    
    .navbar-brand .logo{
      height: auto;
      width: 100%;
    }
    

    width: 100%; for the logo ensures that the image takes up the entire available space of the parent container .navbar-brand.

    .navbar-brand {
      float: left;
      width: 30%;
      overflow: hidden;
      margin-left: 5px;
    }
    
    .navbar-brand .logo {
      height: auto;
      width: 100%;
    }
    
    .navbar {
      color: white;
      background: linear-gradient(to bottom, #000000c4 90%, #1b1b1b00 100%);
      transition: background-color 0.5s ease-in-out;
    }
    
    .navbar-nav .nav-link {
      flex-grow: 1;
    }
    
    
    /*.logo {
      width: 50%;
      height: auto;
      margin-left: 5px;
      float: left;
    }*/
    
    body {
      background-image: url(../imgs/bg-img.jpg);
      background-size: cover;
      background-position: center;
      background-attachment: fixed;
      height: 100vh;
      overflow: auto;
      font-family: "Arial", sans-serif;
      margin: 0;
      padding: 0;
      background-color: #313436;
    }
    
    header {
      background-color: #00478f;
      color: white;
      padding: 1em;
      text-align: center;
    }
    
    h1 {
      color: white;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1" />
      <title>Gran Turismo</title>
      <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" />
      <link rel="stylesheet" href="css/styles.css" />
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" />
    </head>
    
    <body>
      <!-- Navbar -->
      <nav class="navbar navbar-expand-lg navbar-dark">
        <div class="container-fluid">
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
              <span class="navbar-toggler-icon"></span>
            </button>
          <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
            <a class="navbar-brand" href="index.html">
              <img src="imgs/logo.png" class="logo" alt="GTLogo" />
            </a>
            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
              <li class="nav-item">
                <a class="nav-link active" href="#">News</a>
              </li>
              <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Spiele
                  </a>
                <!-- Dropdown für Spiele -->
                <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                  <a class="dropdown-item" href="#">Gran Turismo Sport</a>
                  <a class="dropdown-item" href="#">Gran Turismo 7</a>
                  <a class="dropdown-item" href="#">Gran Turismo 6</a>
                  <a class="dropdown-item" href="#">Gran Turismo 5</a>
                  <a class="dropdown-item" href="#">Gran Turismo 4</a>
                  <a class="dropdown-item" href="#">Gran Turismo 3</a>
                  <a class="dropdown-item" href="#">Gran Turismo 2</a>
                  <a class="dropdown-item" href="#">Gran Turismo</a>
                  <!-- Weitere Spiele hinzufügen -->
                </div>
              </li>
            </ul>
          </div>
        </div>
      </nav>
    
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
    </body>
    
    </html>
    Login or Signup to reply.
  2. I suspect the issue is with your logo class width: 50%; setting.

    When your image is downloaded it has an Intrinsic size. By setting it to 50% there will be a rendered size half of it’s Intrinsic size but still consuming it’s parent container.

    To resolve your issue just fix the width to something and move on. For example:

    .logo {
      width: 120px;
      height: auto;
      margin-left: 5px;
      float: left;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search