滚动视口时,底部或右侧的背景图像被剪切掉 [英] Background image is cut off at bottom or right when scrolling viewport
问题描述
我看到了很多类似的问题,但无法找到解决方案。
打开
I saw quite a few similar questions but could not find a fix.
Open this sample and resize the browser to make its height shorter
than the main div height, ~400 pixels.
When scrolling down, the background image attached to the body is cut off:
代码:
html { height: 100%; color: white; }
body { height:100%; padding: 0; margin: 0; background:url(bg.jpg) repeat-x; background-position: bottom; background-size: contain; }
/*#pageWrap { background:url(bg.jpg) repeat-x;}*/
#page { height:100%; }
#divHeader { width:100%; height:115px; }
#divMain { width:600px; height:400px; border: solid 1px brown; }
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="pageWrap">
<div id="page">
<div id="divHeader">Header</div>
<div id="divMain">Main</div>
<div id="divFooter"><p>All Rights Reserved. Blabla® 2015</p></div>
</div>
</div>
</body>
</html>
我试图将背景图像移动到pageWrap div作为有人建议。
它解决了垂直滚动问题,但在水平方向创建一个类似的问题:
当窗口是
I tried to move the background image to the pageWrap div as someone suggested.
It solves the vertical scroll problem, but creates a similar problem horizontally:
when the window is too narrow and you scroll left, the image is cut off on the right.
任何真正的解决方案?
推荐答案
因为重复一个渐变看起来不是很好,我想你只是想让背景覆盖你的整个视口,不滚动它吗?这将使用 no-repeat
和 cover
来完成,例如:
Since repeating a gradient doesn't look that good, I guess you just want that background alwas cover your whole viewport and not scroll with it? That would be done with no-repeat
and cover
, like this:
body {
height:100%;
padding: 0;
margin: 0;
background:url(bg.jpg) no-repeat fixed;
background-position: bottom;
background-size: cover;
}
这篇关于滚动视口时,底部或右侧的背景图像被剪切掉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!