使用 CSS 在背景图像上设置大小? [英] Set size on background image with CSS?

查看:33
本文介绍了使用 CSS 在背景图像上设置大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 CSS 设置背景图片的大小?

Is it possible to set the size of the background image with CSS?

我想做类似的事情:

background: url('bg.gif') top repeat-y;
background-size: 490px;

但这样做似乎完全错误...

But it seems it's totally wrong to do it like that...

推荐答案

CSS2

如果您需要将图像放大,则必须在图像编辑器中编辑图像本身.

CSS2

If you need to make the image bigger, you must edit the image itself in an image editor.

如果您使用 img 标签,您可以更改大小,但是如果您需要图像作为其他内容的背景(并且它不会像您想要的那样重复),则不会给您想要的结果...

If you use the img tag, you can change the size, but that would not give you the desired result if you need the image to be background for some other content (and it will not repeat itself like you seems to want)...

这可以通过 background-size 在 CSS3 中实现.

This is possible to do in CSS3 with background-size.

所有现代浏览器都支持这一点,所以除非你需要支持旧浏览器,否则就是这样做的方式.
支持的浏览器:
Mozilla Firefox 4.0+ (Gecko 2.0+)、Microsoft Internet Explorer 9.0+、Opera 10.0+、Safari 4.1+ (webkit 532) 和 Chrome 3.0+.

All modern browsers support this, so unless you need to support old browsers, this is the way to do it.
Supported browsers:
Mozilla Firefox 4.0+ (Gecko 2.0+), Microsoft Internet Explorer 9.0+, Opera 10.0+, Safari 4.1+ (webkit 532) and Chrome 3.0+.

.stretch{
/* Will stretch to specified width/height */
  background-size: 200px 150px;
}
.stretch-content{
/* Will stretch to width/height of element */
  background-size: 100% 100%;
}

.resize-width{
/* width: 150px, height: auto to retain aspect ratio */
  background-size: 150px Auto;
}
.resize-height{
/* height: 150px, width: auto to retain aspect ratio */
  background-size: Auto 150px;
}
.resize-fill-and-clip{ 
  /* Resize to fill and retain aspect ratio.
     Will cause clipping if aspect ratio of box is different from image. */ 
  background-size: cover;
}
.resize-best-fit{
/* Resize to best fit and retain aspect ratio.
   Will cause gap if aspect ratio of box is different from image. */ 
  background-size: contain;
}

特别是,我喜欢 covercontain 值,它们为我们提供了以前没有的新控制能力.

In particular, I like the cover and contain values that gives us new power of control that we didn't have before.

你也可以使用background-size: round 与repeat结合使用有含义:

You can also use background-size: round that have a meaning in combination with repeat:

.resize-best-fit-in-repeat{
/* Resize to best fit in a whole number of times in x-direction */ 
  background-size: round auto; /* Height: auto is to keep aspect ratio */
  background-repeat: repeat;
}

这将调整图像宽度,使其适合背景定位区域的整数倍.

This will adjust the image width so it fits a whole number of times in the background positioning area.

附加说明
如果您需要的大小是静态像素大小,那么物理调整实际图像的大小仍然是明智之举.这既是为了提高调整大小的质量(假设您的图像软件比浏览器做得更好),又是为了在原始图像大于要显示的图像时节省带宽.

Additional note
If the size you need is static pixel size, it is still smart to physically resize the actual image. This is both to improve quality of the resize (given that your image software does a better job than the browsers), and to save bandwidth if the original image is larger than what to display.

这篇关于使用 CSS 在背景图像上设置大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