CSS 分类 (Classification) 实例

CSS 分类 (Classification) 实例
CSS 分类属性 (Classification)
CSS 分类属性允许你控制如何显示元素,设置图像显示于另一元素中的何处,相对于其正常位置来定位元素,使用绝对值来定位元素,以及元素的可见度。

属性 描述
clear 设置一个元素的侧面是否允许其他的浮动元素。
cursor 规定当指向某元素之上时显示的指针类型。
display 设置是否及如何显示元素。
float 定义元素在哪个方向浮动。
position 把元素放置到一个静态的、相对的、绝对的、或固定的位置中。
visibility 设置元素是否可见或不可见。

1.如何把元素显示为内联元素
本例演示如何把元素显示为内联元素。
<style type="text/css">
p{display:inline}
div{display:none}
</style>
</head>
<body>
<p>本实例得样式表把段落元素设置为内联元素。</p>
<br/>
<p>而div 元素不会显示出来</p>
<br/>
<div>div 元素的内容不会显示出来!</div>
</body>

2.如何把元素显示为块级元素
本例演示如何把元素显示为块级元素。
<style>
span{display:block}
</style>
</head>
<body>
<span>本实例中的样式表把span元素设置为块级元素。</span>
<span>两个span 元素之间产生了一个换行行为。</span>
</body>

3.float 属性的简单应用
使图像浮动于一个段落的右侧。

<style type="text/css">
img{float:right}
</style>
</head>
<body>
<p>在下面的段落中,我们添加了一个样式为 <b>float:right</b> 的图像。结果是这个图像会浮动到段落的右侧。</p>
<p>
<img src="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>

4.将带有边框和边界的图像浮动于段落的右侧
使图像浮动于段落的右侧。向图像添加边框和边界。
<style type="text/css">
img{float:right;
border:1px dotted black;
margin:0px 15px 20px;

}
</style>
</head>
<body>
<p>在下面的段落中,图像会浮动到右侧,并且添加了点状的边框。我们还为图像添加了边距,这样就可以把文本推离图像:
上和右外边距是 0px,下外边距是 15px,而图像左侧的外边距是 20px</p>
<p>
<img src="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>

5.带标题的图像浮动于右侧
使带有标题的图像浮动于右侧
<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="eg_cute.gif">
</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>

6.使段落的首字母浮动于左侧
使段落的首字母浮动于左侧,并向这个字母添加样式。

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

7.创建水平菜单
使用具有一栏超链接的浮动来创建水平菜单。

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

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>

8.创建无表格的首页
使用浮动来创建拥有页眉、页脚、左侧目录和主体内容的首页。
<style type="text/css">
div.container{
width:100%;
margin:0px;
border:1px;
line-herght:150%;
}
div.header,div.footer{
color:white;

clear:left;
}
h1.header{
padding:0;
margin:0;
}
div.left{
float:left;
width:160px;
margin:0px;
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">zoulixiang.com</h1>
</div>
<div class="left">
<p>"Never increasre,beyond what is ne"</p>
</div>
<div class="content">
<h2>Fre web</h2>
<p>At zoulixiang.com</p>
</div>
<div class="footer">Copy right</div>
</div>

</body>
</html>

9.定位:相对定位
本例演示如何相对于一个元素的正常位置来对其定位。
<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>

10.定位:绝对定位
本例演示如何使用绝对值来对元素进行定位。
<style type="text/css">
h2.pos_abs{
position:absolute;
left:100px;
top:150px;
}
</style>
</head>
<body>
<h2 class="pos_abs">这个是带有绝对定位的标题</h2>
<p>t通过绝对定位,元素可以放置到页面的任何位置。下面的标题距离页面左侧 100px, 距离页面顶部150px。</p>
</body>

11.定位:固定定位
本例演示如何相对于浏览器窗口来对元素进行定位。
<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>

12.如何使元素不可见
本例演示如何使元素不可见。你希望元素被显示出来,还是不呢?
<style type="text/css">
h1.visible{visibility:visible}
h1.invisible{visibility:hidden}
</style>
</head>
<body>
<h1 class="visible">这是可见的标题</h1>
<h1 class="invisible">这是不可见的标题</h1>
</body>

13.把表格元素设置为 collapse(请在非 IE 的浏览器中查看)
本例演示如何使表格元素叠加?
<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>

14.改变光标
本例演示如何改变光标。
<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: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>

15.清除元素的侧面
本例演示如何使用清除元素侧面的浮动元素。
<style type="text/css">
img{
float:left;
clear:both;
}
</style>
</head>
<body>
<img src="eg_cute.gif"/>
<img src="eg_cute.gif" />

</body>

posted @ 2018-11-19 17:01  Mr.zou  阅读(602)  评论(0编辑  收藏  举报