標籤:style http color 使用 strong os
CSS代碼編寫的過程中,代碼數量非常龐大,許多屬性會被反覆使用,這樣來來回回地敲打同樣的屬性名稱會佔用不少的時間。作為一名優秀的web前端人員,應當瞭解記憶各種屬性的簡寫方式,用以提高自身的工作效率,縮減代碼數量,增強可讀性。作為一名剛接觸不久的初學者,在網上搜集資料,整理了常用屬性的簡寫入模式,供大家參考。
1、顏色color簡寫
在顏色值採用16進位,且每兩位的值相同,可以簡寫一半。
簡寫為
2、邊距margin和padding簡寫
外邊距margin和內邊距padding在CSS中使用率是很高,margin和padding都有四個方向的邊距
| 1 2 3 4 |
padding-top : 2px ; padding-right : 2px ; padding-bottom : 2px ; padding-left : 2px ; |
有四種簡寫方式,都很好記
| 1 2 3 4 |
padding : 1px ;//四個方向的邊距相同 padding : 1px 2px ;//第一個值表示上下邊距,第二個值表示左右邊距 padding : 1px 2px 3px ;//第一個值表示上邊距,中間值表示左右邊距,第三個值表示下邊距 padding : 1px 2px 3px 4px ;//邊距值以順時針方向顯示,上、右、下、左 |
3、邊框border簡寫
邊框有三個屬性
| 1 2 3 4 5 |
border-width: 1px 2px 3px 4px ; //上下左右,類似邊距的簡寫 border-style: solid dashed dotted groove ; border-color : red #ffffff ; 1 按照width、style和color的順序簡寫 1 border: 1px solid #ffffff ;//個人喜歡總結為三字口訣“粗型色” |
4、背景background簡寫
一共五個屬性,background採用簡寫將大大提供效率
| 1 2 3 4 5 |
background-color : #ffffff || transparent ; background-image : url () || none ; background-repeat : repeat || repeat-x || repeat-y || no-repeat ; background-position : X Y || ( top || bottom || center ) ( left || right || center ); background-attachment : scroll || fixed ; |
background簡寫按順序如下
| 1 |
background : #fff url (img.gif) no-repeat 0 0 ;//個人總結五字口訣“色圖重定位” |
background的屬性有預設值,且不會繼承,你可以只聲明其中的一個,其它的值會被應用預設值。
| 1 |
background : transparent none repeat scroll top left ; |
5、字型font簡寫
font有六個屬性,採用簡寫的話比如簡化代碼提供效率,不過簡寫規則要求比較高,要注意
| 1 2 3 4 5 6 |
font-style : normal || italic || oblique ; font-variant : normal || small-caps ; font-weight : normal || bold || bolder || || lighter || ( 100 -900 ); font-size : 16px || 1em ; line-height : normal || 16px ; font-family : arial , sans-serif ; |
可以看出font簡寫規則中font-size和font-famil是必寫的,其他的四個屬性可選,四個屬性預設值都為normal,未聲明屬性則應用預設值。
| 1 |
font : italic normal bold 12px / 20px arial , sans-serif ;//個人慣用口絕“型轉粗,大高體” |
6、列表list-style簡寫
估計大家用得最多的一個列表屬性是list-style:none;其實list-style也有三個屬性
| 1 2 3 |
list-style-type : none || disc || circle || square || decimal || lower-alpha || upper-alpha || lower-roman || upper-roman list-style-position : inside || outside || inherit list-style-image : ( url ) || none || inherit |
list-style的預設屬性如下,三個都是可選屬性,未聲明則應用預設值,但是如果聲明list-style-image則
list-style-type無效。
| 1 |
list-style : disc outside none ; |
常用的CSS簡寫規則總結完畢,CSS3樣式屬性簡寫待總結中。