Home > OS >  Bootstrap 5 navigation not wide as parent container
Bootstrap 5 navigation not wide as parent container

Time:10-04

I have created a navigation in Bootstrap inside a container, but for some reason the navigation bar is not as wide as the container, but why? I even had to set padding of the navigation bar manually.

Code: index.php

   <!DOCTYPE html>
    <html lang="cs">
        <head>
            <title>xGhost.cz | Československý Gamehosting.</title>
            <!-- meta data -->
            <meta name="author" content="Jan Dvořák">
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <!-- styles -->
            <link rel="stylesheet" href="css/style.css">
            <!-- BOOTSTRAP START -->
            <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" >
            <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
            <!-- BOOTSTRAP END -->      
        </head>
        <body>
            <div class="container-lg">
                <nav class="navbar navbar-expand-md navbar-dark bg-dark my-5">
                    <a class="navbar-brand" href="#">xGhost.cz</a>
                    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
    
                    <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
                        <ul class="navbar-nav ms-auto">
                            <li class="nav-item"><a class="nav-link active" aria-current="page" href="#">Úvod</a></li>
                            <li class="nav-item"><a class="nav-link" href="#">Ceník</a></li>
                            <li class="nav-item"><a class="nav-link" href="#">Objednávka</a></li>
                            <li class="nav-item"><a class="nav-link" href="#">Příručka</a></li>
                            <li class="nav-item"><a class="nav-link" href="#">Servery</a></li>
                            <li class="nav-item"><a class="nav-link" href="#">Technika</a></li>
                            <li class="nav-item"><a class="nav-link" href="#">Kontakt</a></li>
                        </ul>
                    </div>
                </nav>
    
                <div class="row">
                    <div class="content col-md-8">Col here ok? content</div>
                    <div class="login col-md-4">Col here ok? login</div>
                </div>
            </div>
        </body>
    </html>

style.css

.navbar {
    padding-left: 15px;
    padding-right: 15px;
    text-align: center;
}

.content, .login {
    background: #f0f0f0;
}

Here's a preview: https://jsfiddle.net/Ld9f5a0e/

CodePudding user response:

You have to wrap the nav in a container whose class is row

container-lg has some positive margin-left and margin-right, but you have to add row whose margin-left and margin-right is negative, which nullify the effect of margin.

.navbar {
  padding-left: 15px;
  padding-right: 15px;
  text-align: center;
}

.content,
.login {
  background: #f0f0f0;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-lg">
  <div class="row">

    <nav class="navbar navbar-expand-md navbar-dark bg-dark my-5">
      <a class="navbar-brand" href="#">xGhost.cz</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>

      <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
        <ul class="navbar-nav ms-auto">
          <li class="nav-item"><a class="nav-link active" aria-current="page" href="#">Úvod</a></li>
          <li class="nav-item"><a class="nav-link" href="#">Ceník</a></li>
          <li class="nav-item"><a class="nav-link" href="#">Objednávka</a></li>
          <li class="nav-item"><a class="nav-link" href="#">Příručka</a></li>
          <li class="nav-item"><a class="nav-link" href="#">Servery</a></li>
          <li class="nav-item"><a class="nav-link" href="#">Technika</a></li>
          <li class="nav-item"><a class="nav-link" href="#">Kontakt</a></li>
        </ul>
      </div>
    </nav>
  </div>

  <div class="row">
    <div class="content col-md-8">Col here ok? content</div>
    <div class="login col-md-4">Col here ok? login</div>
  </div>
</div>

  • Related