Using CSS to apply hidden box shadow to nth child element

I am encountering a challenge with using the CSS selector :nth-child(...) in combination with the box-shadow effect. The intended outcome is as follows:

  • The even-numbered div elements within a specific container should have alternating background colors.
  • When a user hovers over one of these div elements, a box shadow is supposed to be applied, creating the illusion that the "hovered" div is floating above the next div.

However, I am facing an issue. While the box shadow does appear on the hovered element, it behaves differently for even-numbered divs compared to odd-numbered ones. Essentially, the shadow of each even div overlaps onto the succeeding odd div, whereas the shadow of each odd div is positioned behind the following even div.

You can see this problem clearly in action through this codepen link: http://codepen.io/jtlovetteiii/pen/cEaLK

Below is an excerpt of the HTML code snippet being used:

<div class="rows">
  <div class="row"></div>
  <div class="row"></div>
  ...
</div>

And here is the corresponding CSS style:

.rows
{
  background-color: #AAAAAA;
}

.rows .row:nth-child(even)
{
    background-color: #E2E2E2;
}

.row
{
  height: 20px;
  cursor: pointer;
}

.row:hover
{
  box-shadow: 0px 10px 10px #888888;
}

Can you spot what might be causing this issue?

Answer №1

The reason behind this occurrence is due to the fact that only the nth-child(even) divs within your structure are assigned a background color. Although it may seem like the hover shadow is covering other div elements, it is actually overlapping the background color of the parent.

You can resolve this issue by utilizing both position: relative and z-index properties:

.rows {
  position: relative;
}

.row
{
  position: relative;
  height: 20px;
  cursor: pointer;
  background-color: #CCCCCC;
}

.row:nth-child(even)
{
  background-color: #E2E2E2;
}

.row:hover
{
  box-shadow: 0px 10px 10px #888888;
  z-index: 100;
}

Check out the CodePen demo

Answer №2

Curious. Not exactly sure why this is occurring, but I've come up with a solution. By including a position: relative in the :hover elements, the hover effect becomes more uniform:

http://codepen.io/user123/pen/abcd

.sections
{
  background-color: #CCCCCC;
}

.sections .section:nth-child(even)
{
    background-color: #F0F0F0;
}

.section
{
  height: 30px;
  cursor: pointer;
}

.section:hover
{
  box-shadow: 5px 10px 15px #999999;
  position: relative;
}

Though it's not perfect yet, perhaps adjusting the margin offset could improve its appearance.

Answer №3

Example Link

.container
{
  width: 100%;
  display: flex;
  justify-content: center;
}

.box
{
  background-color: #f2f2f2;
  padding: 10px;
}

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

Selenium is unable to interact with hyperlink buttons in Python

I am currently working on a project where an automated program logs into an account and navigates through various webpages. My current struggle lies in the inability to click on any hyperlink buttons on a specific webpage to progress to the next page. I h ...

Transfer a GET parameter from the URL to a .js file, then pass it from the .js file to a

Hey there, I'm currently using a JavaScript file to populate my HTML page with dynamic data. The JS file makes a call to a PHP file, which then fetches information from my SQL database and echoes it as json_encode to populate the HTML page. The issue ...

Adjusting the zoom feature in CSS3

I'm looking to have my React app display as if the user has changed the zoom level to 90%. I attempted to tackle this issue using CSS. body { transform: scale(0.9); } However, this method did not result in the screen appearing similar to wh ...

Updating hyperlinks from dynamic php links to stable static links

I have a script that predominantly uses absolute addressing, but now I need to switch to relative addressing. The problem I'm encountering is when trying to change the links in PHP from: <link href="<?php print $theme; ?>style.css" type="tex ...

Having difficulty with PHP code, attempting to output some HTML while also opening a file and displaying its contents

I'm completely new to PHP and I've been experimenting with some code to display button elements that, when pressed, should open a file and write a number to it. Unfortunately, the code doesn't seem to be working as expected and I'm stru ...

