What is the best way to layer multiple navigation menus on top of each other using z-index?

I'm facing a challenge trying to position or overlap two navigation menus in my project. The technologies I am using are Bootstrap, HTML, and CSS.

It seems like I might need to utilize the z-index property, but I'm unsure about the exact implementation.

Below is the code snippet that I have been working on along with an image illustrating the desired outcome.

Code:

<!DOCTYPE html>
<html>
<head>

  <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
    <link rel="stylesheet" href="styles.css" type="text/css">
  </head>
  <body>

  <nav class="navbar navbar-expand-md bg-white navbar-white">
    <a class="navbar-brand"  href="#">
    <img src="assets/logo2.png"height="84px">
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="collapsibleNavbar">
      <ul class="accessibility">
        <li class="nav-item">
          <a class="contrast">Contrast</a>
        </li>
        <li class="Font-size">Font size</a>
        </li>
        <li class="download">Download a brochure</a>
        </li>
      </ul>
      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link" href="#">New Vehicles</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Nearly New/ Used Vehicles</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Motability</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Aftercare</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Contact Us</a>
        </li>
      </ul>
    </div>
  </nav>
  <br>
  <nav class="navbar navbar-inverse bg-primary">
  <ul>
      <li class="sec-menu">Versa Connect</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li class="sec-menu"><a href="#">Overview</a></li>
        <li class="sec-menu"><a href="#">Offers</a></li>
          <li class="sec-menu"><a href="#">Models</a></li>
            <li class="sec-menu"><a href="#">Optional Extras</a></li>
      </ul>
</nav>
  <section id="cover_image">
    <div class="container">
 <img src="assets/cover_image.jpg" >
 <div class="text-block">
   <h1>Versa Connect</h1>
   <p>The Ford Torneo Connect is a relaxing and comfortable place to be.
   Great to drive, great to sit in, it boasts premium finish, high quality materials
 and fuel efficiency without compromising everyday practicality. </p>
 <button type="button" class="btn btn-lg btn-primary">Make an enquiry</button>
 </div>
</div>
    </body>
</html>

CSS

h1{
  font-family: roboto;
}

p{
  font-family: roboto;
  font-size: 18px;
}
li{
  font-family: roboto;
  font-size: 18px;
  display:inline;

  padding: 30px;
}

li a{
  color: #000;
}
.navbar-right{
  float: right !important;
  margin-right: -15px;
  display: inline;
}
ul.nav navbar-nav navbar-right li a{
  display: flex;
}
li.sec-menu a{
  color: #fff;
  font-family: roboto;
}
.container{
  margin: 0;
  padding: 0;
}
.text-block {
  text-align: center;
  position: absolute;
  bottom: 10px;
  left: 100px;

  background-color:rgba(255,255,255,0.66);
  color:#000;
  width: 380px;
  padding:50px;
}
.btn btn-lg btn-primary{
  padding-left: 10px;
  width: 360px;
}

I would really appreciate it if someone could guide me through this coding challenge and help me understand how and where to incorporate the z-index in the code. You can view the image for reference https://i.stack.imgur.com/eE4qX.jpg.

Answer №1

The z-index property is utilized to stack items on top of one another along the z-plane, resembling a 3-dimensional layout. Therefore, this property will not be beneficial in this situation. To achieve the desired outcome, create each navbar using distinct "nav" or "div" tags to place them on separate lines.

You are heading in the right direction, but your code contains multiple errors causing the issue at hand. The navbar comprising "Contrast, Font Size, Download a Brochure" has closing anchor tags without corresponding opening anchor tags. Additionally, this navbar is assigned a class "accessibility" that is not defined in your CSS stylesheet, resulting in an unstyled unordered list.

Below is a simplified version of the solution you seek:

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
}

