CSS簡寫就是指將多行的CSS屬性簡寫成一行,又稱為CSS代碼優化或CSS縮寫。CSS 
簡寫的最大好處就是能夠顯著減少CSS文件的大小,優化網站整體性能,更加容易閱讀。 下面介紹常見的CSS簡寫規則: 
一、盒子大小 
這里主要用于兩個屬性:margin和padding,我們以margin為例,padding與之相同。 
盒子有上下左右四個方向,每個方向都有個外邊距: 
| margin-top:1px; margin-right:2px; margin-bottom:3px; margin-left:4px; | 
你可以簡寫成:
| margin:1px 2px 3px 4px; | 
語法 margin: top right bottom left
| //四個方向的邊距相同,等同于margin:1px 1px 1px 1px; margin:1px; //上下邊距都為1px,左右邊距均為2px,等同于margin:1px 2px 1px 2px margin:1px 2px; //右邊距和左邊距相同,等同于margin:1px 2px 3px 2px; margin:1px 2px 3px; //注意,這里雖然上下邊距都為1px,但是這里不能縮寫。 margin:1px 2px 1px 3px; | 
二、邊框(border) 
邊框的屬性如下: 
| border-width:1px; border-style:solid; border-color:#000; | 
可以縮寫為一句:
| border:1px solid #000; | 
語法 border:width style color; 
三、背景(Backgrounds) 
背景的屬性如下: 
| background-color:#f00; background-image:url(background.gif); background-repeat:no-repeat; background-attachment:fixed; background-position:0 0; | 
可以縮寫為一句:
| background:#f00 url(background.gif) no-repeat fixed 0 0; | 
語法是background:color image repeat attachment position; 
你可以省略其中一個或多個屬性值,如果省略,該屬性值將用瀏覽器默認值,默認值 
為: 
| color: transparent image: none repeat: repeat attachment: scroll position: 0% 0% | 
四、字體(fonts) 
字體的屬性如下: 
| font-style:italic; font-variant:small-caps; font-weight:bold; font-size:1em; line-height:140%; font-family:"Lucida Grande",sans-serif; | 
可以縮寫為一句:
| font:italic small-caps bold 1em/140% "Lucida Grande",sans-serif; | 
注意,如果你縮寫字體定義,至少要定義font-size和font-family兩個值。 
五、列表(lists) 
取消默認的圓點和序號可以這樣寫list-style:none;, 
list的屬性如下: 
| list-style-type:square; list-style-position:inside; list-style-image:url(image.gif); | 
可以縮寫為一句:
| list-style:square inside url(image.gif); | 
六、顏色(Color)
新聞熱點
疑難解答