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

No feedback received after sending keys using Selenium

Whenever I execute my code using PhantomJS and Selenium, the result appears to be correct. However, when it comes to using send_keys function, the code gets stuck without any errors, answers, or progress. I am puzzled and eager to find out why this is ha ...

Ensure that the div spans the entire width of the page, prioritize maintaining the aspect ratio, but allow for variable

I have a dilemma with a div containing multiple elements. My goal is to make the div 100% in width, while also attempting to preserve the aspect ratio if feasible. Should the enclosed elements exceed the div's boundaries, then the height will adjust a ...

Some browsers are failing to display the navigation bar on my website

After working on my website, www.alexanderpopov.org, I encountered an issue with the navigation bar disappearing on some computers and browsers. I'm using css to style it, but the problem persists. Below is the HTML code for your reference. Any advice ...

How can I deactivate a specific range without altering the appearance with Bootstrap 5?

My challenge lies in maintaining the style of a range input while disabling user interaction with it. I've tried to recreate the original styling manually, but without success: <!DOCTYPE html> <html lang="en"> <head> <!-- ...

Navigating to the end of a list using Angular's scroll feature

I'm trying to figure out how to automatically scroll to the bottom of a list in TypeScript, can someone help me with this? Here's a similar example I found that uses jQuery (I specifically need it in TypeScript): Scroll to bottom of list wit ...

The Viadeo Social Toolbox seems to be encountering technical difficulties at the moment

I attempted to utilize a Viadeo Social Toolbox, specifically the Viadeo Share Button, but it seems to be malfunctioning in certain browsers? I came across some outdated viadeo share URLs like this: http://www.viadeo.com/shareit/share/?url=${url}&title ...

Mastering the Art of Displaying Every Side of a Spinning Cube Using HTML and CSS Animations

As I navigate the online world, my goal is to create a dynamic animation of a rotating cube with an image on each face. Despite my efforts, I am struggling to display all faces simultaneously, especially the front and back faces. I have explored various so ...

The menu field remains open even after clicking on the menu

I have encountered an issue with my code. Here is a DEMO that I created on jsfiddle.net Currently, when you click on the red div, the menu opens. However, if you click on the menu items, the menu area does not close. What do I need to do in order to clo ...

The CSS styles are not being applied to the PHP code

I seem to have a tangled mess of CSS / PHP / HTML to deal with. The issue I'm facing is that something works on one PHP script but not the other. Here's a snippet of my code: <?php if ($daten->getData_DB_User($get_page_num) != fa ...

Creating custom designs for Material UI components

Although not a major issue, there is something that bothers me. I am currently using react, typescript, and css modules along with . The problem arises when styling material ui components as I find myself needing to use !important quite frequently. Is th ...

An error is thrown when using less.js

Every time I attempt to add less.js, I encounter an XmlHttpRequest Exception 101. Including the .less file is done using this method: <link rel="stylesheet/less" type="text/css" href="anything.less" /> This issue only arises when I upload the th ...

Switch up your code and toggle a class on or off for all elements that share a specific class

I've been attempting to create a functionality where, upon clicking a switch, a specific class gets added to every element that is assigned the class "ChangeColors". Unfortunately, I have encountered some difficulties in achieving this task. The error ...

Challenges with borders within a modal box

I am trying to create some spacing above and below the 'offer' button within my modal. Initially, I attempted to add a 30-pixel border to the bottom of the button, but it doesn't seem to be appearing. The end of the modal aligns directly wit ...

Chrome clipping positioned spans

Having trouble positioning a label above inline sections with spans in Chrome, as the labels are getting clipped oddly. Check out these screenshots: Firefox view: Chrome view: In the Chrome screenshot, you can see that the labels are being cut off based ...

Creating a background image with linear gradients in Rails 5

Does anyone know how to implement a background image with linear gradients in CSS? Here is the code snippet I have: .main-bg-back-1{ background:linear-gradient(180deg,#22263f,rgba(33,41,94,.96)),url(../img/app/download-bg.jpg) no-repeat; backgroun ...

Using React components with Material UI to create styled div boxes

As I work on creating a mock website with React and Material UI, I have utilized the RaisedButtonExampleSimple component for buttons from Material UI within a div named App-Intro. Strangely, the text in the Div "Customer-Choice" is displaying above the but ...

What is the best way to smoothly hide and reveal a flex child element?

I am looking to create a flex container where the child element can smoothly animate to shrink and grow, while the remaining child elements expand to fill in the space. The method I have found involves setting the max-width to 0px and the borders' wi ...

I must modify the initial statement to appear in bold from the paragraph with the use of JavaScript and CSS

Mr. XYZ and Mrs. ABC are known for their integrity. They own a stylish car. In the next paragraph, I would like to emphasize the first sentence by making it bold, We admire our exceptional leader J.K.L. He shows great potential. In this section, I wan ...

Issues with HTML5 tag <animate> in Internet Explorer 11

After getting the hang of some HTML basics, I decided to get creative and make an animation. Surprisingly, it works perfectly on my Firefox browser. However, when I tested it on IE 11, it failed to play the animation as expected. Everything is displayed bu ...

Implementing gridlines on a webpage using HTML and JavaScript to mimic the grid layout found in Visual Studios

Currently, I have a grid snap function in place, however, I am looking to add light grey lines horizontally and vertically on my Designing application. The goal is to achieve a similar aesthetic to Visual Studios' form designing feature. Utilizing so ...