Mastering the art of aligning menus in Bootstrap 4 like a pro

I am encountering difficulties with my coding as I try to align the menu sandwich in my header correctly. The issue arises when I minimize the browser width, causing the menu sandwich to be positioned below the logo instead of to the right of it. Additionally, I would like the menu sandwich to be located at the far right of the header when the browser window is larger. Despite conducting research, I am struggling to figure out how to manipulate this menu effectively. While the codes I have come across are similar regarding the menu sandwich, they do not exhibit the same behavior as on my website.

Here's my code:

HTML

<!doctype html>
<html>
<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 href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="style.css" rel="stylesheet">
  <link href='http://fonts.googleapis.com/css?family=Droid+Serif' rel='stylesheet' type='text/css'>
  <link href='http://fonts.googleapis.com/css?family=Droid+Sans' rel='stylesheet' type='text/css'>
  <link href="https://fonts.googleapis.com/css?family=Cantarell" rel="stylesheet">
  <title>Rev'Iso entreprise de rénovation et d'isolation (rénovation énergétique) de votre habitat.</title>
  <meta name="description" content="Rev'iso vous propose ses artisans pour la réalisation de différents travaux de rénovation et d'isolation dans 
votre habitat avec des certications de qualité." />
</head>
<body>
  <div class="container-fluid" style="background:transparent url('images/fond_header8.jpg') no-repeat center center;min-height : 485px;">

    <div class="row" style="background:transparent url('images/bar.png') repeat-x;">
      <!-- Taille 6 sur smartphones, 4 sur tablettes, 3 sur desktop et plus -->
      <div class="col-xs-6 col-sm-4 col-md-3">
        <img src="images/logo.png">
      </div>
      <!-- Taille 6 sur smartphones et tablettes, 7 sur desktop et plus -->
      <div class="col-md-3 d-none d-lg-block">
        <span class="textTel"> <img src="images/telline.svg"/ width="95px" height="70px"> 09 82 52 79 04</span>
      </div>
      <!-- Masquée sur smartphones, taille 2 sur tablettes et plus -->
      <div class="col-sm-7 col-md-6 hidden-xs" style="vertical-align:middle;">
        <nav class="navbar navbar-expand-lg navbar-light bg-light">

          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
          <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav">
              <li class="nav-item active">
                <a class="nav-link" href="index.html">Accueil <span class="sr-only">(current)</span></a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="renovation.html">Rénovation</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="isolation.html">Isolation</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="devis.html">Devis</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Contact</a>
              </li>
            </ul>
          </div>
        </nav>
      </div>
    </div>

CSS :

body{
  background-color: #C7C8C9;
  width:100%;
  margin:0px;
  padding:0px;
}

.container-fluid { 
  padding-right: 0px;
  padding-left: 0px;
  margin-right: auto;
  margin-left: auto;
  padding-bottom :0px;
  padding-top:0px;
}
/* More CSS styles go here */

screenshots :

https://i.sstatic.net/JEGSY.png

https://i.sstatic.net/P5VjX.png

I appreciate any assistance provided.

Answer №1

To be honest, there is quite a lot of code here which may make it challenging to explain the solution clearly. Hopefully, this code snippet will help you with your issue. Feel free to ask any questions in the comments.

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous></script>;
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>

<div class="container-fluid">
  <div class="row bg-light">
    <div class="col">
      <nav class="navbar navbar-expand-lg navbar-light justify-content-sm-end px-0">
        <div class="row no-gutters w-100 align-items-center">
          <div class="col-auto col-lg-3">
            <a class="navbar-brand" href="#">Logo</a>
          </div>
          <div class="col-auto col-lg-3">
            <span class="textTel"> 09 82 52 79 04</span>
          </div>
          <div class="col d-lg-none text-right">
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
          </div>
          <div class="col-12 col-lg-6">
            <div class="collapse navbar-collapse" id="navbarNav">
              <ul class="navbar-nav">
                <li class="nav-item active">
                  <a class="nav-link" href="index.html">Accueil <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="renovation.html">Rénovation</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="isolation.html">Isolation</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="devis.html">Devis</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="#">Contact</a>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </nav>
    </div>
  </div>
</div>


It's worth noting that Bootstrap has removed all variations of the '-xs- classes.

https://codepen.io/anon/pen/pOWMZL

Answer №2

After working through the problem, I've come up with a more accurate code for the navbar.

<body>
    <div style="background:transparent url('images/fond_header8.jpg') no-repeat center center;min-height : 485px; position:relative;">



                    <!-- Hidden on smartphones, size 2 on tablets and above -->

                    <nav class="navbar navbar-expand-lg navbar-light bg-light" style="background:transparent url('images/bar.png') repeat-x;">

                        <div class="navbar-header">

                        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">

                        </button>
                        <a class="navbar-brand" href="index.html"><span class="logo"><img src="images/logo.png"></span></a>
                        </div>


                          <div class="collapse navbar-collapse" id="navbarNav" style="position:absolute; right:100px;bottom:80px;">
                            <ul class="nav navbar-nav ml-auto">
                              <li class="nav-item active">
                                <a class="nav-link" href="index.html">Home<span class="sr-only">(current)</span></a>
                              </li>
                              <li class="nav-item">
                                <a class="nav-link" href="renovation.html">Renovation</a>
                              </li>
                              <li class="nav-item">
                                <a class="nav-link" href="isolation.html">Insulation</a>
                              </li>
                              <li class="nav-item">
                                <a class="nav-link" href="quote.html">Quote</a>
                              </li>
                              <li class="nav-item">
                                <a class="nav-link" href="#">Contact</a>
                              </li>
                            </ul>

                          </div>
                        </nav>

