[英]How exactly background-size and background-repeat works?
Here's an example . 这是一个例子 。 I'd like to crop background image, and then to use cropped image as background for a bigger (in size) element. 我想裁剪背景图像,然后将裁剪后的图像用作更大(大小)元素的背景。 I mean the div is bigger than its background, and I don't need repetition. 我的意思是div大于其背景,并且我不需要重复。 Now when background-repeat
is uncommented, element disappear. 现在,当取消对background-repeat
的注释时,元素消失。 But I supposed that it will show cropped unrepeated background. 但是我以为它将显示裁剪过的重复背景。
#div { background-image: url(http://dummyimage.com/600x400/000/fff); padding: 10px; width: 100px; height: 100px; background-position: 0px -100px; background-size: 100px 100px; background-repeat: no-repeat; /*comment this*/ position: absolute; }
<div id="div"></div>
The background
disappears when the background-repeat: no-repeat
setting is added because of the position that is assigned to the background. 当添加background-repeat: no-repeat
设置时, background
消失了,因为已将位置分配给背景。 It is set at 0px -100px
with respect to the origin. 相对于原点设置为0px -100px
。 You haven't set any value for background-origin
and so the default value (which is, padding-box
would be used) and the height of the image is only 100px. 您尚未为background-origin
设置任何值,因此默认值(将使用padding-box
)和图像的高度仅为100px。 Because of this when you instruct the browser to not repeat the image, there is nothing in the visible area. 因此,当您指示浏览器不要重复图像时,可见区域中没有任何内容。
For the case illustrated in demo, the image doesn't need to be cropped because its size is only 100 x 100 (set using background-size
) and the div
box's size (along with padding
of 10px on all sides) is bigger than the image (refer to the below snippet to see this in action). 对于演示中所示的情况, 不需要裁剪图像,因为它的大小仅为100 x 100(使用background-size
设置),并且div
框的大小(以及所有padding
均为10px)大于图片(请参阅下面的代码片段以查看实际效果)。
If you mean to say that you'd like to scale the 600 x 400 image into 100 x 100 using background-size
property and display it within the div
then you could do it as shown in the below snippet itself. 如果您想说要使用background-size
属性将600 x 400的图像缩放为100 x 100并在div
显示,则可以按照下面的代码片段所示进行操作。
.div { /*position: absolute; commented out for demo */ background-image: url(http://dummyimage.com/600x400/000/fff); padding: 10px; width: 100px; height: 100px; /*background-position: 0px -100px; - this makes it positioned above the viewable area of the box container, so remove it */ background-size: 100px 100px; background-repeat: no-repeat; border: 1px solid red; /* just to show how box is bigger than image */ } /* If the image has to be centered within the div */ .div.centered { background-position: 50% 50%; } /* Just for demo */ div{ margin: 10px;}
<div class="div"></div> <div class="div centered"></div>
On the other hand, if you were intending to use background-position
to specify the area from where the cropping should be done then it is not possible to do that way. 另一方面,如果您打算使用background-position
来指定应从中进行裁剪的区域,则无法这样做。 For such a case, you should avoid using background-size
and just use background-position
like in the below snippet. 在这种情况下,您应该避免使用background-size
而应像下面的代码片段一样使用background-position
。
Here, by specifying background-position
as -240px -140px
, the portion of the image that is present within the coordinates (240,140) to (360,260) on the image would be displayed within the box. 在此,通过将background-position
指定为-240px -140px
,图像中坐标(240,140)至(360,260)内的图像部分将显示在框中。 It shows 120 x 120 pixels of image due to the size of the box (including padding). 由于框的大小(包括填充),它显示120 x 120像素的图像。
.div { position: absolute; background-image: url(http://dummyimage.com/600x400/000/fff); padding: 10px; width: 100px; height: 100px; background-position: -240px -140px; background-repeat: no-repeat; border: 1px solid red; /* just to show how box is bigger than image */ } /* Just for demo */ div{ margin: 10px; }
<div class="div"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.