li {
  float: left;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover {
  background-color: #111;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="./style.css">
</head>
<body>
<nav>
<ul>
<li><a href="#">Contrast</a></li>
<li><a href="#">Font Size</a></li>
<li><a href="#">Download a Brochure</a></li>
</ul>
</nav>
<nav>
<ul>
<li><a href="#">New Vehicles</a></li>
<li><a href="#">Nearly New/Used Vehicles</a></li>
<li><a href="#">Motability</a></li>
<li><a href="#">Aftercare</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</nav>
</body>
</html>

Answer №2

This script runs smoothly without the requirement for z-index. For a demo, visit codepen here https://codepen.io/anon/pen/Rvdgaq

<nav class="navbar navbar-expand-md bg-white navbar-white">
    <a class="navbar-brand"  href="#">
         <img src="assets/logo2.png"height="84px">
    </a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
  <span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="collapsibleNavbar">

 <ul class="accessability">
    <li class="nav-item">
      <a class="contrast">Contrast</a>
    </li>
    <li class="Font-size">Font size</a>
    </li>
    <li class="download">Download a brochure</a>
    </li>
   </ul>
<ul class="navbar-nav">
    <li class="nav-item">
      <a class="nav-link" href="#">New Vehicles</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Nearly New/ Used Vehicles</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Motability</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Aftercare</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Contact Us</a>
    </li>
  </ul>
</div>
</nav>

    <nav class="navbar navbar-inverse bg-primary">
  <ul>
      <li class="nav-item sec-menu">Versa Connect</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li class="nav-item sec-menu"><a href="#">Overview</a></li>
        <li class="nav-item sec-menu"><a href="#">Offers</a></li>
        <li class="nav-item sec-menu"><a href="#">Models</a></li>
        <li class="nav-item sec-menu"><a href="#">Optional Extras</a></li>
      </ul>
</nav>

  </div>

</div>
 <section id="cover_image">
    <div class="container">
      <div class="row">
         <img src="assets/cover_image.jpg" >
 <div class="text-block">
<h1>Versa Connect</h1>
<p>The Ford Torneo Connect is a relaxing and comfortable place to be.
   Great to drive, great to sit in, it boasts premium finish, high quality materials
 and fuel efficiency without compromising everyday practicality. </p>
 <button type="button" class="btn btn-lg btn-primary">Make an enquiry</button>
 </div>
</div>
</div>
 </section>

I noticed a couple of things: 1. The section tag was not properly closed which may be causing errors. Also, the 'position: absolute;' on .text-block may be unnecessary - try changing it to 'position: relative' instead.

Make sure to update these details for optimal performance!

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

Tips for keeping a video background stationary while allowing the text to move smoothly

When I added the video for the background, it only appears at the top of the page. However, I want it to be visible as the rest of the page is scrolled. <div class="hero"> <video autoplay loop muted plays-inline class="back-video&qu ...

Using multiple background images in CSS on mobile devices

I decided to experiment with creating a grid pattern using css background-images in this way: .grid { width:720px; height:720px; background-color: #333333; background-image: linear-gradient(rgba(255,255,255,0.1), 1px, transparent 1px), ...

Bootstrap Scrollable Card

Currently, I am working with bootstrap 4 and have set up a layout with 3 columns in a single row. One of the columns contains a list while the other two do not. I am trying to make only the column with the list scrollable without affecting the entire conta ...

Run a JavaScript function in 5 seconds

I'm looking to execute this function after a 5-second delay: $(document).ready(function() { $("#signInButton").trigger('click'); }); Your assistance is greatly appreciated. ...

Unable to eliminate top padding without using universal selector

Trying to remove the top padding on a webpage, but encountering some difficulty. Here's the HTML code: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="style.css"> <title&g ...

Sending HTML parameters to a PHP file

I have been trying to pass the parameters from the current HTML page to a PHP page using a form. In my header in the HTML, I currently have the following function: <script> function getURLParameter(name) { return decodeURIComponent((new Re ...

Attempting to create a login and registration form

Hello, I am attempting to create a form that can generate new user accounts and passwords. These values should be stored from the input tag when the user clicks on the register button. Unfortunately, I am encountering an issue where clicking the register ...

Column 1 with fixed headers

Is there a way to make sticky headings in a single column scroll without them overlapping? Currently, my h1s are overlapping when I scroll down. Update: I would like the headings to stack on top of each other as I scroll, instead of being pushed off scree ...

Enable vertical scrolling on the second row of a table while keeping the first row fixed as the table header (CSS)

While embedding the Ace Editor in a Chrome popup window, I encountered a challenge. My goal was to keep the first row of text always visible at the top while allowing the rest of the Editor to fill the screen. However, there is an issue with a vertical scr ...

What is the technique for anchoring elements at the top of the page when scrolling?

There is a common design feature where the sidebar on the left starts at a lower position on the page, but as you scroll it moves up to the top and remains fixed in place instead of disappearing off the screen. This is a popular design trend that I have ...

Tips for breaking out of the foundation row and aligning with the right edge of the viewport

I have a CodePen example showcasing the Zurb Foundation grid. I am looking for a way to make a div extend to the right edge of the viewport while keeping the left edge in line with the grid as the viewport is resized. <div class="row"> <div cla ...

Objective-C and the World of WebSockets

Possible Duplicates: Comparison of WebSockets, TCP/IP, and JavaScript/AJAX for iPhone chat Integrating WebSockets into a Cocoa application Hello all, our team is embarking on creating a bespoke iPhone chat app and deliberating the use of WebSocket ...

jQuery Mobile is experiencing page height inaccuracies

My web application, built with jQuery Mobile 1.2.0, is encountering an issue with page height calculations on Windows Phone. While iOS and Android display correctly, there is a gap at the bottom of the page on Windows Phone. Is there a CSS-only solution t ...

Unable to configure AngularJS inputs to have a blank default value

I am facing a peculiar issue where I am unable to initialize my input fields as blank/empty. Even after clearing the cache and performing a hard reload, Google Chrome seems to be auto-populating them with older cached values. Here is the current view: Th ...

Unlocking the potential of resizable bootstrap table columns in React

Currently utilizing a bootstrap table <div class="table-responsive"> <table class="table table-bordered"> <thead> <tr> <th>#</th> <th>Table heading</th> </tr> < ...

Text alignment from the lower edge

Imagine having a title inside an image, positioned near the bottom with a negative margin-top. The issue arises when the text orientation expands from top to bottom as more content is added. Is there a way to reverse this, so that the text div grows toward ...

What is the solution to making text appear on the top rather than on the side?

Seeking a way to keep the text description above the textbox within a definition list, however currently the text appears to the left of the box. Here is the provided HTML structure: <dl> <dt> <label>Phone Number</label> &l ...

Need assistance with a coin flip using HTML and Javascript?

After spending hours trying to make the code work with the example provided, I find myself unable to get it functioning correctly. Is there anyone who can assist me in putting all of this together so that it runs smoothly? var result,userchoice; functio ...

Trigger a function when clicking outside the element, similar to how a Bootstrap modal is closed

I have successfully created a popup box using angular in my project. It appears when I click on an icon and disappears when I click on the close button. However, I would like it to also close if someone clicks outside of the popup. Can anyone help me with ...

WindiCSS classes are not functioning properly within a Nuxt application

I've been encountering some difficulties with WindiCSS. The classes are not being applied to the elements as expected. I attempted to install an older version of Windi, but the issue persisted. I even tried switching to Tailwind instead of Windi, but ...