Now, there's just one issue remaining. I used position relative on the first div and absolute on the menu to center it because line-height wasn't functioning as expected:

.navbar-nav li{
    background : url(images/separateur.png) no-repeat;
background-position : left;
}

.navbar-nav li a {
    color:blue;
 line-height: 80px;

}

If anyone knows why this is happening, please let me know. Otherwise, I'll stick with this solution. Thank you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can someone show me how to code a function that transforms my paragraph into an image when I hover over it?

< p id="p1" onmouseover="this.style.color='transparent';flipPara()"> < p id="p2" onmouseover="spookyPara()"> function spookyPara() { document.getElementById("p1").attribute = "All_Images/ghost.png"; } I currently have this code sn ...

Enhancing NG Style in Angular 6 using a custom function

Today, my curiosity lies in the NG Style with Angular 6. Specifically, I am seeking guidance on how to dynamically update [ngStyle] when utilizing a function to determine the value. To better illustrate my query, let me present a simplified scenario: I ha ...

Using jQuery to toggle the visibility of div elements while displaying an indicator

A query arises regarding the utilization of JQuery to facilitate toggling the visibility of text divs. The question pertains to how one can incorporate an indicator - such as an up and down arrow graphic - to denote whether the divs are open or closed. Sug ...

Retrieving the Content of Textarea Following Form Submission

When attempting to retrieve the value of my text area after submitting a form, I utilized the following command: $message = $_POST['message']; Unfortunately, this approach did not yield the desired result. This may be due to it not being an inp ...

Craft a backdrop with subtle hints of italics

I am looking to create a unique background for my webpage using HTML and CSS. I want to have repeated italic shades in various shades of blue, red, and white, similar to rectangular shapes. For inspiration, take a look at the design on this website: If n ...

Tips for decreasing Vuetify Grid column width on larger screens

I'm currently utilizing the vuetify grid system to construct a reply component. The column ratio is set at 1:11 for the icon and reply columns, respectively. Below you'll find the code snippet: <v-row justify="center" class="ma-0 pa-0"> ...

Using Django to render multiple images for a post

For the past few days, I have been attempting to provide the admin user with the ability to upload multiple images/slides for each individual post. One idea that came to mind was nesting a for loop inside the posts' for loop so that for every post, al ...

How can I retrieve the class's name rather than its content?

When looking to retrieve elements by their class name in JavaScript, document.getElementsByClassName() is a helpful method. However, is there a built-in JavaScript function that can return the name of the class itself? For example: var classElement ...

"Embedding Bootstrap, jQuery, and Twitter Bootstrap directly onto

I'm currently working on customizing some source code to fit my needs. To do this, I need to download and use Bootstrap, jQuery, and Bootstrap locally. I have already downloaded all of them to a directory named "libs". However, when I try to run the P ...

Implementing validation for multiple email addresses in JavaScript: A step-by-step guide

Currently, I am utilizing Javascript to perform validation on my webpage. Specifically, I have successfully implemented email validation according to standard email format rules. However, I am now seeking assistance in enhancing the validation to allow for ...

Issue with implementing custom fonts using @font-face

I've added custom @font-face styling to my CSS using a downloaded font, but it doesn't seem to be working. Below is my CSS code: @font-face { font-family: "Quicksand"; src: url("fonts/Quicksand/Quicksand-Light.otf") format("opentype"); } The f ...

Issue with Iconify icon not updating when "data-icon" is set using setAttribute()

I'm having trouble trying to animate or replace an icon using the "setAttribute" method. Can someone take a look at my code and help me figure out what's wrong? <!DOCTYPE html> <html> <script src="https://code.iconify.design/1/1 ...

The waveform's bottom appears distorted when using Bootstrap

I'm currently experimenting with creating a header wave in Bootstrap CSS using SVG shape. However, I encountered an issue when implementing bootstrap into the HTML code - my design resembles the second image instead of the desired look of the first on ...

Develop fresh JavaScript code using JavaScript

Is it possible to dynamically create a new row in an HTML table by clicking on a button? Each row contains multiple input fields. <script type="text/javascript> row = 2; specific_row_id = new Array(); $(document).ready(function() { $(".change_1 ...

Managing multiple element click events through bubbling

I have a collection of controls contained within a main div called overlay-controls. Each control has its own set of overlay-controls. To handle deletion, I am using a for loop to attach an event listener to each button with the class delete. Prior to a ...

Looking to switch the markers from circles to icons in the GeoChart component of react-google-charts

During my personal project, I came across the react-google-charts library and discovered a method to add markers to the map. <Chart width={"200%"} height={"100%"} chartType={"GeoChart"} data={geoData} rootProps={{ "data-testid": "1" ...

Arrange and pile up 3 columns using Bootstrap 4

My project currently has a structure using bootstrap columns, as shown in the image below: https://i.sstatic.net/JufaU.png However, I need to change to a lower resolution and rearrange the columns like this: https://i.sstatic.net/IB9sY.png I came acr ...

Ensuring that objects remain fixed in place regardless of window resizing is a common task in web development, often achieved through a

I've been attempting to create range sliders with boxes that display the slider's current value, but I'm having trouble getting them positioned correctly when the window size changes. Despite trying various solutions found online, I resorted ...

What is the reason behind this button disrupting the arrangement of my design?

Whenever I attempt to add this customized CSS button to my company's website, it disrupts the center alignment of the entire webpage. Below is the HTML code: <div id="contain"> <button class="support"> </button> ​ Here is the c ...

Issues encountered in loading JavaScript with jQuery script

I am facing an issue with my jQuery script not loading correctly. When I click on the calculate button, nothing happens as expected. I made sure to copy and paste jQuery directly into the file for offline use, and also created a personal console due to res ...