ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# CSS 分类 (Classification) **CSS 分类属性允许你规定如何以及在何处显示元素。** ## CSS分类(Classification)实例: [如何把元素显示为内联元素](/tiy/t.asp?f=csse_display) ``` <html> <head> <style type="text/css"> p {display: inline} div {display: none} </style> </head> <body> <p>本例中的样式表把段落元素设置为内联元素。</p> <p>而 div 元素不会显示出来!</p> <div>div 元素的内容不会显示出来!</div> </body> </html> ``` [如何把元素显示为块级元素](/tiy/t.asp?f=csse_display_block) ``` <html> <head> <style type="text/css"> span { display: block } </style> </head> <body> <span>本例中的样式表把 span 元素设置为块级元素。</span> <span>两个 span 元素之间产生了一个换行行为。</span> </body> </html> ``` [float 属性的简单应用](/tiy/t.asp?f=csse_float) ``` <html> <head> <style type="text/css"> img { float:right } </style> </head> <body> <p>在下面的段落中,我们添加了一个样式为 <b>float:right</b> 的图像。结果是这个图像会浮动到段落的右侧。</p> <p> <img src="/i/eg_cute.gif" /> This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </p> </body> </html> ``` [将带有边框和边界的图像浮动于段落的右侧](/tiy/t.asp?f=csse_float2) ``` <html> <head> <style type="text/css"> img { float:right; border:1px dotted black; margin:0px 0px 15px 20px; } </style> </head> <body> <p>在下面的段落中,图像会浮动到右侧,并且添加了点状的边框。我们还为图像添加了边距,这样就可以把文本推离图像:上和右外边距是 0px,下外边距是 15px,而图像左侧的外边距是 20px。</p> <p> <img src="/i/eg_cute.gif" /> This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </p> </body> </html> ``` [带标题的图像浮动于右侧](/tiy/t.asp?f=csse_float3) ``` <html> <head> <style type="text/css"> div { float:right; width:120px; margin:0 0 15px 20px; padding:15px; border:1px solid black; text-align:center; } </style> </head> <body> <div> <img src="/i/eg_cute.gif" /><br /> CSS is fun! </div> <p> This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </p> <p> 在上面的段落中,div 元素的宽度是 120 像素,它其中包含图像。div 元素浮动到右侧。我们向 div 元素添加了外边距,这样就可以把 div 推离文本。同时,我们还向 div 添加了边框和内边距。 </p> </body> </html> ``` [使段落的首字母浮动于左侧](/tiy/t.asp?f=csse_float4) ``` <html> <head> <style type="text/css"> span { float:left; width:0.7em; font-size:400%; font-family:algerian,courier; line-height:80%; } </style> </head> <body> <p> <span>T</span>his is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </p> <p> 在上面的段落中,文本的第一个字母包含在一个 span 元素中。这个 span 元素的宽度是当前字体尺寸的 0.7 倍。span 元素的字体尺寸是 400%,行高是 80%。span 中的字母字体是 "Algerian" </p> </body> </html> ``` [创建水平菜单](/tiy/t.asp?f=csse_float5) ``` <html> <head> <style type="text/css"> ul { float:left; width:100%; padding:0; margin:0; list-style-type:none; } a { float:left; width:7em; text-decoration:none; color:white; background-color:purple; padding:0.2em 0.6em; border-right:1px solid white; } a:hover {background-color:#ff3300} li {display:inline} </style> </head> <body> <ul> <li><a href="#">Link one</a></li> <li><a href="#">Link two</a></li> <li><a href="#">Link three</a></li> <li><a href="#">Link four</a></li> </ul> <p> 在上面的例子中,我们把 ul 元素和 a 元素浮向左浮动。li 元素显示为行内元素(元素前后没有换行)。这样就可以使列表排列成一行。ul 元素的宽度是 100%,列表中的每个超链接的宽度是 7em(当前字体尺寸的 7 倍)。我们添加了颜色和边框,以使其更漂亮。 </p> </body> </html> ``` [创建无表格的首页](/tiy/t.asp?f=csse_float6) ``` <html> <head> <style type="text/css"> div.container { width:100%; margin:0px; border:1px solid gray; line-height:150%; } div.header,div.footer { padding:0.5em; color:white; background-color:gray; clear:left; } h1.header { padding:0; margin:0; } div.left { float:left; width:160px; margin:0; padding:1em; } div.content { margin-left:190px; border-left:1px solid gray; padding:1em; } </style> </head> <body> <div class="container"> <div class="header"><h1 class="header">W3School.com.cn</h1></div> <div class="left"><p>"Never increase, beyond what is necessary, the number of entities required to explain anything." William of Ockham (1285-1349)</p></div> <div class="content"> <h2>Free Web Building Tutorials</h2> <p>At W3School.com.cn you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP.</p> <p>W3School.com.cn - The Largest Web Developers Site On The Net!</p></div> <div class="footer">Copyright 2008 by YingKe Investment.</div> </div> </body> </html> ``` [定位:相对定位](/tiy/t.asp?f=csse_position_relative) ``` <html> <head> <style type="text/css"> h2.pos_left { position:relative; left:-20px } h2.pos_right { position:relative; left:20px } </style> </head> <body> <h2>这是位于正常位置的标题</h2> <h2 class="pos_left">这个标题相对于其正常位置向左移动</h2> <h2 class="pos_right">这个标题相对于其正常位置向右移动</h2> <p>相对定位会按照元素的原始位置对该元素进行移动。</p> <p>样式 "left:-20px" 从元素的原始左侧位置减去 20 像素。</p> <p>样式 "left:20px" 向元素的原始左侧位置增加 20 像素。</p> </body> </html> ``` [定位:绝对定位](/tiy/t.asp?f=csse_position_absolute) ``` <html> <head> <style type="text/css"> h2.pos_abs { position:absolute; left:100px; top:150px } </style> </head> <body> <h2 class="pos_abs">这是带有绝对定位的标题</h2> <p>通过绝对定位,元素可以放置到页面上的任何位置。下面的标题距离页面左侧 100px,距离页面顶部 150px。</p> </body> </html> ``` [定位:固定定位](/tiy/t.asp?f=csse_position_fixed) ``` <html> <head> <style type="text/css"> p.one { position:fixed; left:5px; top:5px; } p.two { position:fixed; top:30px; right:5px; } </style> </head> <body> <p class="one">一些文本。</p> <p class="two">更多的文本。</p> </body> </html> ``` [如何使元素不可见](/tiy/t.asp?f=csse_visibility) ``` <html> <head> <style type="text/css"> h1.visible {visibility:visible} h1.invisible {visibility:hidden} </style> </head> <body> <h1 class="visible">这是可见的标题</h1> <h1 class="invisible">这是不可见的标题</h1> </body> </html> ``` [把表格元素设置为 collapse(请在非 IE 的浏览器中查看)](/tiy/t.asp?f=csse_visibility_collapse) ``` <html> <head> <style type="text/css"> tr.coll { visibility:collapse } </style> </head> <body> <table border="1"> <tr> <td>Adams</td> <td>John</td> </tr> <tr class="coll"> <td>Bush</td> <td>George</td> </tr> </table> </body> </html> ``` [改变光标](/tiy/t.asp?f=csse_cursor) ``` <html> <body> <p>请把鼠标移动到单词上,可以看到鼠标指针发生变化:</p> <span style="cursor:auto"> Auto</span><br /> <span style="cursor:crosshair"> Crosshair</span><br /> <span style="cursor:default"> Default</span><br /> <span style="cursor:pointer"> Pointer</span><br /> <span style="cursor:move"> Move</span><br /> <span style="cursor:e-resize"> e-resize</span><br /> <span style="cursor:ne-resize"> ne-resize</span><br /> <span style="cursor:nw-resize"> nw-resize</span><br /> <span style="cursor:n-resize"> n-resize</span><br /> <span style="cursor:se-resize"> se-resize</span><br /> <span style="cursor:sw-resize"> sw-resize</span><br /> <span style="cursor:s-resize"> s-resize</span><br /> <span style="cursor:w-resize"> w-resize</span><br /> <span style="cursor:text"> text</span><br /> <span style="cursor:wait"> wait</span><br /> <span style="cursor:help"> help</span> </body> </html> ``` [清除元素的侧面](/tiy/t.asp?f=csse_class-clear) ``` <html> <head> <style type="text/css"> img { float:left; clear:both; } </style> </head> <body> <img src="/i/eg_smile.gif" /> <img src="/i/eg_smile.gif" /> </body> </html> ``` ## CSS 分类属性 (Classification) CSS 分类属性允许你控制如何显示元素,设置图像显示于另一元素中的何处,相对于其正常位置来定位元素,使用绝对值来定位元素,以及元素的可见度。 | 属性 | 描述 | | --- | --- | | [clear](/cssref/pr_class_clear.asp "CSS clear 属性") | 设置一个元素的侧面是否允许其他的浮动元素。 | | [cursor](/cssref/pr_class_cursor.asp "CSS cursor 属性") | 规定当指向某元素之上时显示的指针类型。 | | [display](/cssref/pr_class_display.asp "CSS display 属性") | 设置是否及如何显示元素。 | | [float](/cssref/pr_class_float.asp "CSS float 属性") | 定义元素在哪个方向浮动。 | | [position](/cssref/pr_class_position.asp "CSS position 属性") | 把元素放置到一个静态的、相对的、绝对的、或固定的位置中。 | | [visibility](/cssref/pr_class_visibility.asp "CSS visibility 属性") | 设置元素是否可见或不可见。 |