我已经知道我的问题的解决方案,但我想知道为什么会这样。为什么背景会覆盖背景大小?请不要写关于交叉浏览的内容。
HTML
<div class="icon"></div>
CSS
.icon {
height: 60px;
width: 60px;
background-size: 60px 60px;
background: transparent url("icon.png") no-repeat 0 0;
}
演示
解决方案
在background
下方插入background-size
指令
最佳答案
background
在您设置大小后重置图像位置。
原因是“背景”属性是在样式表中的同一位置设置大多数背景属性的简写属性。
包括 background-size
,这是您之前尝试使用的那个。
编辑了新的 CSS 规范链接:https://www.w3.org/TR/css-backgrounds-3/#background
你用
background-size: 60px 60px;
background: transparent url("icon.png") no-repeat 0 0;
转化为:
background-size: 60px 60px; /*You try to set bg-size*/
background-color: transparent ;
background-position: 0% 0%;
background-size: auto auto; /* it resets here */
background-repeat: no-repeat no-repeat;
background-clip: border-box;
background-origin: padding-box;
background-attachment: scroll;
background-image: url("icon.png");
所以你可以尝试使用单独的background-...
设置
所以要么使用:
background-size: 100% 100%;
background-image: url("http://1.bp.blogspot.com/-T3EviK7nK1s/TzEq90xiJCI/AAAAAAAABCg/OMZsUBxuMf0/s400/smilelaughuy0.png");
background-repeat:no-repeat;
background-position:0 0;
或者只是换行。
关于html - 背景覆盖背景大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17329850/