Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Box-Shadow over child elements?

I need to make my box-shadow appear like a border: If I have a parent with a inset boxshadow and I put a child div in it, the box shadow should go over the child div like shown here with borders:

jsFiddle: http://jsfiddle.net/7rRsw/2/

Is there anything like a z index for this problem, or a css hack?

Thanks

EDIT: I need to use box shadow inset + no borders or box seizings. I am searching for hacks to make this possible only with box shadow. A possible hack would be to add box shadows left and right on the child div.

like image 857
Plic Pl Avatar asked Jan 18 '14 19:01

Plic Pl


People also ask

Which property applies shadow to elements?

The CSS box-shadow property is used to apply one or more shadows to an element.

Can we apply transform property to box-shadow?

Using transforms on the box-shadow (& transform ) property, we can create the illusion of an element moving closer or further away from the user.

How can you add a shadow to your element?

In CSS, shadows on the boxes of elements are created using the box-shadow property (if you want to add a shadow to the text itself, you need text-shadow ). The box-shadow property takes a number of values: The offset on the x-axis. The offset on the y-axis.

Which box-shadow property controls the size of the shadow?

The blur radius (optional), if set to 0 the shadow will be sharp, the higher the number, the more blurred it will be. The spread radius (optional), positive values increase the size of the shadow, negative values decrease the size.


1 Answers

If you want a solution in which you don't need any extra markup, you can use the ":before" pseudo class selector: http://jsfiddle.net/7rRsw/8/

HTML

<div class="a"><div class="b">No extra markup needed</div></div>

CSS

.a {
    width: 200px;
    float: left;
    height: 200px;
    margin-right: 100px;
    background-color: red;
    position: relative;
}

.a:before {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    width: 200px;
    height: 200px;
    box-shadow: inset 0px 0px 0px 2px black;
    -webkit-box-shadow: inset 0px 0px 0px 2px black;
    -moz-box-shadow: inset 0px 0px 0px 2px black;
}

.b {
    width: 200px;
    height: 100px;
    background-color: yellow;
}
like image 178
TriangleJuice Avatar answered Nov 09 '22 23:11

TriangleJuice