10個CSS簡寫/優化技巧-摘自網友

10個CSS簡寫/優化技巧
23
來源/做者:未知 類別:前端開發 字體大小:大|中|小 背景顏色:藍|白|灰 ? ?css

CSS簡寫就是指將多行的CSS屬性簡寫成一行,又稱爲CSS代碼優化或CSS縮寫。CSS簡寫的最大好處就是可以顯著減小CSS文件的大小,優化網站總體性能,更加容易閱讀。前端

下面介紹常見的CSS簡寫規則:css3

1、盒子大小
這裏主要用於兩個屬性:margin和padding,咱們以margin爲例,padding與之相同。盒子有上下左右四個方向,每一個方向都有個外邊距:web

1
2
3
4
margin-top:1px;
margin-right:2px;
margin-bottom:3px;
margin-left:4px;
你能夠簡寫成:瀏覽器

1
margin:1px 2px 3px 4px;
語法 margin:top right bottom left;ide

1
2
3
4
5
6
7
8
//四個方向的邊距相同,等同於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;
2、邊框(border)性能

邊框的屬性以下:字體

1
2
3
border-width:1px;
border-style:solid;
border-color:#000;
能夠縮寫爲一句:優化

1
border:1px solid #000;
語法 border:width style color;網站

3、背景(Backgrounds)
背景的屬性以下:

1
2
3
4
5
background-color:#f00;
background-image:url(background.gif);
background-repeat:no-repeat;
background-attachment:fixed;
background-position:00;
能夠縮寫爲一句:

1
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%

4、字體(fonts)
字體的屬性以下:

1
2
3
4
5
font-style:italic;
font-variant:small-caps;
font-weight:bold;font-size:1em;
line-height:140%;
font-family:"Lucida Grande",sans-serif;
能夠縮寫爲一句:

1
font:italic small-caps bold 1em/140%"Lucida Grande",sans-serif;
注意,若是你縮寫字體定義,至少要定義font-size和font-family兩個值。

5、列表(lists)
取消默認的圓點和序號能夠這樣寫list-style:none;,
list的屬性以下:

1
2
3
list-style-type:square;
list-style-position:inside;
list-style-image:url(image.gif);
能夠縮寫爲一句:

1
list-style:square inside url(image.gif);
6、顏色(Color)

16進制的色彩值,若是每兩位的值相同,能夠縮寫一半。例如:

Aqua: #00ffff ——#0ff
Black: #000000 ——#000
Blue: #0000ff ——#00f
Dark Grey: #666666 ——#666
Fuchsia:#ff00ff ——#f0f
Light Grey: #cccccc ——#ccc
Lime: #00ff00 ——#0f0
Orange: #ff6600 ——#f60
Red: #ff0000 ——#f00
White: #ffffff ——#fff
Yellow: #ffff00 ——#ff0

7、屬性值爲0
書寫原則是若是CSS屬性值爲0,那麼你沒必要爲其添加單位(如:px/em),你可能會這樣寫:

1
padding:10px 5px 0px 0px;
試試這樣吧:

1
padding:10px 5px 00 ;
8、最後一個分號

最後一個屬性值後面分號能夠不寫,如:

1
2
3
4
5
6
#nav{
border-top:4px solid #333;
font-style: normal;
font-variant:normal;
font-weight: normal;
}
能夠簡寫成:

1
2
3
4
5
6
#nav{
border-top:4px solid #333;
font-style: normal;
font-variant:normal;
font-weight: normal
}
9、字體粗細(font-weight)

你可能會這樣寫:

1
2
3
4
5
6
h1{
font-weight:bold;
}
p{
font-weight:normal;
}
能夠簡寫成:

1
2
3
4
5
6
h1{
font-weight:700;
}
p{
font-weight:400;
}
10、圓角半徑(border-radius)
border-radius是css3中新加入的屬性,用來實現圓角邊框。

1
2
3
4
5
6
7
8
9
-moz-border-radius-bottomleft:6px;
-moz-border-radius-topleft:6px;
-moz-border-radius-topright:6px;
-webkit-border-bottom-left-radius:6px;
-webkit-border-top-left-radius:6px;
-webkit-border-top-right-radius:6px;
border-bottom-left-radius:6px;
border-top-left-radius:6px;
border-top-right-radius:6px;
能夠簡寫成:

123-moz-border-radius:0 6px 6px;-webkit-border-radius:0 6px 6px;border-radius:0 6px 6px;語法 border-radius:topleft topright bottomright bottomleft

相關文章
相關標籤/搜索