简体   繁体   English

盒子阴影在儿童元素?

[英]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: 我需要让我的盒子阴影看起来像一个边框:如果我有一个带有插入框阴影的父母并且我在其中放置了一个子div,则框阴影应该越过子div,如此处所示带边框:

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

Is there anything like az index for this problem, or a css hack? 这个问题有什么像az索引,还是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. 可能的hack是在子div上左右添加框阴影。

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/ 如果你想要一个不需要任何额外标记的解决方案,你可以使用“:before”伪类选择器: http//jsfiddle.net/7rRsw/8/

HTML HTML

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

CSS 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;
}

It is because your box shadow is inset. 这是因为你的盒子阴影是插入的。 Meaning it will appear inside the box. 这意味着它将出现在框内。
Whilst your nested div will cover it. 虽然您的嵌套div将覆盖它。 Using a border applies to the outside of the "box". 使用边框适用于“框”的外部。
Removing the inset from your CSS will cause the effect you are after. 从CSS中删除inset将导致您所感受到的效果。
See updated fiddle with inset remove. 查看更新的小提琴插入删除。 fiddle 小提琴

box-shadow: 0px 0px 0px 2px black;
-webkit-box-shadow: 0px 0px 0px 2px black;
-moz-box-shadow: 0px 0px 0px 2px black;

UPDATE UPDATE
To have just the inset box shadow visible, you could make the child div 4px pixels smaller in width than the parent. 要使插入框阴影可见,您可以使子div的宽度小于父级的4px像素。 Then use margins to correctly position the div. 然后使用边距来正确定位div。 However I'm not sure this completely achieves what you are after? 但是我不确定这会完全实现你的目标吗? See this fiddle . 看到这个小提琴

.a{
    width: 200px;
    float: left;
    height: 200px;
    margin-right: 100px;
    background-color: red;
    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: 196px;
    height: 100px;
    background-color: yellow;
    margin:2px auto 0 auto;
}

UPDATE 2 更新2
This "Hack" applies an overlay to the two elements with the box shadow. 这个“Hack”使用框阴影将叠加应用于两个元素。 See fiddle . 小提琴

HTML HTML

<div class="a">
    <div class="b">How it is (Yellow div covers the box shadow)</div>
    <div class="shadow">&nbsp;</div>
</div>

CSS CSS

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

.b {
    width: 200px;
    height: 100px;
    background-color: yellow;
}

.shadow {
    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;
    position:absolute;
    top:0;
    width:200px;
    height:200px;
}

One way is to not make your box-shadow inset so that it appears outside the box. 一种方法是不要让你的盒子阴影inset ,使它出现在盒子外面。 But if you really want to use an inset box shadow, you can add a padding to the container element equal to the thickness of the shadow: 但是如果你真的想使用inset框阴影,你可以在容器元素中添加一个等于阴影厚度的填充:

.a {
  ...
  padding: 2px;
  box-shadow: 0 0 0 2px #000000;
}

Add: 加:

z-index: -1; /** less than the parent in general */

To the child element and it should work. 对于子元素 ,它应该工作。

Give your main element position: relative , then create another div within that element that has: 给你的主元素position: relative ,然后在该元素中创建另一个具有以下内容的div:

position: absolute;
width: 100%;
height: 100%;   
box-shadow: inset 0px 0px 0px 2px black;

What this does is it creates an invisible div that goes over your content, then you apply your box-shadow to that div and it will lay on top of all the elements that were previously covering the shadow. 这样做是因为它会创建一个覆盖你内容的不可见的div,然后你将你的盒子阴影应用到那个div,它将放在之前覆盖阴影的所有元素之上。 It's like placing a sheet of glass with the shadow etched on to its edges over your element. 这就像放置一块玻璃,阴影蚀刻在元素的边缘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM