【发布时间】:2026-02-01 01:45:02
【问题描述】:
我有一张图片需要拉伸整个身体,所以我不知道最好的方法是什么
html{
/*background image properties*/
}
或
body{
/*background image properties*/
}
【问题讨论】:
我有一张图片需要拉伸整个身体,所以我不知道最好的方法是什么
html{
/*background image properties*/
}
或
body{
/*background image properties*/
}
【问题讨论】:
body{
background-image:url('../images/background.jpg');
background-attachment:fixed;
background-repeat: no-repeat;
background-size: cover;
}
这将是最好的方法,你可以将它应用到 HTML,这真的取决于你喜欢什么......
background-image:url('../images/background.jpg');
假设您的 css 文件位于不同的地图中,您执行 ../ 以转到放置 css 文件夹的地图,然后进入图像文件并选择图像。
background-attachment:fixed;
在设置背景图像时,我个人喜欢使用它,这样当用户滚动时,背景图像会保持其当前位置。
background-repeat: no-repeat;
当使用此设置时,它可以使图像不会重复,以防它太小或无法覆盖整个背景。
background-size: cover;
当您应用此选项时,您会将背景大小设置为覆盖,并结合 no-repeat 和 attachment: fixed 它是设置背景图像样式的好方法
【讨论】:
根据此处的 CSS 2.1 规范:http://www.w3.org/TR/CSS2/colors.html#background
但是,对于 HTML 文档,我们建议作者指定 BODY 元素而不是 HTML 元素的背景。为了 根元素是 HTML“HTML”元素或 XHTML 的文档 “html”元素的计算值为“透明” 'background-color' 和 'none' 对于 'background-image',用户代理必须 而是使用该背景属性的计算值 元素的第一个 HTML“BODY”元素或 XHTML“body”元素的子元素 为画布绘制背景,不得绘制背景 对于那个子元素....
因此,建议在body(而不是html)上使用background。
如果您希望background-image 拉伸整个容器(即body),那么您可以使用以下样式:
background-size: 100% 100%;
如果您想保留纵横比,则可以使用cover 使其覆盖整个容器,或使用contain 使其保持在容器边界内。当您使用contain 时,根据背景图片的纵横比,您可能会在图片下方或之后出现空白(信箱)。
background-image: url('...');
background-size: cover;
background-repeat: no-repeat;
...
.
【讨论】:
body{
/*background image properties*/
}
这是最好的方法,因为 body 是网页上所有可见元素的直接父级。
您可以使用background-size:contain; 用背景图片覆盖所有区域
body{
width:500px;
height:500px;
background:url(http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg);
background-cover;
background-repeat:no-repeat;
}
注意:还有一个我想到的案例:
<html>
<head>
some free data
</head>
<body>
</body>
</html>
这里some free data会显示在网页内部,即body内部,所以我们不会关心将背景属性赋予html tag,
只使用body{//background properties } 就可以了
Edit:
虽然这不是这里应该使用什么属性的问题。可以有各种各样的东西,例如:
background-size:cover;
OR
background-contain;
OR
background-100% 100%;
最适合您问题的属性是background-100% 100%;
【讨论】:
background-size:cover,不是吗?
background-100% 100%; 会拉伸背景图片
body{
background-image:url('../images/background.jpg');
background-attachment:fixed;
background-repeat: no-repeat;
background-size: cover;
}
【讨论】:
在语义上,我会在正文中使用。
body{
background-image: url(path.jpg);/*wearing a cloth in body instead of heart*/
}
似乎在语义上适用于全身。
【讨论】:
您应该定位body 标记并将background-size 属性应用于它。
像这样
body{
background-size: 100%;
}
【讨论】:
你可以使用
body{
background: url("http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg") no-repeat scroll 0 0 #fff;
}
【讨论】:
试试这个代码:
<!DOCTYPE HTML>
<html>
<head>
<title>Background to fit screen</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Imagetoolbar" content="no">
<style type="text/css">
/* pushes the page to the full capacity of the viewing area */
html {height:100%;}
body {height:100%; margin:0; padding:0;}
/* prepares the background image to full capacity of the viewing area */
#bg {position:fixed; top:0; left:0; width:100%; height:100%;}
/* places the content ontop of the background image */
#content {position:relative; z-index:1;}
</style>
</head>
<body>
<div id="bg"><img src="yourimage.jpg" width="100%" height="100%" alt=""></div>
<div id="content"><p>Enter a ton of text or whatever here.</p></div>
</body>
</html>
例如:Check this
【讨论】:
CSS3 background-size:cover 属性可以很好地处理全屏背景图像,包括响应性。以下内容适用于我测试过的所有桌面和移动设备。
body {
background-image: url(/assets/img/yourimage.jpg);
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
height:100%;
width:100%;
}
【讨论】:
background:no-repeat url(' ') #154454 bottom center ;
background-size:contain;
body {
background-image: url(/_assets/img/zoom-17536689-3.jpg);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
【讨论】:
.bk
{
background: url('../assets/imgs/img.jpg') no-repeat center center fixed;
}
【讨论】: