10个CSS简写/优化技巧
<div><p>CSS简写就是指将多行的CSS属性简写成一行,又称为CSS代码优化或CSS缩写。CSS简写的最大好处就是能够显著减少CSS文件的大小,优化网站整体性能,更加容易阅读。</p><p>下面介绍常见的CSS简写规则:</p><div style="page-break-after: always;"><span style="display: none;"><!--more-->& nbsp ;</span></div><h4>一、盒子大小</h4><p>这里主要用于两个属性:margin和padding,我们以margin为例,padding与之相同。盒子有上下左右四个方向,每个方向都有个外边距:</p><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> <div>4</div> </td> <td> <div> <div>margin-top
:
1px
;
</div> <div>margin-right
:
2px
;
</div> <div>margin-bottom
:
3px
;
</div> <div>margin-left
:
4px
;
</div> </div> </td> </tr> </tbody></table><pre> </pre><p>你可以简写成:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> </td> <td> <div> <div>margin
:
1px
2px
3px
4px
;
</div> </div> </td> </tr> </tbody></table><p>语法 margin:top right bottom left;</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> </td> <td> <div> <div>//四个方向的边距相同,等同于
margin
:
1px
1px
1px
1px
;
</div> <div>margin
:
1px
;
</div> <div>//上下边距都为
1px
,左右边距均为
2px
,等同于
margin
:
1px
2px
1px
2px
;
</div> <div>margin
:
1px
2px
;
</div> <div>//右边距和左边距相同,等同于
margin
:
1px
2px
3px
2px
;
</div> <div>margin
:
1px
2px
3px
;
</div> <div>//注意,这里虽然上下边距都为
1px
,但是这里不能缩写。
</div> <div>margin
:
1px
2px
1px
3px
;
</div> </div> </td> </tr> </tbody></table><p>二、边框(border)</p><p>边框的属性如下:</p><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> </td> <td> <div> <div>border-width
:
1px
;
</div> <div>border-style
:
solid
;
</div> <div>border-color
:
#000
;
</div> </div> </td> </tr> </tbody></table><pre> </pre><p>可以缩写为一句:</p><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> </td> <td> <div> <div>border
:
1px
solid
#000
;
</div> </div> </td> </tr> </tbody></table><p>语法 border:width style color;</p><h4>三、背景(Backgrounds)</h4><p>背景的属性如下:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </td> <td> <div> <div>background-color
:
#f00
;
</div> <div>background-image
:
url
(background.gif);
</div> <div>background-repeat
:
no-repeat
;
</div> <div>background-attachment
:
fixed
;
</div> <div>background-position
:
00
;
</div> </div> </td> </tr> </tbody></table><p>可以缩写为一句:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> </td> <td> <div> <div>background
:
#f00
url
(background.gif)
no-repeat
fixed
0
0
;
</div> </div> </td> </tr> </tbody></table><p>语法是background:color image repeat attachment position;
你可以省略其中一个或多个属性值,如果省略,该属性值将用浏览器默认值,默认值为:</p><ul> <li> <p>color: transparent</p> </li> <li> <p>image: none</p> </li> <li> <p>repeat: repeat</p> </li> <li> <p>attachment: scroll</p> </li> <li> <p>position: 0% 0%</p> </li></ul><h4>四、字体(fonts)</h4><p>字体的属性如下:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </td> <td> <div> <div>font-style
:
italic
;
</div> <div>font-variant
:
small-caps
;
</div> <div>font-weight
:
bold
;
font-size
:
1em
;
</div> <div>line-height
:
140%
;
</div> <div>font-family
:
"Lucida Grande"
,
sans-serif
;
</div> </div> </td> </tr> </tbody></table><p>可以缩写为一句:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> </td> <td> <div> <div>font
:
italic
small-caps
bold
1em
/
140%
"Lucida Grande"
,
sans-serif
;
</div> </div> </td> </tr> </tbody></table><p>注意,如果你缩写字体定义,至少要定义font-size和font-family两个值。</p><h4>五、列表(lists)</h4><p>取消默认的圆点和序号可以这样写list-style:none;,
list的属性如下:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> </td> <td> <div> <div>list-style-type
:
square
;
</div> <div>list-style-position
:
inside
;
</div> <div>list-style-image
:
url
(image.gif);
</div> </div> </td> </tr> </tbody></table><p>可以缩写为一句:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> </td> <td> <div> <div>list-style
:
square
inside
url
(image.gif);
</div> </div> </td> </tr> </tbody></table><p>六、颜色(Color)</p><p>16进制的色彩值,如果每两位的值相同,可以缩写一半。例如:</p><p>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</p><h4>七、属性值为0</h4><p>书写原则是如果CSS属性值为0,那么你不必为其添加单位(如:px/em),你可能会这样写:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> </td> <td> <div> <div>padding
:
10px
5px
0px
0px
;
</div> </div> </td> </tr> </tbody></table><p>试试这样吧:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> </td> <td> <div> <div>padding
:
10px
5px
00
;
</div> </div> </td> </tr> </tbody></table><p>八、最后一个分号</p><p>最后一个属性值后面分号可以不写,如:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> </td> <td> <div> <div>#nav{
</div> <div>border-top
:
4px
solid
#333
;
</div> <div>font-style
: normal
;
</div> <div>font-variant
:
normal
;
</div> <div>font-weight
: normal
;
</div> <div>}
</div> </div> </td> </tr> </tbody></table><p>可以简写成:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> </td> <td> <div> <div>#nav{
</div> <div>border-top
:
4px
solid
#333
;
</div> <div>font-style
: normal
;
</div> <div>font-variant
:
normal
;
</div> <div>font-weight
: normal
</div> <div>}
</div> </div> </td> </tr> </tbody></table><p>九、字体粗细(font-weight)</p><p>你可能会这样写:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> </td> <td> <div> <div>h
1
{
</div> <div>font-weight
:
bold
;
</div> <div>}
</div> <div>p{
</div> <div>font-weight
:
normal
;
</div> <div>}
</div> </div> </td> </tr> </tbody></table><p>可以简写成:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> </td> <td> <div> <div>h
1
{
</div> <div>font-weight
:
700
;
</div> <div>}
</div> <div>p{
</div> <div>font-weight
:
400
;
</div> <div>}
</div> </div> </td> </tr> </tbody></table><h4>十、圆角半径(border-radius)</h4><p>border-radius是css3中新加入的属性,用来实现圆角边框。</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> </td> <td> <div> <div>-moz-border-radius-bottomleft:
6px
;
</div> <div>-moz-border-radius-topleft:
6px
;
</div> <div>-moz-border-radius-topright:
6px
;
</div> <div>-webkit-border-bottom-left-radius:
6px
;
</div> <div>-webkit-border-top-left-radius:
6px
;
</div> <div>-webkit-border-top-right-radius:
6px
;
</div> <div>border-bottom-left-radius:
6px
;
</div> <div>border-top-left-radius:
6px
;
</div> <div>border-top-right-radius:
6px
;
</div> </div> </td> </tr> </tbody></table><p>可以简写成:</p><pre> </pre><table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <div>1</div> <div>2</div> <div>3</div> </td> <td> <div> <div>-moz-border-radius:
0
6px
6px
;
</div> <div>-webkit-border-radius:
0
6px
6px
;
</div> <div>border-radius:
0
6px
6px
;
</div> </div> </td> </tr> </tbody></table><p>语法 border-radius:topleft topright bottomright bottomleft</p></div>