有没有办法对背景图像使用最大宽度和高度?
就这么简单。
将我的布局移动到一个流动的区域,处理可缩放的图像。使用 img 标签并将 max-width 设置为 100% 效果很好,但我宁愿使用将图像设置为背景的 div。
我遇到的问题是图像无法缩放到其背景中的 div 的大小。有什么方法可以将标记添加到后台代码以将其设置为容器的 100% 宽度吗?
#one {
background: url('../img/blahblah.jpg') no-repeat;
max-width: 100%;
}
That simple.
Moving my layout into a fluid territory, working on scalable images. Using the img tag and setting max-width to 100% works perfectly, but i'd rather use a div with the image set as its background.
The issue I'm running into is that the image doesn't scale to the size of the div it's in the background of. Any way to add markup to the background code to set it to 100% width of it's container?
#one {
background: url('../img/blahblah.jpg') no-repeat;
max-width: 100%;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如三十点所说,您可以使用 CSS3
background-size
语法:例如:
但是,正如三十点所说,这在 IE6、7 和 8 中不起作用。
有关
background-size
的更多信息,请参阅以下链接:http://www.w3.org/TR/css3-background/#the-background-尺寸
As thirtydot said, you can use the CSS3
background-size
syntax:For example:
However, as also stated by thirtydot, this does not work in IE6, 7 and 8.
See the following links for more information about
background-size
:http://www.w3.org/TR/css3-background/#the-background-size
您可以使用
background-size
来做到这一点:除了
cover
之外,您还可以将background-size
设置为很多值,看看哪些一个适合你: https://developer.mozilla.org/en-US/docs/Web/CSS/background-size规范:https://www.w3.org/TR/css-backgrounds-3/#the-background-size
适用于所有现代浏览器:http://caniuse.com/#feat=background-img-opts
You can do this with
background-size
:There are a lot of values other than
cover
that you can setbackground-size
to, see which one works for you: https://developer.mozilla.org/en-US/docs/Web/CSS/background-sizeSpec: https://www.w3.org/TR/css-backgrounds-3/#the-background-size
It works in all modern browsers: http://caniuse.com/#feat=background-img-opts
看起来您正在尝试缩放背景图像?下面的参考文献中有一篇很棒的文章,您可以使用 css3 来实现此目的。
如果我错过了这个问题,那么我会谦虚地接受否决票。 (不过还是很高兴知道)
请考虑以下代码:
这适用于所有主要浏览器,当然在 IE 上并不容易。然而,有一些解决方法,例如使用 Microsoft 的过滤器:
有一些替代方案可以通过使用 jQuery 来放心地使用:
HTML
CSS
jQuery:
我希望这会有所帮助!
源:完美的整页背景图片
It looks like you're trying to scale the background image? There's a great article in the reference bellow where you can use css3 to achieve this.
And if I miss-read the question then I humbly accept the votes down. (Still good to know though)
Please consider the following code:
This will work on all major browsers, of course it doesn't come easy on IE. There are some workarounds however such as using Microsoft's filters:
There are some alternatives that can be used with a little bit peace of mind by using jQuery:
HTML
CSS
jQuery:
I hope this helps!
Src: Perfect Full Page Background Image
不幸的是,CSS 中没有
min
(或max
)-background-size,您只能使用背景大小
。但是,如果您正在寻找响应式背景图像,则可以使用background-size
属性的Vmin
和Vmax
单位来实现类似的效果。示例:
这会将高度设置为垂直或水平方向上较小视口的 10%,并将宽度设置为 100%。
在这里阅读有关 css 单元的更多信息: https://www.w3schools.com/cssref/css_units.asp
Unfortunately there's no
min
(ormax
)-background-size in CSS you can only usebackground-size
. However if you are seeking a responsive background image you can useVmin
andVmax
units for thebackground-size
property to achieve something similar.example:
that will set the height to 10% of the whichever smaller viewport you have whether vertical or horizontal, and will set the width to 100%.
Read more about css units here: https://www.w3schools.com/cssref/css_units.asp
这就是您正在寻找的解决方案!
This is the solution you are looking for!
使用 :before 或 :after 和 max-height
Use :before Or :after with max-height