Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 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 Reply

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...