html – 背景覆盖背景大小

前端之家收集整理的这篇文章主要介绍了html – 背景覆盖背景大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经知道我的问题的解决方案,但我想知道为什么会这样.为什么背景会覆盖背景大小?请不要写关于交叉浏览.

HTML

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

CSS

  1. .icon {
  2. height: 60px;
  3. width: 60px;
  4. background-size: 60px 60px;
  5. background: transparent url("icon.png") no-repeat 0 0;
  6.  
  7. }

DEMO

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

在背景下插入背景大小的说明

解决方法

设置尺寸后,背景会重置图像位置.

原因是因为’background’属性是一个简写属性,用于在样式表中的同一位置设置大多数背景属性.
也是背景大小,这是您之前尝试使用的大小.

你用

  1. background-size: 60px 60px;
  2.  
  3. background: transparent url("icon.png") no-repeat 0 0;

这意味着:

  1. background-size: 60px 60px; /*You try to set bg-size*/
  2.  
  3. background-color: transparent ;
  4. background-position: 0% 0%;
  5. background-size: auto auto; /* it resets here */
  6. background-repeat: no-repeat no-repeat;
  7. background-clip: border-Box;
  8. background-origin: padding-Box;
  9. background-attachment: scroll;
  10. background-image: url("icon.png");

因此,您可以尝试使用单独的背景-…设置

所以要么使用:

  1. background-size: 100% 100%;
  2. background-image: url("http://1.bp.blogspot.com/-T3EviK7nK1s/TzEq90xiJCI/AAAAAAAABCg/OMZsUBxuMf0/s400/smilelaughuy0.png");
  3. background-repeat:no-repeat;
  4. background-position:0 0;

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

或者只是换掉你的线.

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

猜你在找的HTML相关文章