[英]How to crop not stretch an image in a slider? I've tried 'background:cover'
[英]How do I stretch a background image to cover the entire HTML element?
我正在尝试获取 HTML 元素(body、div 等)的背景图像以拉伸其整个宽度和高度。
运气不太好。 除了作为背景图像之外,它甚至可能还是我必须以其他方式来做?
我目前的 css 是:
body {
background-position: left top;
background-image: url(_images/home.jpg);
background-repeat: no-repeat;
}
提前致谢。
编辑:我并不热衷于在 Gabriel 的建议中维护 CSS,所以我正在更改页面的布局。 但这似乎是最好的答案,所以我将其标记为这样。
<style>
{ margin: 0; padding: 0; }
html {
background: url('images/yourimage.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
</style>
使用background-size
属性: http : //www.w3.org/TR/css3-background/#the-background-size
总之你可以试试这个......
<div data-role="page" style="background:url('backgrnd.png'); background-repeat: no-repeat; background-size: 100% 100%;" >
我很少使用 css 和 js 的地方...
<link rel="stylesheet" href="css/jquery.mobile-1.0.1.min.css" />
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery.mobile-1.0.1.min.js"></script>
它对我来说很好用。
不确定是否可以拉伸背景图像。 如果您发现它在所有目标浏览器中都不可能或不可靠,您可以尝试使用 z-index 设置较低的拉伸 img 标签,并将位置设置为绝对,以便其他内容出现在它上面。
让我们知道你最终做了什么。
编辑:我建议的基本上是加布里埃尔链接中的内容。 所以试试吧:)
要扩展@PhiLho 答案,您可以在页面上将一个非常大的图像(或任何尺寸的图像)居中:
{
background-image: url(_images/home.jpg);
background-repeat:no-repeat;
background-position:center;
}
或者您可以使用背景颜色与图像背景相匹配的较小图像(如果它是纯色)。 这可能适合也可能不适合您的目的。
{
background-color: green;
background-image: url(_images/home.jpg);
background-repeat:no-repeat;
background-position:center;
}
如果您需要在调整屏幕大小时拉伸背景图像并且不需要与旧浏览器版本兼容,则可以这样做:
body {
background-image: url('../images/image.jpg');
background-repeat: no-repeat;
background-size: cover;
}
如果您有一张大的横向图像,此示例在纵向模式下调整背景大小,使其显示在顶部,底部留空:
html, body {
margin: 0;
padding: 0;
min-height: 100%;
}
body {
background-image: url('myimage.jpg');
background-position-x: center;
background-position-y: bottom;
background-repeat: no-repeat;
background-attachment: scroll;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
@media screen and (orientation:portrait) {
body {
background-position-y: top;
-webkit-background-size: contain;
-moz-background-size: contain;
-o-background-size: contain;
background-size: contain;
}
}
我主要使用以下代码来实现所要求的效果:
body {
background-image: url('../images/bg.jpg');
background-repeat: no-repeat;
background-size: 100%;
}
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
你不能在纯 CSS 中。 在所有其他组件后面覆盖整个页面的图像可能是您最好的选择(看起来这就是上面给出的解决方案)。 无论如何,无论如何它看起来都会很糟糕。 我会尝试使用足够大的图像来覆盖大多数屏幕分辨率(比如高达 1600x1200,高于它的更少),以限制页面的宽度,或者只是使用平铺的图像。
图片{
background-size: cover;
background-repeat: no-repeat;
background-position: center;
padding: 0 3em 0 3em;
margin: -1.5em -0.5em -0.5em -1em;
width: absolute;
max-width: 100%;
只需将 div 设为 body 的直接子级(例如类名 bg),包含 body 中的所有其他元素,并将其添加到 CSS 文件中:
.bg {
background-image: url('_images/home.jpg');//Put your appropriate image URL here
background-size: 100% 100%; //You need to put 100% twice here to stretch width and height
}
请参阅此链接: https: //www.w3schools.com/css/css_rwd_images.asp 向下滚动到以下部分:
- 如果 background-size 属性设置为“100% 100%”,背景图像将拉伸以覆盖整个内容区域
无论您如何调整大小,它都会显示“img_flowers.jpg”拉伸到屏幕或浏览器的大小。
这个对我有用
.page-bg {
background: url("res://background");
background-position: center center;
background-repeat: no-repeat;
background-size: 100% 100%;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.