The CSS background shadow on one div behaves differently compared to the neighboring div

Currently, I am facing an issue where I have two divs positioned next to each other in a line.

http://jsfiddle.net/A5Jc7/60/

The HTML structure is as follows:

<div>
    <div class="box1"></div>
    <div class="box2"></div>
</div>

The CSS for the divs is listed below:

.box1 {
    width:200px;
    min-height:200px;
    background:blue;
    float:left;
    margin:20px 0 0 0;
}
.box1:hover {
    background: white;
    box-shadow: 0px 0px 5px 5px #888888;
    height:200px;
    margin:20px 0 0 0;
}
.box2 {
    width:200px;
    min-height:200px;
    background:blue;
    float:left;
    margin:20px 0 0 0;
}
.box2:hover {
    background: white;
    box-shadow: 0px 0px 5px 5px #888888;
    height:200px;
    margin:20px 0 0 0;
}

The problem arises when hovering over the left div, the shadow appears under the right div. Conversely, hovering over the right div causes the shadow to appear over the left div. I've tried using z-index adjustments without success. Any suggestions on how to make both shadows consistent?

Answer №1

Z-index will only be effective for elements that are either absolutely or relatively positioned.

To ensure proper functionality, make sure to include the following in each box:

position:relative;

Additionally, add the following for hover effects:

z-index:1;

If you want shadows to appear behind the parent element of an object, simply apply:

z-index:-1;

For a practical example, visit: http://jsfiddle.net/charlescarver/A5Jc7/64/

This method eliminates the need for using unusual z-index values or absolute positioning, which can lead to additional CSS complexity for styling purposes.

Answer №2

Due to the coverage of your shadow being offset by the first block,

If you adjust the x position in the second box shadow to something like

box-shadow: 5px 0px 5px 5px #888888;
, that should resolve the issue.

However, if you wish to maintain symmetry, you will also need to modify the offset of the first shadow using

box-shadow: -5px 0px 5px 5px #888888;

Answer №3

Utilizing absolute positioning is key for proper z-index functionality.

.container-1 {
position:relative;
width:400px;
height:200px; 
}

.element-1{
width:200px;
min-height:200px;
background:black;
position:absolute;
margin:20px 0 0 0;
z-index: 1;
left:0;
 top:0       
}

.element-1:hover{
background: white;
box-shadow: 0px 0px 5px 5px #888888;    
height:200px;
margin:20px 0 0 0;
    z-index:5
}

.element-2{
width:200px;
min-height:200px;
background:black;
position:absolute;
margin:20px 0 0 0;
z-index:1
z-index: 1;
right:0;
 top:0           
}

.element-2:hover{
background: white;
box-shadow: 0px 0px 5px 5px #888888;  
height:200px;
margin:20px 0 0 0;
    z-index:5
}

Answer №4

To achieve the desired effect, apply z-index: -1 to .box1 and z-index: -11 to .box2:hover.

Additionally, make sure to use position:relative for both boxes in order to properly utilize the z-index property.

Your code should look something like this:


.box1 {
width:200px;
min-height:200px;
background:black;
float:left;
margin:20px 0 0 0;   
width: 200px;
z-index: -1;
position: relative;
}

.box2:hover {
background: white;
box-shadow: 0px 0px 5px 5px #888888;  
height:200px;
margin:20px 0 0 0;
z-index: -11;
position: relative;
}

Check out the Fiddle demonstration for a visual representation: http://jsfiddle.net/A5Jc7/63/

The concept behind this setup is to ensure that the shadow of box2 appears below box1 when it is being hovered over.

Answer №5

Visit this link for the code

.box1{
    width:200px;
    min-height:200px;
    background:black;
    float:left;
    margin:20px 0 0 0;
    z-index: 1; 
    position: relative;
}

.box1:hover{
    background: white;
    box-shadow: 0px 0px 5px 5px #888888;    
    height:200px;
    margin:20px 0 0 0;
    z-index:2;
}

Adjusted positioning and z-index properties on hover effect.

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

When the height of the window decreases, scrolling is not implemented

Having trouble adjusting the height of my Responsive Modal when the window height decreases. The content is slipping under the window and the scroll bar isn't adjusting accordingly. It seems like the nested div structure is causing issues. https://i.s ...

Is there a way to rotate a div without utilizing the "transform" CSS property?

I am currently utilizing a plugin from the SVG library to render graphics within a div (). However, I am encountering an issue when attempting to rotate it using the "transform" property. The rotation is purely visual and does not alter the X and Y axes of ...

Animating an image inside a bordered div

