Lucien S.
Lucien S.

Reputation: 5345

Bootstrap Navbar conflicting with another div

I'm trying to add a simple Navbar to a (simple) Leaflet map. I'm having problems setting the Navbar and the map in each their own containers.

Here's my code:

<!DOCTYPE html>
<html>
    <head>
        <title>Title</title>
        <meta charset="UTF-8">
        
<!--    Leaflet-->
        <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
        <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
        
<!--    Jquery  -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>    
        
<!--    Bootstrap-->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
        
        <style>
            html, body {height: 100%; margin: 0;}
            #map { width: 100%; height: 100%; }
        </style>
    </head>
    <body>
        
<!--    Simple Navbar-->
        <nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">       
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto">                 
                    <li class="nav-item">
                        <select class="custom-select">
                            <optgroup label="Menu">
                                <option selected value="0">Option 0</option>
                                <option value="1">Option 1</option>
                                <option value="2">Option 2</option>
                                <option value="3">Option 3</option>
                            </optgroup>
                        </select>
                    </li>
                </ul>
            </div>
        </nav>
        
<!--    Div for my Leaflet map-->
        <div id='map'></div>

        <script>
            myMap = L.map('map')
            myMap.setView([0,0], 3)
            BgLayer = L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png')
            BgLayer.addTo(myMap)
        </script>
    </body>
</html>

And here's what the result looks like:

enter image description here

On this screen grab, we can see that the map (and its zooming button "+/-") is behind the menu, meaning that the map starts at the very top of the browser window instead of at the bottom of the Navbar.

How could I (i.e by taking advantage of Bootstrap's containers?) make sure the Navbar and the map div don't overlap?

Thanks!

Upvotes: 0

Views: 139

Answers (1)

Zeikman
Zeikman

Reputation: 747

You can use display:flex to restructure the layout to fix the overlapping issue.

  1. using a d-flex flex-column h-100 container
  2. using flew-grow-1 for the map container to fit the rest of space and put the map inside

<!DOCTYPE html>
<html>
    <head>
        <title>Title</title>
        <meta charset="UTF-8">
        
<!--    Leaflet-->
        <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
        <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
        
<!--    Jquery  -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>    
        
<!--    Bootstrap-->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
        
        <style>
            html, body {height: 100%; margin: 0;}
            #map { width: 100%; height: 100%; }
        </style>
    </head>
    <body>
        

      <div class="d-flex flex-column h-100">
        <!--    Simple Navbar-->
        <nav class="navbar navbar-expand-lg navbar-light bg-light _fixed-top">       
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto">                 
                    <li class="nav-item">
                        <select class="custom-select">
                            <optgroup label="Menu">
                                <option selected value="0">Option 0</option>
                                <option value="1">Option 1</option>
                                <option value="2">Option 2</option>
                                <option value="3">Option 3</option>
                            </optgroup>
                        </select>
                    </li>
                </ul>
            </div>
        </nav>
        
        <div class="flex-grow-1">
          <!--    Div for my Leaflet map-->
          <div id='map'></div>
        </div>
      </div>

        <script>
            myMap = L.map('map')
            myMap.setView([0,0], 3)
            BgLayer = L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png')
            BgLayer.addTo(myMap)
        </script>
    </body>
</html>

Upvotes: 1

Related Questions