热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

用于重叠式div的CSS3box-shadow-CSS3box-shadowforoverlapping-likedivs

Imtryingtoachievethiseffectwithcss3:我试图用css3实现这个效果:TheHTMLcodeiscleanlysomethingl

I'm trying to achieve this effect with css3:

我试图用css3实现这个效果:

header/main

The HTML code is cleanly something like

HTML代码很简单

...

    ...


...
...

and the css is, for the moment, something like

而目前,css就是这样的

header {
    display: block;
    width: 900px;
    height: 230px;
    margin: 0 auto;
    border: 1px solid #211C18;
    ...
    box-shadow: 2px 4px 20px #005377;
    -moz-box-shadow: 2px 4px 20px #005377;
    -webkit-box-shadow: 2px 4px 20px #005377;
}

#wrapper {
    width: 820px;    
    margin: 0 auto;
    ...
    border-right: 1px solid #211C18;
    border-bottom: 1px solid #211C18;
    border-left: 1px solid #211C18;
    ...
    box-shadow: 2px 4px 20px #005377;
    -moz-box-shadow: 2px 4px 20px #005377;
    -webkit-box-shadow: 2px 4px 20px #005377;
}

In order to obtain the desired result, I need to:

为了获得理想的结果,我需要:

  1. Hide the top shadow of the main div (no problem with that)
  2. 隐藏主div的顶部阴影(没问题)
  3. Bring header's bottom shadow in front of the main div, and not behind as it is right now.
  4. 将标题的底部阴影置于主div的前面,而不是现在的背后。

I've been reading a lot about box-shadow, and I haven't found a solution that really pleases me... Any idea?

我一直在阅读很多关于盒子影子的内容,而且我还没有找到一个让我高兴的解决方案......任何想法?

3 个解决方案

#1


15  

This jsfiddle does what you want.

这个jsfiddle做你想要的。

The way it works is with a main #wrap element that centres the content and creates a coordinate map for the absolutely positioned #main element. It does this because of its position: relative CSS rule. You end up with the following markup:

它的工作方式是使用一个主要的#wrap元素使内容居中,并为绝对定位的#main元素创建一个坐标图。这是因为它的位置:相对CSS规则。您最终得到以下标记:

The header is then placed inside of this and given position: relative and a z-index to set it stacking above the #main container.

然后将标题放在此位置和给定位置:relative和z-index以将其设置在#main容器上方。

Finally #main is absolutely positioned below the header. The CSS looks like so:

最后#main绝对位于标题下方。 CSS看起来像这样:

/* centre content and create coordinate map for absolutely positioned #main */
#wrap {
    width: 300px;
    margin: 20px auto;
    position: relative;
}

header, #main {
    background: #fff;

    /* rounded corners */
    border: 1px solid #211C18;
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;    

    /* dropshadows */
    box-shadow: 2px 4px 20px #005377;
    -moz-box-shadow: 2px 4px 20px #005377;
    -webkit-box-shadow: 2px 4px 20px #005377;
}

header {
    display: block;
    width: 300px;
    height: 50px;

    /* stack above the #main container */
    position: relative;
    z-index: 2;
}

#main {
    width: 200px;
    height: 70px;

    /* stack below the header and underlap it...if that's even a word */
    position: absolute;
    z-index: 1;
    top: 40px;
    left: 50px;
}

#2


3  

I just had some luck using relative positioning.

我只是运气相对定位。

For example.

例如。

1st div has a box-shadow, and a bottom margin. 2nd div slides up under the shadow.

第一个div有一个盒子阴影和一个底部边距。第二个div在阴影下滑动。

#firstdiv {width: 960px; box-shadow: 5px 5px 5px #ccc; margin-bottom: 10px;}
#seconddiv {width: 960px; position: relative; top: -10px;}

It's not the best solution but it works for me.

这不是最好的解决方案,但它对我有用。

#3


2  

setting position to "relative" will already solve the problem!

将位置设置为“相对”已经解决了问题!


推荐阅读
author-avatar
fdsfdsfsfsfsfsfsfsfsafsf
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有