添加图片后Div高度无法铺满屏幕的技术问题
Fixing Overflowing Images & Container Height Issues
Hey there! I see the problem you're facing—your second image is spilling outside the red container, and the container isn't expanding to fit its content. Let's break down why this happens and fix it step by step.
What's Causing the Issue?
The root problem boils down to two key choices in your CSS:
- Your
.First-Halfand.wrapperelements haveheight: 100%set, locking them to the viewport height instead of letting them grow with their content. - Both images use
position: absolute, which takes them out of the normal document flow. This means the container doesn't "register" their height, so it won't expand to fit them.
The Solution
Here's how to adjust your code to make the container fit all content while keeping it anchored to the left half of the screen:
Updated HTML (no changes needed here, included for reference)
<section className="App"> <div className="First-Half"> <div className="wrapper"> <div className="Lacazette-Image"> <img alt="Lacazette" src={Lacazette}></img> </div> <div className="Arsenal-Fans"> Arsenal Fans </div> <div className="Celebrate-Image"> <img alt="Celebrations" src={Celebrations}></img> </div> </div> </div> </section>
Updated CSS
@import url('https://fonts.googleapis.com/css?family=Kanit|Lobster|Mansalva&display=swap'); .App { font-family: 'Lobster', sans-serif; text-align: center; /* Removed display: table since it's unnecessary here */ } .First-Half { background-color: red; /* Replaced position: absolute with float: left to keep it left-aligned while allowing height to grow */ float: left; width: 50%; /* Removed height: 100% so the container adapts to content */ } .wrapper { width: 100%; position: relative; /* Kept this to maintain absolute positioning for the text */ } .Arsenal-Fans { position: absolute; height: 50px; left: 74%; top: 58%; font-family: 'Lobster', sans-serif; font-size: 55px; font-weight: normal; font-style: normal; font-stretch: normal; line-height: 1.22; letter-spacing: normal; text-align: left; color: rgba(249, 246, 246, 0.97); z-index: 10; /* Added to ensure text stays visible above images */ } .Lacazette-Image img{ /* Removed position: absolute to put the image back in the document flow */ width: 100%; opacity: 0.8; border: solid 1px #707070; } .Celebrate-Image img{ /* Removed position: absolute and top/left to keep it in the flow */ width: 100%; opacity: 0.8; border: solid 1px #707070; }
Key Changes Explained:
- Removed fixed heights: By ditching
height: 100%from.First-Halfand.wrapper, the container now expands vertically to fit all its content. - Swapped absolute positioning for float: Using
float: lefton.First-Halfkeeps it anchored to the left 50% of the screen while letting its height adjust naturally. - Restored images to normal flow: Taking
position: absoluteoff the images makes the container recognize their height, so it wraps around them properly. - Added z-index to text: If you want the "Arsenal Fans" text to sit over the images, keeping
position: absoluteand addingz-index:10ensures it stays visible above the images.
If you need the images to overlap each other specifically, we can tweak the approach—just let me know! But for the issue you described, this should fix the overflow and container height problem.
内容的提问来源于stack exchange,提问作者Mahdi Jaber
相关产品推荐
相关产品推荐

