CSS2快速参考之三 |
3、布局(Layout)属性: 在以前的HTML里,元素的位置只能靠元素的依次排列觉得,而在CSS里你可以更精确的定位元素。Netscape曾提出过Layer标记,它对于精确布局很有好处,但是并没有被W3C承认,W3C在CSS提出了类似于Layer标记的功能。 position属性: position属性用来决定元素的位置类型,详见属性: 属性名称: 'position' 属性值: absolute | relative | static 初始值: static 适合对象: 所有元素 是否继承: no 百分比备注: 被禁止 其属性值分别代表: absolute:屏幕上的绝对位置。 relative:屏幕上的相对位置。 static:固有位置。 direction属性: direction属性决定BOX的排列方向,详见属性: 属性名称: 'direction' 属性值: ltr| rtl 初始值: ltr 适合对象: 所有元素 是否继承: yes 百分比备注: 被禁止 float和clear属性: 在HTML中图片可以选择飘浮的位置,现在BOX对象通过CSS对于也可以选择飘浮的位置。改变BOX的float属性,BOX将飘浮在其他元素的左或右方: 属性名称: 'float' 属性值: left| right|none 初始值: none 适合对象: 所有元素 是否继承: no 百分比备注: 被禁止 例如: <STYLE type="text/css"> IMG { float: left } BODY, P, IMG { margin: 2em } </STYLE> <BODY> <P> <IMG src=img.gif> Some sample text that has no other... </BODY> 相反的,使用clear属性将禁止元素在BOX的左方或右方飘浮: 属性名称: 'clear' 属性值: left| right|both|none 初始值: none 适合对象: 所有元素 是否继承: no 百分比备注: 被禁止 绝对位置属性: 绝对位置属性有四个属性:top、right、bottom和left,属性值为长度单位或百分数: 属性名称: 'top'、'right'、'bottom'、'left' 属性值: <length>|<percentage>|auto 初始值: none 适合对象: 所有元素 是否继承: no 百分比备注: 被禁止 利用以上属性,用户就可以精确定义元素的位置,如: <P style="position: relative; margin-right: 10px; left: 10px;"> I used two red hyphens to serve as a change bar. They will "float" to the left of the line containing THIS <SPAN style="position: absolute; top: auto; left: 0px; color: red;">--</SPAN> word.</P> z-index属性: 在CSS中允许元素的重叠显示,这样就有一个显示顺序的问题,z-index属性描述了元素的前后位置,如果把电脑屏幕看作X-Y平面的话,那么Z轴就是垂直于屏幕的,z-index使用整数表示元素的前后位置,数值越大,就会显示在相对靠前的位置,并且CSS同意在z-index中使用负数。 属性名称: 'z-index' 属性值: auto|<integer> 初始值: auto 适合对象: 使用position属性的元素 是否继承: no 百分比备注: 被禁止 width属性: 规定BOX的width属性,可以使BOX的宽度不依靠它所包含的内容的多少: 属性名称: 'width' 属性值: <length> | <percentage> | auto 初始值: auto 适合对象: 块元素 是否继承: no 百分比备注:根据父元素的width而定 在CSS中还提供了min-width和max-width属性,使得BOX的宽度在最小宽度和最大宽度之间。 属性名称: 'min-width' 属性值: <length> | <percentage> 初始值: 0 适合对象: all 是否继承: no 百分比备注:根据父元素的width而定 属性名称: 'max-width' 属性值: <length> | <percentage> 初始值: 100% 适合对象: all 是否继承: no 百分比备注:根据父元素的width而定 height属性: 相同的BOX还有height属性来控制本身的高度: 属性名称: 'height' 属性值: <length> | <percentage> | auto 初始值: auto 适合对象: 块元素 是否继承: no 百分比备注:根据父元素的height而定 在CSS中还提供了min-height和max-height属性,使得BOX的高度在最小高度和最大高度之间。 属性名称: 'min-height' 属性值: <length> | <percentage> 初始值: 0 适合对象: all 是否继承: no 百分比备注:根据父元素的height而定 属性名称: 'max-height' 属性值: <length> | <percentage> 初始值: 100% 适合对象: all 是否继承: no 百分比备注:根据父元素的height而定 overflow属性: 在规定元素的宽度和高度时,如果元素的面积不足以显示全部内容的话就要用到overflow属性: 属性名称: 'overflow' 属性值: visible | hidden | scroll | auto 初始值: visible 适合对象: 元素的position属性 是否继承: no 百分比备注: 被禁止 属性值含义如下: visible:扩大面积以显示所有内容。 hidden:隐藏超出范围的内容。 scroll:在元素的右边显示一个滚动条。 auto:当内容超出元素面积时,显示滚动条。 clip属性: CSS还提供了一种clip属性,可以把元素区域剪切成各种形状,但目前提供的只有方形一种: 属性名称: 'clip' 属性值: <shape> | auto 初始值: auto 适合元素: 元素的position属性被设为absolute 是否继承: no 百分比备注: 被禁止 <shape>值为rect(top right bottom left)。 line-height和vertical-align属性: line-height属性可以规定元素内部的行间距,使用长度单位或百分数: 属性名称: 'line-height' 属性值: normal | <number> | <length> | <percentage> 初始值: normal 适合对象: 所有元素 是否继承: yes 百分比备注:根据元素的字体大小而定 例如下面的例子,虽然表达方式不同,但结果一样: DIV { line-height: 1.2; font-size: 10pt } DIV { line-height: 1.2em; font-size: 10pt } DIV { line-height: 120%; font-size: 10pt } vertical-align属性决定元素在垂直位置的显示: 属性名称: 'vertical-align' 属性值: baseline | sub | super | top | text-top | middle | bottom | text-bottom | <percentage> | <length> 初始值: baseline 适合对象: inline elements 适合继承: no 百分比备注: 根据元素的line-height属性而定 属性值含义如下: baseline:与元素的基线对齐。 middle:与元素中部对齐。 sub:字下沉。 super:字上升。 text-top:文本顶部对齐。 text-bottom:文本底部对齐。 Top:和本行位置最高元素对齐。 Bottom:和本行位置最低元素对齐。 Visibility属性: 该属性用于控制元素的显示或隐藏: 属性名称: 'visibility' 属性值: inherit | visible | hidden 初始值: inherit 适合对象: 所有元素 是否继承: 如果该值为inherit,则继承父元素属性 百分比备注: 被禁止 4、颜色和背景(Color and Background)属性: 这里介绍有关CSS中前景色和背景颜色、图片的设定方法。 color属性: color属性用于设定元素的前景色: 属性名称: 'color' 属性值: <color> 初始值: 根据用户的初始值而定 适合对象: 所有元素 是否继承: yes 百分比备注: 被禁止 color属性的值可以是十六进制数值、rgb()函数或CSS承认的颜色名称。如: EM { color: red } EM { color: rgb(255,0,0) } 背景属性: background-color属性用于设定背景色,初始值为透明: 属性名称: 'background-color' 属性值: <color> | transparent 初始值: transparent 适合对象: 所有元素 是否继承: no 百分比备注: 被禁止 backgroud-image属性用于设定背景的图片: 属性名称: 'background-image' 属性值: <url> | none 初始值: none 适合对象: 所有元素 是否继承: no 百分比备注: 被禁止 其中url可以为绝对地址,也可以是相对地址,例如: BODY { background-image: url(marble.gif) } P { background-image: none } 以上两个属性利用普通的HTML属性也可以实现,下面的属性是CSS对原有HTML的扩展。 background-repeat属性用来描述背景图片的重复排列方式: 属性名称: 'background-repeat' 属性值: repeat | repeat-x | repeat-y | no-repeat 初始值: repeat 适合对象: 所有元素 是否继承: no 百分比备注: 被禁止 其中属性值的含义为: repeat:沿X轴和Y轴两个方向重复显示图片。 repeat-x:沿X轴方向重复图片。 repeat-y:沿Y轴方向重复图片。 none:不重复图片。 例如: BODY { background: red url(pendant.gif); background-repeat: repeat-y; } /*表示沿Y轴重复图片“pendant.gif”,其余部分以红色为背景色*/ background-attachment属性表示在滚动整个文档时,背景图片的显示方式。它的属性值有两种:fixed和scroll,fixed相当于IE4里的水印效果,也就是说在拖动文档时,背景相对是静止的,scroll则和文档一起滚动。 background-position属性用来指定背景图片显示的位置: 属性名称: 'background-position' 属性值: [<percentage> | <length> ]{1,2} | [top | center | bottom] || [left | center | right] 初始值: 0% 0% 适合对象: 容器元素 是否继承: no 百分比备注: refer to the size of the element itself 其中属性值含义为: “top left”和“left top”表示“0% 0%”。 “top”、“top center”和“center top”表示“50% 0%”。 “right top”和“top right”都表示“100% 0%”。 “left”、“left center”和“center left”表示“0% 50%”。 “center”和“center center”表示“50% 50%”。 “right”、“right center”和“center right”都表示“100% 50%”。 “bottom left”和“left bottom”表示“0% 100%”。 “bottom”、“bottom center”和“center bottom”都表示“50% 100%” “bottom right”和“right bottom”表示“100% 100%”。 例如: BODY { background: url(banner.jpeg) right top } /* 100% 0% */ BODY { background: url(banner.jpeg) top center } /* 50% 0% */ BODY { background: url(banner.jpeg) center } /* 50% 50% */ BODY { background: url(banner.jpeg) bottom } /* 50% 100% */ background属性是以上背景属性的快捷方式,属性和顺序如下: 属性名称: 'background' 属性值: <'background-color'> || <'background-image'> || <'background-repeat'> || <'background-attachment'> || <'background-position'> 适合对象: 所有元素 是否继承: no 百分比备注: 只在background-position中容许使用 |