I am struggling to layer the boxes using z-index

I have been struggling to find a solution to the problem, but nothing seems to be working. My goal is to create a gradient border for a slideshow on my website, similar to the one showcased in this video. I attempted to use the :before pseudo selector in my HTML code, which looks like this:

* {
  color: white;
  margin: 0px 0px;
  padding: 0px 0px;
  box-sizing: border-box;
}

body {
  background-color: #060c21;
}

/* NavBar Starts */
#mainnav {
  display: flex;
  justify-content: center;
  padding: 15px;
  background-color: black;
  width: 98%;
  position: static;
}

.items>a {
  font-family: "Roboto Slab", serif;
  text-decoration: none;
}

.items {
  margin: 0 5vw 0 5vw;
  padding: 1vw;
  width: fit-content;
  font-size: 1.3rem;
}

.items>a:hover {
  color: rgb(0, 255, 242);
}

/* NavBar Ends */

/* Content Starts */
.slide-box {
  position: relative;
  border: 2px solid red;
  height: 75vh;
  width: 95%;
  margin: 2% auto;
}

.slide-box:before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: white;
  z-index: -1;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Practice</title>

  <link href="https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@531&display=swap" rel="stylesheet" />
  <!-- font-family: 'Roboto Slab', serif; -->
  <link rel="stylesheet" href="Vaishnavi.css" />
</head>

<body>
  <nav>
    <div id="mainnav">
      <div class="items">
        <a href="Vaishnavi.html">Home</a>
      </div>

      <div class="items">
        <a href="">About US</a>
      </div>

      <div class="items">
        <a href="">Creations</a>
      </div>

      <div class="items">
        <a href="">Help</a>
      </div>
    </div>
  </nav>
  <div class="slide-box">
    <h1>This is glowing box</h1>
    <p>
      Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quo voluptatibus et quasi illum inventore rem excepturi quam tenetur eius est, minima aliquam repellendus deleniti modi laudantium similique iste ipsum? Ad!
    </p>
  </div>
</body>

</html>

I would greatly appreciate it if someone could point out where I might be going wrong and why my z-index is not functioning as expected. I am confident that my code is correct, so any insights would be highly valued.

Answer №1

Are you satisfied with this solution?

After carefully analyzing the example, I have reconstructed it using the information from the video you shared.

* {
  color: white;
  margin: 0px 0px;
  padding: 0px 0px;
  box-sizing: border-box;
}

body {
  background-color: #060c21;
}


/* NavBar Starts */

#mainnav {
  display: flex;
  justify-content: center;
  padding: 15px;
  background-color: black;
  width: 98%;
  position: static;
}

.items>a {
  font-family: "Roboto Slab", serif;
  text-decoration: none;
}

.items {
  margin: 0 5vw 0 5vw;
  padding: 1vw;
  width: fit-content;
  font-size: 1.3rem;
}

.items>a:hover {
  color: rgb(0, 255, 242);
}


/* NavBar Ends */


/* Content Starts */

