简体   繁体   English

同时具有多个值(px和rem)的CSS元素

[英]CSS element with multiple values (px & rem) at the same time

I have some CSS code in which many elements are having multiple values to a single css attribute. 我有一些CSS代码,其中许多元素对单个css属性具有多个值。

For example, below CSS will produce a button like shown below. 例如,下面的CSS将产生一个如下所示的按钮。

CSS 的CSS

.btn-gold {
    display: block;
    text-decoration: none;
    font-size: 20px;
    font-size: 2rem;
    line-height: 22px;
    line-height: 2.2rem;
    color: #222;
    padding: 8px 0;
    padding: 0.8rem 0;
    text-align: center;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    background: rgb(254,196,26);
    background: -moz-linear-gradient(top, rgb(254,196,26) 0%, rgb(255,170,0) 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgb(254,196,26)), color-stop(100%,rgb(255,170,0)));
    background: -webkit-linear-gradient(top, rgb(254,196,26) 0%,rgb(255,170,0) 100%);
    background: -o-linear-gradient(top, rgb(254,196,26) 0%,rgb(255,170,0) 100%);
    background: -ms-linear-gradient(top, rgb(254,196,26) 0%,rgb(255,170,0) 100%);
    background: linear-gradient(to bottom, rgb(254,196,26) 0%,rgb(255,170,0) 100%);
    -webkit-box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, .4);
    box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, .4);
}

Result 结果

在此处输入图片说明

Now I am not understanding what's the purpose of giving multiple values to a single attribute; 现在,我不了解为单个属性赋予多个值的目的是什么; see font-size , line-height and padding are given two values?? 看到font-sizeline-heightpadding有两个值?

font-size: 20px;
font-size: 2rem;
line-height: 22px;
line-height: 2.2rem; 
padding: 8px 0;
padding: 0.8rem 0;

Is this a valid CSS and how many combination of values are allow to a single attribute. 这是有效的CSS,并且单个属性允许多少个值组合。

It's to give a fallback to older browsers. 这是对较旧的浏览器的后备。

A modern browser will first apply "22px" line-height, then overwrite this with a "2.2rem" line-height. 现代浏览器将首先应用“ 22px”的行高,然后使用“ 2.2rem”的行高覆盖。 An old browser will apply "22px", then won't understand "2.2rem" and won't use it. 旧的浏览器将应用“ 22px”,然后将无法理解“ 2.2rem”并且不会使用它。

This is often use with rem values, or background for linear-gradient, for example. 例如,这通常与rem值或线性渐变的背景一起使用。

PS: note that you don't need prefixes for box-shadow or border-radius anymore. PS:请注意,框阴影或边框半径不再需要前缀。 ;) ;)

Px is Absolute Length (cm, mm, in, pt, pc, px) And Px是绝对长度(cm,mm,in,pt,pc,px)并且

Rem is font-relative Length (em, ex, ch, rem) Rem是字体相对长度(em,ex,ch,rem)

Either px or rem will run on browser open inspect Element in chrome and see only one value accept another reject with dash line px或rem均可在浏览器中运行,打开chrome中的inspect元素,然后仅看到一个值用虚线接受另一个拒绝

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

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