• 技术支持:13631619545
  • 服务热线:0755-28938063

常用CSS缩写语法总结

分享到微博

使用缩写可以帮助减少你CSS文件的大小,更加容易阅读。css缩写的主要规则如下:

颜色

16进制的色彩值,如果每两位的值相同,可以缩写一半,例如:
#000000可以缩写为#000;#336699可以缩写为#369;

盒尺寸

通常有下面四种书写方法:

  • property:value1; 表示所有边都是一个值value1;
  • property:value1 value2; 表示top和bottom的值是value1,right和left的值是value2
  • property:value1 value2 value3; 表示top的值是value1,right和left的值是value2,bottom的值是value3
  • property:value1 value2 value3 value4; 四个值依次表示top,right,bottom,left

方便的记忆方法是顺时针,上右下左。具体应用在margin和padding的例子如下:
margin:1em 0 2em 0.5em;

边框(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);


CSS简写就是指将多行的CSS属性简写成一行,又称为CSS代码优化或CSS缩写。CSS简写的最大好处就是能够显著减少CSS文件的大小,优化网站整体性能,更加容易阅读。

下面介绍常见的CSS简写规则:

一、盒子大小

这里主要用于两个属性:margin和padding,我们以margin为例,padding与之相同。盒子有上下左右四个方向,每个方向都有个外边距:

1
2
3
4
margin-top:1px;
margin-right:2px;
margin-bottom:3px;
margin-left:4px;
	

你可以简写成:

	
		
		
			
			
				
				
					
					
1
margin:1px2px3px4px;

语法 margin:top right bottom left;

	
		
		
			
			
				
				
					
					
1
2
3
4
5
6
7
8
//四个方向的边距相同,等同于margin:1px1px1px1px;
margin:1px;
//上下边距都为1px,左右边距均为2px,等同于margin:1px2px1px2px;
margin:1px2px;
//右边距和左边距相同,等同于margin:1px2px3px2px;
margin:1px2px3px;
//注意,这里虽然上下边距都为1px,但是这里不能缩写。
margin:1px2px1px3px;

二、边框(border)

边框的属性如下:

1
2
3
border-width:1px;
border-style:solid;
border-color:#000;
	

可以缩写为一句:

1
border:1pxsolid#000;

语法 border:width style color;

三、背景(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:#f00url(background.gif)no-repeatfixed00;

语法是background:color image repeat attachment position; 你可以省略其中一个或多个属性值,如果省略,该属性值将用浏览器默认值,默认值为:

  • color: transparent

  • image: none

  • repeat: repeat

  • attachment: scroll

  • position: 0% 0%

四、字体(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:italicsmall-capsbold1em/140%"Lucida Grande",sans-serif;

注意,如果你缩写字体定义,至少要定义font-size和font-family两个值。

五、列表(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:squareinsideurl(image.gif);

六、颜色(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

七、属性值为0

书写原则是如果CSS属性值为0,那么你不必为其添加单位(如:px/em),你可能会这样写:

	
		
		
			
			
				
				
					
					
1
padding:10px5px0px0px;

试试这样吧:

	
		
		
			
			
				
				
					
					
1
padding:10px5px00;

八、最后一个分号

最后一个属性值后面分号可以不写,如:

	
		
		
			
			
				
				
					
					
1
2
3
4
5
6
#nav{
border-top:4pxsolid#333;
font-style:normal;
font-variant:normal;
font-weight:normal;
}

可以简写成:

	
		
		
			
			
				
				
					
					
1
2
3
4
5
6
#nav{
border-top:4pxsolid#333;
font-style:normal;
font-variant:normal;
font-weight:normal
}

九、字体粗细(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;
}

十、圆角半径(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;

可以简写成:

	
		
		
			
			
				
				
					
					
1
2
3
-moz-border-radius:06px6px;
-webkit-border-radius:06px6px;
border-radius:06px6px;

语法 border-radius:topleft topright bottomright bottomleft