Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

box shadow overflow parent

Tags:

html

css

I'm creating a sidebar block with an inset shadow. I want the shadow to appear on top of the .block-title element, but the shadow is rendered under it.

You can see the problem here: JSFiddle

This is my css:

.block{
  width:250px;
  -webkit-box-shadow: inset 7px 0 10px -5px rgba(0,0,0,0.3);
  -moz-box-shadow: inset 7px 0 10px -5px rgba(0,0,0,0.3);
  box-shadow: inset 7px 0 10px -5px rgba(0,0,0,0.3);
  border:1px solid gray;
}

.block .block-title{
  background:#dedede;
  padding:5px;
}

.block .block-title span{
    color:#333;
  font-size:14px;
  text-align:center;
}

.block .block-content{
  padding:10px;
}
like image 850
eatmailyo Avatar asked Apr 17 '26 19:04

eatmailyo


2 Answers

You can set the box shadow on top of the children using an absolutely positioned pseudo element (::before). Use poiner-events: none on the pseudo element to allow interaction with the elements under it.

body {
  margin: 10px;
}

.block {
  position: relative;
  width: 250px;
  border: 1px solid gray;
}

.block::before {
  display: block;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  box-shadow: inset 7px 0 10px -5px rgba(0, 0, 0, 0.3);
  pointer-events: none;
  content: '';
}

.block .block-title {
  background: #dedede;
  padding: 5px;
}

.block .block-title span {
  color: #333;
  font-size: 14px;
  text-align: center;
}

.block .block-content {
  padding: 10px;
}
<div class="block">
  <div class="block-title">
    <span>Lorem ipsum dolor</span>
  </div>
  <div class="block-content">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam efficitur elit quis auctor sollicitudin. Aenean est lorem, ornare in laoreet sed, lobortis ac libero. Phasellus dignissim imperdiet varius.
  </div>
</div>
like image 108
Ori Drori Avatar answered Apr 19 '26 09:04

Ori Drori


What is happening here is that .block-title is covering the shadow effect. You could make the effect appear a bit more if you added opacity, or gave the color an rgba-value.

I changed the hexidecimal color to rgba in my example.

JS-Fiddle: https://jsfiddle.net/4fkfdxn6/4/

html

<div class="block">
  <div class="block-title">
    <span>Lorem ipsum dolor</span>
  </div>
  <div class="block-content">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam efficitur elit quis auctor sollicitudin. Aenean est lorem, ornare in laoreet sed, lobortis ac libero. Phasellus dignissim imperdiet varius.
  </div>
</div>

css

body{
  margin:10px;
}

.block{
  width:250px;
  -webkit-box-shadow: inset 7px 0 10px -5px rgba(0,0,0,0.3);
  -moz-box-shadow: inset 7px 0 10px -5px rgba(0,0,0,0.3);
  box-shadow: inset 7px 0 10px -5px rgba(0,0,0,0.3);
  border:1px solid gray;
}

.block .block-title{
  background:rgba(0, 0, 0, 0.4); /*changed this value*/
  padding:5px;
}

.block .block-title span{
  color:#333;
  font-size:14px;
  text-align:center;
}

.block .block-content{
  padding:10px;
}
like image 31
Rob Monhemius Avatar answered Apr 19 '26 10:04

Rob Monhemius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!