简体   繁体   English

背景尺寸不起作用

[英]Background-size doesn't work

Here's my CSS: 这是我的CSS:

.banner-text-BG {
    background: #00A7E1 url(images/sale_tag.png) left center no-repeat !important;  
    background-size: 20px 20px;
    height: auto;
    padding: 15px;
    position: static;
    width: auto;
    -webkit-border-radius: 70px 10px 10px 70px;
    -moz-border-radius: 70px 10px 10px 70px;
    border-radius: 70px 10px 10px 70px;     
    padding-left: 70px;
}

Contrary to all the other styles, the "background-size: 20px;" 与所有其他样式相反,“ background-size:20px;” has no effect on my page, is not visible in Firebug, and as a sidenote is not highlighted as a valid CSS instruction in Notepad++. 对我的页面没有影响,在Firebug中不可见,并且在Notepad ++中,作为补充说明未作为有效的CSS指令突出显示。 Same with "background-size: 20px;" 与“ background-size:20px;”相同 or "background-size: 20px auto;" 或“ background-size:20px auto;”

Am I missing something? 我想念什么吗? Why does it not work? 为什么不起作用?

As the background-size docs state: 背景尺寸文档所述

If the value of this property is not set in a background shorthand property that is applied to the element after the background-size CSS property, the value of this property is then reset to its initial value by the shorthand property. 如果此属性的值未在background-size CSS属性之后应用于该元素的背景速记属性中设置,则该属性的值将由shorthand属性重置为其初始值。

In other words, if you're using the shorthand property but don't include the background-size rule as a part of it, the separate background-size is essentially reset and ignored. 换句话说,如果您使用速记属性,但不包括background-size规则作为其一部分,则实际上将重置并忽略单独的background-size。

So to get around that you just need to roll it into the background shorthand you're already using by adding a slash after the background-position and including your size: 因此,要解决该问题,只需将其滚动到已使用的背景速记中,方法是在背景位置后添加斜线并包括您的尺寸:

background: #00A7E1 url(http://placekitten.com/200/200) left center/20px 20px no-repeat !important;

jsFiddle example jsFiddle示例

Your use of !important in the background shorthand is preventing the background-size property from applying, at least in Chrome. 您在背景速记中使用!important至少在Chrome中无法应用background-size属性。

Removing the !important allows the background-sizing to take effect. 删除!important可使背景大小生效。 See the jsfiddle: http://jsfiddle.net/QVXsj/9/ 参见jsfiddle: http : //jsfiddle.net/QVXsj/9/

"!important" should be avoided. 应避免使用“!important”。

I would split all background attributes as such: 我将这样分割所有背景属性:

.banner-text-BG {
    background-image: url(images/sale_tag.png) 
    background-position: left center;
    background-color: #00A7E1; 
    background-repeat: no-repeat;
    background-size: 20px 20px;   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM