日期:2014-05-17  浏览次数:20653 次

CSS简写方法说明

? ? 简单的说,css简写就是在等效的前提下,把多句css代码简化成一句。在我看来,简写css的好处有三:一是写起来方便(就像键盘快捷键);二是简化代码;三是帮助你熟悉和深刻理解css。

闲话少说,书归正传。能够简写的css属性主要有以下几个:

font

简写:

font:italic small-caps bold 12px/1.5em arial,verdana;
?

等效于:

font-style:italic;
font-variant:small-caps;
font-weight:bold;
font-size:12px;
line-height:1.5em;
font-family:arial,verdana;
?

顺序:font-style | font-variant | font-weight | font-size | line-height | font-family

(注:简写时,font-size和line-height只能通过斜杠/组成一个值,不能分开写。)

background

简写:

background:#fff url(bg.gif) no-repeat fixed left top;
?

等效于:

background-color:#fff;
background-image:url(bg.gif);
background-repeat:no-repeat;
background-attachment:fixed;
background-position:left top;
?

顺序:background-color | background-image | background-repeat | background-attachment | background-position

margin & padding

简写:

margin:1px 0 2em -20px;
?

等效于:

margin-top:1px;
margin-right:0;
margin-bottom:2em;
margin-left:-20px;
?

顺序:margin-top | margin-right | margin-bottom | margin-left

padding的简写和margin完全一样。

border

简写:

border:1px solid #000;
?

等效于:

border-width:1px;
border-style:solid;
border-color:#000;
?

顺序:border-width | border-style |