博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

常用CSS语法总结

Posted on 2007-01-23 16:27  .NET酷  阅读(535)  评论(0编辑  收藏  举报

1、常用缩写

颜色

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);

2、简要说明
(1)CSS背景:background-repeat
语法:
background-repeat : repeat | no-repeat | repeat-x | repeat-y
取值:
repeat  : 默认值。背景图像在纵向和横向上平铺
no-repeat  : 背景图像不平铺
repeat-x  : 背景图像仅在横向上平铺
repeat-y  : 背景图像仅在纵向上平铺

说明:
设置或检索对象的背景图像是否及如何铺排。必须先指定对象的背景图像( background-image )。
此属性对于 currentStyle 对象而言是只读的。对于其他对象而言是可读写的。
对应的脚本特性为 backgroundRepeat 。
示例:
menu { background: url("images/aardvark.gif"); background-repeat: repeat-y; }
p { background: url("images/aardvark.gif"); background-repeat: no-repeat; }

(2)CSS背景:background-position
语法:
background-position : length || length
background-position : position || position
取值:
length  : 百分数 | 由浮点数字和单位标识符组成的长度值。请参阅 长度单位 
position  : top | center | bottom | left | center | right

说明:
设置或检索对象的背景图像位置。必须先指定 background-image 属性。
该属性定位不受对象的补丁属性( padding )设置影响。
默认值为: 0% 0% 。此时背景图片将被定位于对象不包括补丁( padding )的内容区域的左上角。
如果只指定了一个值,该值将用于横坐标。纵坐标将默认为 50% 。如果指定了两个值,第二个值将用于纵坐标。
如果设置值为 right center ,因为 right 作为横坐标值将会覆盖 center 值,所以背景图片将被居右定位。
对应的脚本特性为 backgroundPosition 。
示例:
div { background: url("images/aardvark.gif"); background-position: 35% 80%; }
menu { background: url("images/aardvark.gif"); background-position: 35% 2.5cm; }
a { background: url("images/aardvark.gif"); background-position: 3.25in; }
body { background: url("images/aardvark.gif"); background-position: top right; } 

(3)CSS背景:background-position-x
语法: background-position-x : length | left | center | right 取值: length : 百分数 | 由浮点数字和单位标识符组成的长度值。请参阅 长度单位 left : 居左 center : 居中 right : 居右 说明:设置或检索对象的背景图像横坐标位置。必须先指定 background-image 属性。该属性定位不受对象的补丁属性( padding )设置影响。默认值为: 0% 。此属性对于 currentStyle 对象而言是只读的。对于其他对象而言是可读写的。对应的脚本特性为 backgroundPositionX 。 示例: p { background-image: url("images/aardvark.gif"); background-position-x: 35%; background-repeat:no-repeat; }

(4)CSS背景:background-position-y
语法:
background-position-y : length | top | center | bottom
取值:
length  : 百分数 | 由浮点数字和单位标识符组成的长度值。请参阅 长度单位 
top  : 居顶
center  : 居中
bottom  : 居底

说明:
设置或检索对象的背景图像纵坐标位置。必须先指定 background-image 属性。
该属性定位不受对象的补丁属性( padding )设置影响。
默认值为: 0% 。
此属性对于 currentStyle 对象而言是只读的。对于其他对象而言是可读写的。
对应的脚本特性为 backgroundPositionY 。
示例:
div { background-image: url("images/aardvark.gif"); background-position-y: 35%; background-repeat:no-repeat; }

(5)CSS背景:background-attachment
语法:
background-attachment : scroll | fixed
取值:
scroll  : 默认值。背景图像是随对象内容滚动
fixed  : 背景图像固定

说明:
设置或检索背景图像是随对象内容滚动还是固定的。
此属性对于 currentStyle 对象而言是只读的。对于其他对象而言是可读写的。
请参阅 bgProperties 属性(特性)。对应的脚本特性为 backgroundAttachment 。
示例:
html { background-image: url("anasazi.tif"); background-attachment: fixed; }
body { background-attachment: scroll; }