简体   繁体   English

我可以将图片扩展为适合HTML部分的边界吗?

[英]Can I make my image expand to fit the boundaries of an HTML section?

I have the following HTML: 我有以下HTML:

<section id="infopic" class="grid_5 prefix_1" style="height: 500px;">
   <div id="login-image"></div>
</section>

and this CSS: 和这个CSS:

#infopic {
   background-image: url("/Images/login.png");
}

I have a few different ways to make my image stretch to fit the boundaries of the #infopic but still cannot get this to work right. 我有几种不同的方法来拉伸图像以适应#infopic的边界,但仍然无法使其正常工作。 Right now it repeats in the code above the image (which is smaller than 500px square) just repeats. 现在,它会在图像上方的代码(小于500像素见方)中重复。

try adding background-size property. 尝试添加background-size属性。

Some examples: http://www.css3.info/preview/background-size/ 一些示例: http : //www.css3.info/preview/background-size/

You could try the background-size declaration: 您可以尝试background-size声明:

background-size: 100% 100%;

This will work in IE9+, Firefox 4+, Opera, Chrome, and Safari 5+. 这将在IE9 +,Firefox 4 +,Opera,Chrome和Safari 5+中运行。 I don't think it can be done in older browsers. 我认为这无法在较旧的浏览器中完成。 Perhaps: 也许:

#infopic {
    background-image: url("/Images/login.png") 50% 50% no-repeat;
    background-size: 100% 100%;
}

for backwards compatibility? 向后兼容?

You can use the CSS3 property background-size: 100%; 您可以使用CSS3属性background-size: 100%; to stretch your image over the entire element: 在整个元素上拉伸图像:

#infopic {
  height: 500px;
  width: 1000px;
  background-image: url("http://placehold.it/250x250");
  background-repeat: no-repeat;
  background-size: 100%;
}

This is a CSS3 property so it's not supported in older browsers (I'm not even sure of IE support) but it's useful for modern browsers. 这是CSS3属性,因此在较旧的浏览器中不支持(我什至不确定IE是否支持),但对现代浏览器很有用。

As an additional note, mixing external and inline CSS is a terrible idea, that's why I moved the height declaration to the external CSS. 另外需要注意的是,混合使用外部CSS和内联CSS是一个糟糕的主意,这就是为什么我将height声明移到外部CSS的原因。

:) :)

http://jsfiddle.net/x4w7V/ http://jsfiddle.net/x4w7V/

Use like this: 像这样使用:

img {
  background-size: 100%;
  max-width: 100%;
}

Reference: 参考:

Fliud Images 欺骗图像

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

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