繁体   English   中英

HTML 正文背景图像未覆盖所有页面

[英]HTML body background image not cover all page

我正在尝试将背景图像设置为整个 html 页面,但我每次都失败了。它没有拉伸。这是我的 css 和 html 代码:

css代码:

body {
    background-image:url(body_background.png);
    background-repeat: no-repeat;
    background-size: 100% 100%;
    width:1200px;
    margin:0 auto;
}

#main_container_div {
    padding: 0px;
    width: 1000px;
    margin-top: 30px;
    margin-left: 150px;
}

#div2 {
    float: left;
    width: 250px;
    height: 600px;
    margin-top: 0px;
    margin-right: 0px;
    background-repeat: no-repeat;
}
#div3 {
    position: relative;
    float: left;
    width: 750px;
    height: 500px;
    margin-top: 50px;
    overflow: hidden;
}

html正文:

<body>    
    <div id="main_container_div">
        <div id="div3">
        </div>
        <div id="div2">
        </div>
    </div>
</body>

您可以尝试使用 1366x768 大小的图像。 此外,我正在 1366x768 分辨率下尝试此操作。请注意,此问题在此分辨率或更高分辨率上可见。感谢您的帮助。

试试 background-size: cover 在你的 css 中

background-size:cover;

不要在体内使用它。 像这样在 html 元素上尝试:

html
{
  background-image: url('your url');
  background-repeat: no-repeat;
  min-height:100%;
}

<html>
  <!--bg is applied here instead -->
  <body>
    ...
  </body>
</html>

如果要全页显示背景,则需要将背景设置为重复。

background-repeat: no-repeat;

background-repeat: repeat-x;

要么

background-repeat: repeat-y;
background: url(body_background.png) 100% 100% no-repeat;
background-size: cover;
background-size:cover;

确实有效,但它不支持所有浏览器类型。 这是我过去使用的代码,

section#background { 
    background: url('../img/background/feature.jpg') no-repeat center center; 
    filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='img/background/feature.jpg',sizingMethod='scale'); 
    -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='img/background/feature.jpg',sizingMethod='scale')"; 
    -webkit-background-size: cover; 
    -moz-background-size: cover; 
    background-size: cover; }

这是 jsfiddler 链接

http://jsfiddle.net/Raver0124/3AwHa/1/

这通常对我有用,因为我通常用 html 而不是 body 来做。 这有效,因为它需要 Web 浏览器的整个页面。 附: 我知道这是旧的,认为它会有所帮助。

html { 
    background: url(images/bg.jpg or whatever link or img here) no-repeat center fixed; 
    background-size:cover;
}

我刚刚遇到了同样的问题,您可以通过拉伸图像来解决它:

html{
    min-height: 100%;
}

或者保持相同的比例,你可以这样做:

body{
    background-size: cover;
    background-attachement: fixed;
}

就我而言,我通过设置解决了它

height: auto

这样,如果内容比竖屏大,它会一直增长,直到把它全部包裹起来。 但你也必须设置

min-height: 100vh

因此,如果内容小于它,背景将增长到占据所有可见的垂直屏幕。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM