Cannot see the box-shadow with :before and z-index in CSS

My main objective is to prevent the box-shadow from overlapping with adjacent elements by utilizing the :before pseudo-element and adjusting the z-index.

However, I am facing an issue where the shadow appears beneath the container of the list item that is generating it due to the z-index property.

Interestingly, everything works perfectly if the parent container is just the body.

Should I look for a workaround for this problem or is it better to modify my HTML and CSS code?

HTML (PUG)

div#main
  ul
    li
    li
    li

CSS (Stylus)

#main
  background-color: lightyellow;
  height: 300px;
  width: 300px;

ul
  padding: 10px;

  li
    background-color: lightblue;
    height: 50px;
    width: 50px;
    margin: 10px;
    position: relative;

  &::before
    content: '';
    box-shadow: 0px 0px 15px 20px rgba(0,0,0,0.5);
    position: absolute;
    top: 0px;
    right: 0px;
    bottom: 0px;
    left: 0px;
    z-index: -1;

Answer №1

It seems like you are using absolutely positioned pseudo-elements without setting position: relative to its parent. This could be causing the issues you are experiencing. Additionally, make sure to set some basic CSS rules for better results:

Code Snippet

html,
body {
  margin: 0;
  padding: 0;
}
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
#main {
  background-color: lightyellow;
  height: 300px;
  width: 300px;
}
ul {
  padding: 10px;
  margin: 0;
  position: relative;
}
ul li {
  background-color: lightblue;
  height: 50px;
  width: 50px;
  margin: 10px;
  position: relative;
  overflow: hidden;
}
ul li::before {
  content: '';
  box-shadow: 0px 0px 15px 20px rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 0px;
  left: 0px;
  display: inline-block;
  width: 100%;
  height: 1px;
}
<div id="main">
  <ul>
    <li>List01</li>
    <li>List02</li>
    <li>List03</li>
  </ul>
</div>

Answer №2

I stumbled upon a solution, although I'm uncertain if it's the optimal approach

HTML (PUG)

ul
  li
    div.content
      span Everything
  li
    div.content
      span Created
  li
    div.content
      span Solely
  li
    div.content
      span NO LONGER!!!

CSS (STYLUS)

body
  margin 15px

ul
  display inline-block

li
  width 120px
  height 50px

.content
  background-color lightyellow
  border 1px solid rgba(0,0,0,0.85)
  widows 100%
  height 100%
  position relative

  &::after
    box-shadow 0px 0px 15px 5px rgba(0,0,0,0.5)
    position absolute
    top 0
    right 0
    left 0
    bottom 0
    content ''
    z-index -1

  span
    position absolute
    top 50%
    left 50%
    transform translate(-50%, -50%)

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

Create a fresh CSS class using the Chrome developer tool

Can a brand new CSS class be inserted in Chrome dev tools in this way? .myclass { background-color: yellow; } ...

What steps can I take to ensure that my collapsible menu is both dynamic and easily accessible

I have integrated an app with the footable plugin to enable responsive tables. This feature transforms table columns into a collapsible menu that dynamically appears as a new row when a user expands it by clicking a button, and disappears upon collapse. I ...

I would like to style the input checkbox using CSS

Is there a way to style input checkboxes with CSS that will work on all browsers (Chrome, Firefox, IE 6, 7, and 8)? If jQuery is the only solution, please let me know. It needs to be compatible with all browsers. Can anyone provide guidance on how to ach ...

When text exceeds multiple lines, display an ellipsis to indicate overflow and wrap only at word boundaries

