html - 背景覆盖背景大小

标签 html css

我已经知道我的问题的解决方案,但我想知道为什么会这样。为什么背景会覆盖背景大小?请不要写关于交叉浏览的内容。

HTML

<div class="icon"></div>

CSS

.icon {
    height: 60px;
    width: 60px;
    background-size: 60px 60px;
    background: transparent url("icon.png") no-repeat 0 0;

}

演示

http://jsfiddle.net/K74sw/2/

解决方案

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;

http://jsfiddle.net/K74sw/9/

或者只是换行。

http://jsfiddle.net/K74sw/10/

关于html - 背景覆盖背景大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17329850/

相关文章:

javascript - 如何修复非白色背景的 grumble.js jQuery 插件(气泡弹出窗口)?

javascript - 如何在 ASP.NET 网络表单上使用 javascript 设置文本框的值

html - CSS3 :not is not excluding an id

html - 滚动时隐藏固定 div 下方的内容 div

javascript - 主体背景与模态窗口重叠如何解决?

javascript - 当表格包含不同的 colSpan 和 rowSpan 时,html 表格固定行和列

html - 如何使用 swift 在 WKWebView 中预填充网站上的文本框

html - 如何在语义上为 HTML 中的列表提供标题、标题或标签

javascript - 在文本溢出时生成省略号

css - 使用 Bootstrap 导航栏居中菜单项