Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

html - Prevent box shadow of element flowing out of its parent div

I want the box shadow of a child element only overflow within it's parent element but it goes out of its parent element. How can I fix this?

How it is actually: image with box shadow overflow out of its parent

How it is expected to be: image with box shadow only within it's parent

#main {
  background: #ddd;
  height: 200px;
  width: 200px;
  padding: 50px;
}

#child {
  background: #eee;
  height: 100px;
  width: 100px;
  padding: 50px;
}

#grand-child {
  background: #fff;
  height: 100px;
  width: 100px;
  box-shadow: 0 0 100px 40px red;
}
<div id="main">
  <div id="child">
    <div id="grand-child"></div>
  </div>
</div>
question from:https://stackoverflow.com/questions/65897365/prevent-box-shadow-of-element-flowing-out-of-its-parent-div

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You simply add overflow: hidden

#main {
  background: #ddd;
  height: 200px;
  width: 200px;
  padding: 50px;
  overflow: hidden;
}

#child {
  background: #eee;
  height: 100px;
  width: 100px;
  padding: 50px;
  overflow: hidden;
}

#grand-child {
  background: #fff;
  height: 100px;
  width: 100px;
  box-shadow: 0 0 100px 40px red;
}
<div id="main">
  <div id="child">
    <div id="grand-child"></div>
  </div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...