.slide-box {
  position: relative;
  margin: 2% auto;
  height: 75vh;
  width: 95%;
  background: linear-gradient(0deg, #000, #262626);
}
.slide-box:before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  background: linear-gradient(45deg, #fb0094, #0000ff, #00ff00, #ffff00, #ff0000, #fb0094, #0000ff, #00ff00, #ffff00, #ff0000);
  background-size: 400%;
  width: calc(100% + 4px);
  height: calc(100% + 4px);
  z-index: -1;
  animation: animate 20s linear infinite;
}

@keyframes animate {
  0% {
    background-position: 0 0;
  }
  50% {
    background-position: 400% 0;
  }
  100% {
    background-position: 0 0;
  }
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Practice</title>

    <link href="https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@531&display=swap" rel="stylesheet" />
    <!-- font-family: 'Roboto Slab', serif; -->
    <link rel="stylesheet" href="Vaishnavi.css" />
  </head>
  <body>
    <nav>
      <div id="mainnav">
        <div class="items">
          <a href="Vaishnavi.html">Home</a>
        </div>

        <div class="items">
          <a href="">About US</a>
        </div>

        <div class="items">
          <a href="">Creations</a>
        </div>

        <div class="items">
          <a href="">Help</a>
        </div>
      </div>
    </nav>
    <div class="slide-box">
      <h1>This is glowing box</h1>
      <p>
        Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quo voluptatibus et quasi illum inventore rem excepturi quam tenetur eius est, minima aliquam repellendus deleniti modi laudantium similique iste ipsum? Ad!
      </p>
    </div>
  </body>
</html>

To fix the issue with the text visibility, make sure to include the following line in your code:

.slide-box {
  background: linear-gradient(0deg, #000, #262626);
}

The z-index property was functioning correctly, but the white background and text color made the text invisible. Adding the above line will address this problem.

Answer №2

If you're looking to create a gradient border in your code, you can follow this example borrowed from css-tricks. For more information, visit here

body {
  height: 100vh;
  margin: 0;
  display: grid;
  place-items: center;
  background: #222;
}

.module-border-wrap {
  max-width: 250px;
  padding: 1rem;
  position: relative;
  background: linear-gradient(to right, red, purple);
  padding: 3px;
}

.module {
  background: #222;
  color: white;
  padding: 2rem;
}
<div class="module-border-wrap">
  <div class="module">
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Vero pariatur corporis quaerat voluptatum eos tempora temporibus nisi voluptates sed, exercitationem sequi dolore culpa incidunt accusamus, quasi unde reprehenderit ea molestias.
  </div>
  
</div>

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

How can I prevent my mouse click from being overridden by my mouse hover?

Currently, I am developing a Sudoku game using JavaScript and facing some challenges with mouse events. My goal is to enable users to hover over a square and have it change color, as well as click on a square to highlight the entire row, column, and quadra ...

The art of arranging elements on a webpage using HTML and

I designed a rectangle using position: relative; and placed an icon inside it. However, when I added a new div class called .Home, and included an element <h4>Home</h4> with position: relative;, the element appeared outside of the rectangle. Wh ...

Is it possible for ng-model to verify its own value?

Recently, I've been experimenting with Angular and found myself facing a dilemma. Is it possible to apply a different CSS style based on whether the value of ng-model is greater than 0? `<span ng-model="users2" ng-hide="!myVar" ng-class="{'te ...

"Troubleshooting: Why Won't insertAfter Function Work with

I have a p Element that I want to wrap inside a span tag. Then, I need to insert it after another p tag with the id output. Despite my attempts, the insertAfter function is not working as expected. $mytext = $("p#test").html(); $myspan = "<span styl ...

Facing a dilemma: Javascript not updating HTML image source

I am facing an issue while attempting to modify the source of my HTML image element. Despite using document.getElementId('image') in my code, I am unable to make it work as intended. Surprisingly, there are no errors displayed. Interestingly, whe ...

Conceal a different CSS class if a certain CSS class is present on the page

I am currently working on organizing my page to distinguish between purchased and unsold products. For the items that have been bought, I am using the CSS class "winning_bid" within a div element. My goal is to hide another div with the class "price" if th ...

Changing between two images using HTML and CSS

I am currently designing a WordPress theme and I would like to create an effect where two different thumbnail images switch on hover. The code that I have come up with so far looks something like this : <a class="thumb" href="posturl"> <img src= ...

What is the best way to locate and list all video links, and include options for "Play Video" and "Download Video"?

I have a project where I am using PHP to crawl and generate video links from the web. Now, I am looking to implement an option for users to either "Play Video" or "Download Video" when a video link is detected, along with adding a video player if the user ...

I noticed that my jquery code is injecting extra white space into my HTML5 video

Ensuring my HTML5 background video stays centred, full-screen, and aligned properly has been made possible with this jQuery snippet. $(document).ready(function() { var $win = $(window), $video = $('#full-video'), $videoWrapper = $video. ...

What is the best method to use jQuery Validation Plugin to ensure users select "yes" from at least one of multiple select tags?

Using the jQuery Validation Plugin, I am currently implementing form validation in the following manner. Within the form, there are multiple select tags each with a default value of "Select" and two additional options: "Yes" and "No". These select tags are ...

Utilizing JavaScript to enable a Bootstrap 5 dropdown menu to open on hover for desktop users and be clickable for mobile users

I am currently using Bootstrap 5 to design a website and I'm facing an issue with creating a navbar dropdown. On desktop, I want the dropdown to open on hover and redirect the user to a new page when clicked. However, on mobile devices, I only want th ...

How can we integrate this icon/font plugin in CSS/JavaScript?

Check out the live demonstration on Jsfiddle http://jsfiddle.net/hc046u9u/ <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materializ ...

I'm not sure how to use the global .container CSS style in Next.js for multiple pages

I'm currently diving into the world of React and Next.js, tackling a project with Next.js. I've set up a global CSS file with a .container style definition which I imported in my _app.js file. However, I'm unsure how to apply this global sty ...

Cannot Display CSS Background Image Locally

Apologies for the simple inquiry, but I am facing an issue with displaying my background image using background-image:url() in the CSS. Strangely, it does not work with this method, but when I use content:url(); it works just fine. Interestingly, backgrou ...

``How can I easily navigate to the top of the page when changing routes in React Router DOM v6?

What is the best way to scroll to the top when a route changes in react router dom v6? In the past, I used a solution from this StackOverflow post to make my page automatically scroll to the top every time a route changed with react-router-dom v5. However ...

Styling text using SVG in HTML

I'm trying to underline a text element in SVG using the text-decoration attribute, but I'm running into an issue with the color of the line not changing. Here is the code snippet I am working with: <svg id="svg" viewBox="0 0 300 ...

Issue with the Animated Skill Bar not functioning properly when scrolling

I've been trying to implement an animated skill bar in my Portfolio using JQuery, but I'm facing some challenges. Despite following various tutorials, the code doesn't seem to work as expected. I've tried calculating section scroll posi ...

How can I modify the CSS styles for a custom jQuery widget?

I have been working on creating a custom widget with a textarea that has multiple rows. I am now looking to change the background color of this widget on the Onfocus and OnfocusOut events. Can anyone guide me on how to achieve this? <!doctype html> ...

Tips for positioning a label within the span element wpcf7-form-control-wrap

Is it possible to place the label for input elements inside the span element generated by the cf7 shortcode? I want to achieve this in order to make the label only visible when the input field is in focus. To accomplish this, both the label and the input ...

I need help figuring out how to mention an id using a concatenated variable in the jquery appendTo() method

Using jQuery, I am adding HTML code to a div. One part of this code involves referencing a div's ID by concatenating a variable from a loop. $(... + '<div class="recommendations filter" id="recCards-'+ i +'">' + &apo ...