Struggling to place image behind Bootstrap navbar due to z-index issues

This layout features a navigation bar:

<header id="navigation-bar" class="nav-header">
    <nav class="navbar navbar-expand-lg">
        <a class="navbar-brand" href="#">
            <img src="images/logo.svg" alt="logo-icon">
        </a>
        <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 ml-auto">
                <li class="nav-item active">
                    <a class="nav-link" href="#">How we work <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item item2 active">
                    <a class="nav-link" href="#">Blog<span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item item2 active">
                    <a class="nav-link" href="#">Account <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item active">
                    <a class="nav-link nav-btn-link" href="#"><button class="nav-btn btn btn-outline-dark">View Plans</button><span class="sr-only">(current)</span></a>
                </li>
            </ul>
        </div>
    </nav>
</header>

The image bg-img1 is intended to be positioned behind the navbar:

<section id="Heading">
    <img class="bg-img1" src="images/bg-pattern-intro-right-desktop.svg" alt="bg-pattern-left">
    <div class="container-1">
        <div class="heading-text">

Attempts have been made to position the header around the navbar and adjust the z-index, but it doesn't seem to achieve the desired effect.

.nav-header {
    position: relative;
    padding: 1%;
    z-index: 10000;
}

This is how the image has been styled for positioning:

.bg-img1 {
    position: absolute;
    right: 0;
    width: 40%;
    top: 3%;
    z-index: 2;
}

Answer №1

Your approach is correct, but the issue lies in assigning the src attribute to your img, causing it to appear in front of the navbar rather than behind it.

To resolve this, consider using the CSS background property for the img element. This will allow the image to be displayed behind the navbar without requiring the use of any z-index.

.bg-img1 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url(https://images.unsplash.com/photo-1590845947667-381579052389?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60) no-repeat;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">


<section id="Heading">
  <img class="bg-img1" alt="bg-pattern-left">
  <div class="container-1">
    <div class="heading-text">
    </div>
  </div>
</section>

<header id="navigation-bar " class="nav-header">
  <nav class="navbar bg-dark navbar-expand-lg">
    <a class="navbar-brand" href="#">
      <img src="https://placehold.it/20x20" alt="logo-icon">
    </a>
    <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 ml-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">How we work <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item item2 active">
          <a class="nav-link" href="#">Blog<span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item item2 active">
          <a class="nav-link" href="#">Account <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item active">
          <a class="nav-link nav-btn-link" href="#"><button class="nav-btn btn btn-outline-dark">View
                  Plans</button><span class="sr-only">(current)</span></a>
        </li>
      </ul>
    </div>
  </nav>
</header>

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

Acquire $(this) when the onclick event is activated

Usually, I rely on a jQuery event handler for CSS styling. However, today I decided to experiment with using the HTML event attribute instead. Below is the code that I used: $('.navBtn').on('click', function() { var length = $(th ...

Retrieving the dimensions of an iframe using JavaScript

I currently have an iframe on my page with a height of 0px and width of 100px that I would like to access using JavaScript. Although I can grab the iframe element using window.frames[0], I'm struggling to retrieve the actual width and height values. ...

Is the HTML templating mechanism compatible with Angular?

Trying to implement HTML templating in my Angular application, my code looks like the following: <script type='text/html' id="sampleId">...</script> However, upon loading the HTML, I noticed that the script block was not present in ...

An issue has arisen regarding the width of the second image

Can you assist me in resolving an issue related to the width of the second image? I have included a link for your reference if you wish to examine the problem. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

Setting a radio button as default based on a variable value can be accomplished by following a few

I am working with 2 radio buttons and I need to dynamically check one based on a variable value. <input type="radio" name="team" id="team_7" value="7"> <input type="radio" name="team" id="team_8" value="8"> The variable number is set dependin ...

"Utilize an HTML input to query and retrieve data stored in a

Seeking to retrieve data from my MySQL database and display it on the website securely. List of existing files: /includes db_connect.php functions.php getdata.php logout.php process_login.php psl-config.php register.inc.php /j ...

How can I include an HTML tag within a select input value in a React.js project?

I am facing an issue while trying to map values from the input field of a select tag. It seems to be returning [Object object] for some reason. When I do not include another tag in the return statement of the map function, the output works fine. However, I ...

Ways to resolve navbar toggler issue in Bootstrap 4

My problem lies in creating a responsive navbar using bootstrap. On smaller screens, the toggler in the navbar ends up overlapping the logo instead of aligning to the left. I've attempted to add containers around the elements to fix this issue but to ...

What is the best way to prevent images from being loaded with broken links?

Currently, I am working on a new feature that involves rendering images from servers. In the process of aligning these images, I noticed an excessive amount of white space due to loading images with broken links. https://i.stack.imgur.com/jO8BR.png Here ...

Prevent user input in a text box once a radio button is clicked

I have been attempting to create a JavaScript function that will disable a textbox when a radio button is selected. I have four different options for the radio buttons, each with a corresponding textbox. However, even when I select radio button 2, I am s ...

Utilizing aria-expanded="true" for modifying a CSS attribute: A simple guide

I am looking to utilize aria-expanded="true" in order to modify a css property such as an active class : <li class="active"> <a href="#3a" class="btn btn-default btn-lg" data-toggle="tab" aria-expanded="true"> <span class="networ ...

I am experiencing some difficulties with the navbar code from Bootstrap

` I recently started learning about Bootstrap and encountered an issue while trying to create a navbar. Interestingly, I wasn't even attempting to add any elements to it at that point. My main focus was to simply insert my company's or brand&apo ...

Proper alignment of content in HTML using Bootstrap following the integration of .json data

After aligning the emergency hotlines properly, I am struggling to position the text under the image to display the data in three rows. I attempted col-4 but encountered issues with JSON. My objective is to show the image followed by the numbers directly b ...

Implementing a method to display HTML tags within text in a React application

I have been working on a text highlighting feature, and I have successfully detected the selection. Currently, I have something like Te<span>x</span>t within a string, represented as an array: ["Te", "<span>", "x ...

How to play audio with a source path that includes the special character "%" in JavaScript

Having an issue with playing an audio file that has '%' in its name. I've tried using the tag <audio src="test%320.mp3" controls></audio>, but it seems like the file is not being found when I try to play it. Can anyone ...

Issues with appending new rows using JavaScript

I'm facing an issue with adding rows to the table using this code and I can't seem to find a solution. function add() { document.getElementById("popup").style.display = "block"; document.getElementById("add").addEventListener("click", func ...

To modify the color of the breadcrumb navigation bar from purple to #d6df23 using PHP

Can someone help me figure out how to change the color of the bar displayed in this link: I need assistance with modifying the breadcrumb navbar. Is there a way to change the color to yellow? I attempted editing the header file, but it didn't produ ...

Pressing "Enter" initiates the submission of the form

My webpage contains a stylish GIF displayed as a button within a div. The script inside the div triggers a click event when the ENTER key is pressed, using $(this).click(). This click action has its own functionality. Everything functions smoothly in Fire ...

Avoid reloading the page in PHP when the browser back button is clicked

I've built an HTML form where the values are passed to a second page using POST method. On the second page, I have an edit button that, when clicked, redirects back to the HTML form page with the user's typed values. However, my dilemma is figuri ...

What is the default way to toggle content in rows using Material UI?

Currently, I am utilizing Muitables and have a query regarding how to set the expanded rows to be displayed by default, similar to the illustration below: Upon initial page load, it is preferred for the content in that row to automatically expand (arrow d ...