Parent inset box-shadow overlapped by children divs

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

I am having an issue where the children divs are overlapping the parents box-shadow bottom.

The parent has a max-height w/ overflow-y: scroll. Any help would be great!

http://i.stack.imgur.com/jQe0r.png

HTML:

<div class="capture sh-btm">   <div class="threads">     <div class="thread"></div>     <div class="thread"></div>     <div class="thread"></div>     <div class="thread"></div>   </div> </div>

CSS:

.capture {   width: 100%;   height: 100%;   overflow-y: scroll;   overflow-x: hidden;   position: relative; }  .threads {   height: 250px;   width: 100%;   display: inline-block;   padding-bottom: 10px;   position: relative;   clear: left; }  .thread {   width: 248px;   float: left;   margin-right: 8px;   margin-top: 6px;   border-bottom: 2px solid #dadada;   overflow: hidden;   zoom: 1; }  .sh-btm {   box-shadow: inset 0px -5px 9px -2px #000; }

回答1:

No, what you're asking can be done quite easily. However, for ease I would advise for using a background-image or a CSS gradient instead of inset box-shadows. You'll have to tweak the CSS a bit to get exactly what you wanted. (for example to make sure the bottom overlay doesn't cover the bottom arrow of your scrollbar).

Setting a z-index on the child elements will only work if you have nothing more than static text and images to show, and no links or interactive content. You're also not allowed to set a background for the parent, or it will hide the children.

To achieve this, you need to make 2 separate shadow overlay divs, and position them absolutely in a parent container. Your structure will be like this:

  • Parent container
    • Shadow overlay left
    • Shadow overlay bottom
    • Threadcontainer (overflow is set on this div)
      • Thread
      • Thread

Here is a working demo: http://jsbin.com/avafaq/1/edit

<div class="capture sh-btm">   <div id="shadow_overlay_left"></div>   <div id="shadow_overlay_bottom"></div>   <div class="threads">     <div class="thread"></div>     <div class="thread"></div>   </div> </div>
#shadow_overlay_left{   width: 10px;   height: 100%;   position: absolute;   z-index: 5;   box-shadow: inset 3px -2px 5px 0px #000; } #shadow_overlay_bottom{   width: 100%;   min-height: 10px;   position: absolute;   bottom: 0%;   z-index: 5;   box-shadow: inset 0px -5px 9px 0px #000; } .threads {   overflow-y: scroll;   overflow-x: hidden; }

Notice I put the overflow properties on the .threads container instead of the parent container. This is because else your absolutely positioned divs will scroll too, and will not fill their respective widths/ heights.

Again, you can apply box-shadow, a background-image or CSS gradients to your shadow overlay divs.



回答2:

The problem is that the child elements are higher in the stacking context that the parent. To make the shadow show through, you need to make them lower than the parent.

First of all you need to position the element to make z-index apply, then reduce the z-index to a value lower than the parent. Eg.

.thread {     position: relative;     z-index: -1; }

http://jsfiddle.net/7PhfJ/

Or as .threads is already positioned, and the box-shadow is on its parent, you can add the z-index directly on that element:

.threads {     z-index: -1; }

http://jsfiddle.net/7PhfJ/1/



回答3:

You can do it with positioning and box-shadow alone, but browser support would be poor. I used position: sticky (no Chrome support), but a fun experiment anyway.

<div class="wrap"> <!-- We set the overflow on the parent -->     <div class="shadow"></div>     <!-- Create a new container as a layer over the rest of the content -->     <div class="content">         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>     </div> </div>

Use position: sticky instead of absolute, .shadow will stay at the top of its parent as long as the parent is visible, so we set it up with the full height of the parent and offset the content with a negative margin to align it with the top of the parent. Sticky doesn't scroll down with the content like an absolute positioned element would.

You can now set the inset box-shadow to any value and use pointer-events: none to allow interaction with the layer behind the layer with the box-shadow (because positioned elements with a higher z-index will prevent you from interacting with elements behind them).

.wrap{   border: 1px solid #dadada;   /* You'll need a fixed height - assuming that's the case already, given the overflow */   height: 400px;   margin: 5vh auto;   overflow-y: auto;   width: 50vw; } .content{   margin-top: -400px; } .shadow{   box-shadow: 10px -10px 10px rgba(0, 0, 0, 0.3) inset;   height: 100%;   /* To avoid objects below not being accessible we use pointer events - CSS4 and wonky IE support again */   pointer-events: none;   position: sticky;   /* Firefox doesn't really need the vendor prefix and Chrome doesn't support this */   position: -webkit-sticky;   position: -moz-sticky;   position: -ms-sticky;   position: -o-sticky;   top: 0;   width: 100%; } .item{   /* You would obviously add your own styling - just making boxes that show the concept */   background: white;   border: 1px solid #dadada;   box-sizing: border-box;   float: left;   height: 250px;   margin: 1%;   width: 23%; } .item:hover{   /* To demonstrate the click through the top layer and :hover works */   background: #f3f3f3; }

Again - this is experimental and browser support is lacking in places, but proves that you can do it with CSS only.

Fiddle here ...



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!