css - 为什么背景图片会随着这个标记缩放?

标签 css

我的风格定义是:

<style>
  .x { background-image:url(https://raw.githubusercontent.com/iamcal/emoji-data/master/sheet_emojione_64.png); 
       border:solid 1px red; 
     }
</style>

image file上面是 2624px x 2624px,在 64px 方形单元格中包含表情符号。 64是2624的1/41。单元格13,7中的图像是 Camel 。

我的标记是:

<div class='x' style='background-position:-832px -448px; height:64px; width:64px;' ></div>
<div class='x' style='background-position:-832px -448px; background-size:4100%; 
   height:64px; width:64px;' ></div>
<div class='x' style='background-position:-312px -168px; background-size:4100%; 
   height:24px; width:24px;' ></div>

JS Fiddle 位于 https://jsfiddle.net/5j5ahhda/输出是:

enter image description here

我对第二个 DIV 的理解是,我将一个 2624px 的图像压缩到一个 64px 的正方形中,是其大小的 1/41,然后将其放大 41 次,从而得到一个图像与原始尺寸相同。这种理解是否正确?

第三个 DIV 产生了我打算实现的目标,但我不明白是什么导致背景图像很好地缩放到 24/64。导致缩放的是包含 DIV 的指定大小吗?

我想要的是原始图像大小的 24/64。有没有更直接的方法?

最佳答案

My understanding of the second DIV is that I am squeezing a 2624px image into a 64px square, 1/41th of its size, and then blowing it up 41 times, thus resulting in an image the same as the original size. Is this understanding correct?

是的。

The third DIV produces what I was setting out to achieve, but I don't understand what causes the background image to scale nicely to 24/64. Is it the specified size of the containing DIV that causes the scaling?

是的。 background-size 的百分比是 relative to the size of the element's background positioning area .该区域由元素的尺寸决定,而不是图像的尺寸。当设置百分比 background-size 时,这允许背景图像根据元素的尺寸随元素的大小缩放。

关于css - 为什么背景图片会随着这个标记缩放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40934206/

相关文章:

css - 在 CSS 中制作人字形倾斜 block

javascript - 保存带有背景图像的 Canvas

CSS 嵌套 :not() selector

javascript - 如何将悬停规则保持在 Bootstrap 下拉按钮之上

c# - 如何确定设备是否为桌面浏览器?

jquery toggle - 重复双切换效果+不保持打开状态

css - 具有固定长度和高度的 Angular Bootstrap 卡。有没有办法减少代码?

html - CSS 全高 float 图例

带有边框问题的 HTML 导航 float

css - 如何在数据选框中添加 anchor href