下面介紹常見的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-width:1px;
border-style:solid;
border-color:#000;
可以縮寫為一句:
border:1px solid #000;
語法 border:width style color;
背景的屬性如下:
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%;
字體的屬性如下:
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 兩個值。
取消默認的圓點和序號可以這樣寫 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);
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
書寫原則是,如果CSS屬性值為0,那么你不必為其添加單位(如 px/em),你可能會這樣寫:
padding:10px 5px 0px 0px;
試試這樣吧:
padding:10px 5px 0 0 ;
最后一個屬性值后面分號可以不寫,如:
#nav{
border-top:4px solid #333;
font-style: normal;
font-variant:normal;
font-weight: normal;
}
可以簡寫成:
#nav{
border-top:4px solid #333;
font-style: normal;
font-variant:normal;
font-weight: normal
}
你可能會這樣寫:
h1{
font-weight:bold;
}
p{
font-weight:normal;
}
可以簡寫成:
h1{
font-weight:700;
}
p{
font-weight:400;
}
border-radius 是 css3 中新加入的屬性,用來實現圓角邊框。
-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;
可以簡寫成:
-moz-border-radius:6px 6px 0;
-webkit-border-radius:6px 6px 0;
border-radius:6px 6px 0;
語法 border-radius:topleft topright bottomright bottomleft;
【 文章來源:懶人圖庫 】
來源: 代碼君(daimajun.com) | 歡迎分享本文,轉載請注明出處!
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com