W3School-CSS 分类 (Classification) 实例

时间:2021-09-16 09:58:00

CSS 分类 (Classification) 实例

CSS 实例

  • CSS 背景实例
  • CSS 文本实例
  • CSS 字体(font)实例
  • CSS 边框(border)实例
  • CSS 外边距 (margin) 实例
  • CSS 内边距 (padding) 实例
  • CSS 列表实例
  • CSS 表格实例
  • 轮廓(Outline)实例
  • CSS 尺寸 (Dimension) 实例
  • CSS 分类 (Classification) 实例
  • CSS 定位 (Positioning) 实例
  • CSS 伪类 (Pseudo-classes)实例
  • CSS 伪元素 (Pseudo-elements)实例

01如何把元素显示为内联元素和块级元素

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>01如何把元素显示为内联元素和块级元素</title>
<style type="text/css">
p.one {
display: inline;
} div {
display: none;
} span {
display: block;
}
</style>
</head> <body>
<p class="one">我这个段落被设置为内联元素。我这个段落被设置为内联元素。我这个段落被设置为内联元素。</p> <p class="one">哥俩好,我也是inline元素!哥俩好,我也是inline元素!哥俩好,我也是inline元素!</p> <div>我不是p而是div,所以内容不会显示出来!</div>
<p>我是一个段落随便写点啥我是一个段落随便写点啥<span>我这个span元素被设置为块级元素。。块级元素。。!!</span>我是一个段落随便写点啥我是一个段落随便写点啥一个段落随便写点啥我是一个段落随便写点啥我是一个段落随便写点啥我是一个段落随便写点啥<span>所以我们两个span元素就换行了。。换行了。。!!</span>我是一个段落随便写点啥我是一个段落随便写点啥我是一个段落随便写点啥</p>
</body> </html>

W3School-CSS 分类 (Classification) 实例


02浮动属性的简单应用

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>02浮动属性的简单应用</title>
<style type="text/css">
img {
float: right;
}
</style>
</head> <body>
<p>
<img src="2.jpg" /> 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>

W3School-CSS 分类 (Classification) 实例


03将带有边框和边界的图像浮动于段落的右侧

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>03将带有边框和边界的图像浮动于段落的右侧</title>
<style type="text/css">
img {
float: right;
border: 1px solid black;
margin: 0px 0px 15px 20px;
}
</style>
</head> <body>
<p>
<img src="2.jpg" /> 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>

W3School-CSS 分类 (Classification) 实例


04带标题的图像浮动于右侧

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>04带标题的图像浮动于右侧</title>
<style type="text/css">
div {
width: 300px;
float: right;
border: 1px solid black;
margin: 0px 0px 15px 20px;
padding: 10px;
text-align: center;
}
</style>
</head> <body>
<div>
<img src="2.jpg" />
<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. 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>

W3School-CSS 分类 (Classification) 实例


05使段落的首字母浮动于左侧

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>05使段落的首字母浮动于左侧</title>
<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. 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. 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>

W3School-CSS 分类 (Classification) 实例


06创建水平菜单

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>06创建水平菜单</title>
<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="#">链接1</a></li>
<li><a href="#">链接2</a></li>
<li><a href="#">链接3</a></li>
<li><a href="#">链接4</a></li>
</ul>
<p>
在上面的例子中,我们把 ul 元素和 a 元素浮向左浮动。li 元素显示为行内元素(元素前后没有换行)。这样就可以使列表排列成一行。ul 元素的宽度是 100%,列表中的每个超链接的宽度是 7em(当前字体尺寸的 7 倍)。我们添加了颜色和边框,以使其更漂亮。
</p>
</body> </html>

W3School-CSS 分类 (Classification) 实例


07创建无表格的首页

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>07创建无表格的首页</title>
<style type="text/css">
div.container {
width: 100%;
border: 3px solid yellow;
margin: 0;
line-height: 150%;
} div.header,
div.footer {
padding: 0.2em;
color: green;
background-color: purple;
clear: left;
} h1.header {
margin: 0;
padding: 0.2em;
} div.left {
float: left;
width: 160px;
padding: 1em;
margin: 0;
} div.content {
margin-left: 200px;
border-left: 1px solid black;
padding: 1em;
}
</style>
</head> <body>
<div class="container"> <div class="header">
<h1 class="header">浣溪沙·一曲新词酒一杯</h1>
</div> <div class="left">
<p>听一支新曲喝一杯美酒,还是去年的天气旧日的亭台,西落的夕阳何时再回来?</p>
</div> <div class="content">
<h2>浣溪沙·一曲新词酒一杯</h2>
<p>一曲新词酒一杯,去年天气旧亭台。夕阳西下几时回?</p>
<p>无可奈何花落去,似曾相识燕归来。小园香径独徘徊。</p>
</div> <div class="footer">此词虽含伤春惜时之意,却实为感慨抒怀之情。</div> </div> </body> </html>

W3School-CSS 分类 (Classification) 实例


08定位:相对定位

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>08定位:相对定位</title>
<style type="text/css">
h2.left {
position: relative;
left: -20px;
} h2.right {
position: relative;
left: 50px;
}
</style>
</head> <body>
<h2>我是位于正常位置的标题,相对定位会按照我的原始位置对我进行移动</h2>
<h2 class="left">样式 "left:-20px" 从我的左侧位置减去 20 像素</h2>
<h2 class="right">样式 "left:50px" 向我的左侧位置增加 50 像素</h2> </body> </html>

W3School-CSS 分类 (Classification) 实例


09定位:绝对定位

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>09定位:绝对定位</title>
<style type="text/css">
h2.abs {
position: absolute;
left: 100px;
top: 150px;
}
</style>
</head> <body>
<h2 class="abs">我是带有绝对定位的标题</h2>
<p>通过绝对定位,元素可以放置到页面上的任何位置。下面的标题距离页面左侧 100px,距离页面顶部 150px。</p>
</body> </html>

W3School-CSS 分类 (Classification) 实例


10定位:固定定位

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>10定位:固定定位</title>
<style type="text/css">
p.one {
position: fixed;
left: 10px;
top: 30px;
} p.two {
position: fixed;
right: 10px;
top: 30px;
}
</style>
</head> <body>
<p class="one">一些文本。</p>
<p class="two">更多的文本。</p>
</body> </html>

W3School-CSS 分类 (Classification) 实例


11如何使元素不可见

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>11如何使元素不可见</title>
<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>

W3School-CSS 分类 (Classification) 实例


12把表格元素设置为collapse

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>12把表格元素设置为 collapse</title>
<style type="text/css">
tr.no {
visibility: collapse;
}
</style>
</head> <body>
<table border="1">
<tr class="yes">
<td>中国