What can I do to remove the hidden <p> tags that Wordpress is inserting into my HTML code?

There seems to be some extra space appearing between my divs (inside a main div) that is possibly due to unexpected <p> and </p> tags being added by WordPress. This issue is quite common with WP, but the problematic tags are hidden so I am unab ...

Ways to implement a 'Delete' feature in a PHP audio tag player

"I successfully retrieved the name of the uploaded file and am able to play it in my browser. How can I incorporate a delete function for each user-uploaded song?" echo "<hr />"; echo "<h4>" . $name . "<br /><a href='#' titl ...

Having trouble with the Cordova button not functioning on Android 9? A beginner seeks assistance

I attempted to create a button with the id of "clickme" that links to index.html and js/buttonexample.js. Although I could see the button on both the browser and android emulator, it wasn't functioning as expected when clicked. js/buttonexample.js d ...

Can you identify the issue within this code that combines HTML5, CSS, and JavaScript?

I'm attempting to create a feature where, upon clicking a button, a hidden div will be revealed. However, my current implementation doesn't seem to be working. function reload(){ window.reload(); } function vis(x,z,a){ var xpar = docu ...

Illustrate an element positioned beneath a child element within a different element

I am in the process of designing an overlay on a Google Map that resembles a real map placed on a table, using 2 pixel lines as creases above the map. This design does not significantly impact interactions with the map; only 6 out of 500 vertical lines are ...

Execute function upon changing the title of a list item using jQuery

Hey there, I've got a question for you. Is it possible to trigger an event when the title of a list element is changed? For example: <ul> <li id="li_1" title="40">40</li> </ul> So when the title attribute changes (coming ...

Deactivate additional fields when choosing an option from the drop-down selection menu

When designing a form with a select dropdown that displays various options, I encountered an issue. I want to disable certain fields if a specific option is chosen from the dropdown. For instance, if "Within Company" is selected in the transaction type, I ...

Data displayed in a table-like format

Is there a way to achieve this layout view image here (currently done using tables) while maintaining semantic markup? I believe the best tag for this would be dl. Each cell should have the height of its corresponding row. EDIT: The left column contains d ...

The Issue of Missing HTML Registration Form Data in Django

My configurations in demo\urls.py, user\urls.py, views.py, and HTML form seem to be set up correctly. However, upon pressing the submit button on the HTML form, I encounter a "File Not Found" error. Even after seeking assistance from ChatGPT, fol ...

What could be the reason for the absence of this Javascript function in my attribute?

I have been working on an app using electron, and I have a function that successfully adds tabs to the app. The issue arises when I try to add tabs via JavaScript with the onclick attribute - they show up as expected but do not execute the code to hide and ...

Combine React 17 with webpack 5 and babel-plugin-react-css-modules, enabling the use of CSS modules with stylename functionality

I am in the process of setting up a new React application using the latest technologies available, including React 17, Webpack 5, and other essential modules. My goal is to implement CSS modules utilizing the styleName concept with the help of babel-plugi ...

Stop the removal of the CSS content property

When a user enters and re-enters their password, I have a form that checks the strength of the password and displays text accordingly. However, I noticed that if a user makes a mistake and uses the backspace to re-enter the password, the text from data-tex ...

FAQs about utilizing percentage width and margins in CSS with Bootstrap

Currently, I am working on a mobile responsive web design using Twitter Bootstrap 3, and I am facing challenges with margins and resizing. I previously asked about creating a mobile-first responsive web design along with responsive images, which you can f ...

Ways to display URL parameters on an HTML page without using PHP

I'm currently working on a website using HTML (without PHP) and I'm facing an issue with displaying URL parameters on an appointment confirmation page. The appointment details are being successfully passed through the URL parameters, but I'm ...

"Trouble encountered while trying to display Angular in an HTML document

In my Angular project, there is a feature where a list of orders is displayed in one view. Each order is represented by its title only. When the user clicks on the title, they should be redirected to a new view showing the complete content of that particul ...