I'm attempting to create an animated effect where an image moves randomly within the boundaries of a div container. While I've come across solutions for animating within borders, none have specifically addressed keeping the image inside the div. ...

Using jQuery's sortable functionality to rearrange rows in a table can cause conflicts with Angular's orderBy feature

In the past, my angular app used a table with orderBy to sort rows based on a specific column. By clicking on a table header, the orderBy arguments would change and the list would be sorted according to the values in that column. Now, I am experimenting w ...

Send the selected option from the dropdown menu to a different page

I am in the process of designing a unique shopping cart system where, upon clicking on the "add to cart" link, both the selected quantity from the drop-down menu and the product ID are transmitted to the addCart page. While I have successfully managed to p ...

What causes the presence of a boundary around the svg?

I have been encountering an issue while attempting to incorporate my logo into the header of my webpage. Specifically, when viewed on smaller screens, I noticed that there is a margin surrounding the SVG image. Below is my HTML file: <html lang="e ...

Creating a JavaScript-powered multi-department form: A step-by-step guide

Is there a way to maintain the form displayed on a webpage created in HTML5 and JavaScript for logging calls across three different departments? With buttons assigned to each department, clicking on them reveals the respective HTML form. That said, every t ...

Allowing SVGs to be clickable without disrupting the functionality of YouTube videos

I'm experiencing an issue with the overlapping of the svg and YouTube elements. If you hover your mouse over, just to the right of the svg and click, instead of opening the video, it redirects you back to the previous page. Clicking on the area betw ...

The optimal method for designing a select menu to ensure it works smoothly on various web browsers

Recently, I encountered an issue with customizing a select menu using CSS and jQuery. After some work, I was able to achieve a result that I am quite pleased with: So far, the styling works perfectly in Mozilla, Opera, Chrome, and IE7+. Below is the curr ...

Arranging column contents neatly on small screens

Although there are similar answers available, I want to address those who are trying to prevent their columns from stacking on XS display. Despite having two columns, the content is still stacking at certain displays. This issue is occurring while using B ...

Align elements on the left side with some space between them

Having trouble displaying a list of images inline within a div. When I float them left, they leave a lot of space and do not display properly. Can anyone help me with this issue? See screenshot below: Here is my html code: <div class="col75"> & ...

Tips on aligning an entire text in R Markdown using the bookdown::gitbook function as the desired output

I've been looking for a way to justify the text in my gitbook, but haven't had any luck finding a solution. I've attempted: Adding "text-align: justified" to the CSS right after the YAML Header: <style> body { text-align: justify ...

Finding your site's First Contentful Paint (FCP) can be done by analyzing the

After doing some research on Google insights, I came across information about FCP. However, I'm still unsure about how to determine my site's FCP and other relevant metrics. If anyone could provide me with more information or share detailed link ...

There is a slight misalignment when trying to horizontally center a character within a div

I've experimented with various methods like using a px width or em widths. I've attempted nesting a span and trying different styles such as text-align:center or margin:0 auto. I can manage to center either the R or the S, but struggling to get ...

The bottom portion of my page vanishes without a

My footer has the following CSS class: .footer{ border-top:solid 2px #BBB; background : url('../images/footer.png'); font-family : helvetica; font-style : italic; height:9em; clear: both; color: black; wid ...

Show angular variable data in a table based on their index

I have an array of angular variables with values like [ 170, 1, 130, 3, 134, 4.... and so on]. I would like to present these values in a table format where the even positioned values are shown in one column and the odd positioned values in another column. ...

I would like for my element to increase and decrease in size while rotating when the button is clicked

I am trying to create an element that changes its size while spinning when a button is clicked. It seems to be working fine on hover, but not onclick. Here is the code I have: <div class="rec">Rec</div> <button id="button&quo ...

Capture line breaks from textarea in a JavaScript variable with the use of PHP

I need help with handling line breaks in text content from a textarea. Currently, I am using PHP to assign the textarea content to a Javascript variable like this: var textareaContent = '<?php echo trim( $_POST['textarea'] ) ?>'; ...

As the user scrolls down, the navigation becomes obscured by the content

Whenever I scroll down on my website at Bradly Spicer, the navigation menu extends and ends up falling behind the content. This is the script I am using: .menu { position: relative; z-index:9999; } .post { background: #fff; border: 1px solid #ddd; font- ...

Encountering a TypeError with DataTables and Tabledit

I've been attempting to integrate DataTables with Tabledit, but I keep encountering the error message "TypeError: Cannot set properties of undefined (setting 'nTf')". The number of tags also matches up. Interestingly, if I comment out the " ...