Here is an example snippet of my code: <div class="container"> <div class="item n1">Proe Schugaienz</div> <div class="item n2">Proe Schugaienz</div> </div> and I am using the following jQuery code: $(&apos ...

What causes jQuery to not work correctly during keydown events?

I've been working on this external jQuery code: jQuery(document).one('keydown', 'g',function (evt){ if ($("#tb").html() == "0") { $("#tb").html("Testing the chicken.") } else {$("#tb").html("Chickens fart too." ...

When the width of a single table is large, Bootstrap tables can appear misaligned and not properly

I am facing an issue with displaying two tables side by side. Whenever the width of the first table is too large, the second table is pushed below it. https://i.sstatic.net/w8zwp.png https://i.sstatic.net/SZdZU.png Is there a way to make them stay side b ...

How can I create a dimming effect on a webpage while a Details View Control is being displayed?

I have come across websites that use a technique where when a message box pops up, the rest of the webpage dimmed to emphasize the message. Currently, I am working on building a website using Microsoft Web Developer 2010, specifically an ASP.net site. On ...

Incorporating components into Vue while utilizing Flask poses a challenge

Currently, I am working on a project that involves Flask and Vue. I am attempting to add a carousel to my website, but I am running into difficulties with creating and importing components for Vue when following tutorials. Here is my current file structure ...

Transform your current website layout from a fixed 960-grid system with 12 columns into a fluid and

As a newcomer to web development, I have successfully created my portfolio website. I initially set the body width to 1600px because that matched the size of my banner background in Photoshop. I'm using a 960gs 12-column grid system, but when I view t ...

Generate a variety of HTML ordered lists

Can anyone assist me in determining the type of ordered list shown below? What is the method to achieve an ordered list like this: (a) (b) (c) in HTML? ...

The issue of duplicate backgrounds appearing on the burger menu when adjusting the height

There seems to be an issue with my burgermenu; when I attempt to adjust the height, the background-color ends up duplicating. .bm-menu { background-color: rgba(255, 255, 255, 0.9); height: 60vh; position: fixed; transition: top 0.3s; to ...

Guide to implementing a slider in HTML to adjust the size of my canvas brush

Hey there, I'm looking to implement a slider functionality under my canvas that would allow users to change the brush size. Unfortunately, all my attempts so far have been unsuccessful. Can anyone lend a hand? Much appreciated! <canvas i ...

The Spotify List items generated from stored localStorage information are showing as empty

I'm currently developing an app for Spotify and I am facing some challenges while trying to create a views.List object using data stored in our database. Initially, I make a POST request to retrieve the required information and store it in local stora ...

The loop in MVC is not functioning properly when dealing with various MapRoutes

I am new to MVC and have a good understanding of C#. While experimenting with MVC in Visual Studio, I encountered an issue that is puzzling me. I am struggling to identify the cause of this error. My goal is to print a variable n number of times. It works ...

Aligning a bootstrap button group at the center of a container

Looking for suggestions on how to center a Bootstrap button group (btn-group) within an element? For instance, consider the following structure: <div id='toolbar'> <div class="btn-group"> <button type="button" class="b ...

using a CSS tag to vertically center text within an element

After using a tag and setting the background color, height, and width for my content, I decided to align the text in the center along the x-axis using text-align: center;. However, I am now trying to figure out how to align it in the middle along the y-ax ...

Tables that adapt to different screen sizes, displaying perfectly on desktop but with slight variations on mobile devices

I recently experimented with a demo to enhance the performance of tables on my website in development. The demo worked flawlessly on my desktop when I resized using a responsive tester extension for Chrome: https://i.stack.imgur.com/KzFiR.jpg However, u ...

Exploring the integration of foreign languages within C# code?

In search of a C# expert who can assist me with a question I've been struggling to find an answer for all day. Google searches have yielded outdated information, and I need fresh insight. Wondering if there's a way to incorporate CSS and HTML in ...

Ways to conceal components upon clicking a different element

Struggling to make this jQuery function properly. I have a form with all fields contained in a div.form-group. The subscribe button is identified by the id subscribe. I'm aiming to hide the form fields when clicked, but my JavaScript doesn't see ...

Having trouble scrolling to the top in Flickr Search using AngularJS, the results are restricting my movement

I recently followed a tutorial on creating a Flickr Search App with AngularJS (https://www.youtube.com/watch?v=lvGAgul5QT4). I managed to show the search results on my page, but encountered an issue: I couldn't scroll all the way back up to the search ...