简介
- CSS 指层叠样式表 (Cascading Style Sheets)
- 外部样式表可以极大提高工作效率
- 样式定义如何显示 HTML 元素
- 外部样式表通常存储在 CSS 文件中
- 多个样式定义可层叠为一
- 把样式添加到 HTML 4.0 中,是为了解决内容与表现分离的问题
选择器
<html>
<head>
<title>This is the title</title>
<style type="text/css">
h1{ #元素选择器
color:red;#定义前景色
background-color:yellow;
}
#content{ #id选择器
color:black:
background-color:white;
}
.content{ #类选择器
color:blue;
background-color:green;
font-size:16px;
}
a[href]{ #属性选择器
color:darkbule;
font-size:23px;
}
h2 em{ #后代选择器
color:oldlace;
}
h3 >l{ #子元素选择器
color:orange;
border:green solid 1px; #dashed为实心边框
}
li + p{ #相邻兄弟选择器
font-weight:bold;
outline:greenyellow dotted 5px;#轮廓
}
ul{
list-style-type:none; #去掉列表的实心圆
}
</style>
</head>
<body>
<h1>This is the content</h1>
<h2 id="content">This is the another content</h1>
<h2 class="content">This is the another content</h1>
<a href>This is the another content</a>
<h2><em>still a content</em></h2>
<h3>Again <em>is </em>a <l>the </l>content</h3>
<ul>
<li>Last a the content</li>
<p>Last a the content</p>
<li>Lat a the content</li>
<p>Last a the content</p>
</ul>
<p>Last a the content</p>
</body>
</html>
伪元素
CSS 伪元素用于向某些选择器设置特殊效果。
<html>
<head>
<title>This is a title</title>
<style type="text/css">
h3#id:first-letter{
color:yellowgreen;
font-size:xx-large;
}
h3.line:first-line{
color:red;
}
</style>
</head>
<body>
<h3 id="id">
You can use the:first-letter change first letter<br>
</h3>
<h3 class="line">
You can use the:first-line<br>
change first line
</h3>
</body>
</html>
伪类
CSS 伪类用于向某些选择器添加特殊的效果。
<html>
<head>
<title>This again title</title>
<style>
a:link{color:#0000FF} #链接
a:visited{color:black} #已经访问
a:hover{color:green} #悬停
a:active{color:greenyellow} #选定
</style>
</head>
<body>
<a href="https://me.csdn.net/weixin_45131319">This is a link</a>
</body>
</html>
盒子模型
CSS盒模型本质上是一个盒子,封装周围的HTML元素,它包括:外间距,边框,内间距,和HTML元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>still is a title</title>
<style>
div {
background-color: lightgrey;
width: 300px;
border: 25px solid green;
padding: 25px;
margin: 25px;
}
</style>
</head>
<body>
<h2>盒子模型演示</h2>
<div>这里是盒子内的实际内容。有 25px 内间距,25px 外间距、25px 绿色边框。</div>
</body>
</html>
表格
CSS 表格属性可以帮助您极大地改善表格的外观
<html>
<head>
<title>This again title</title>
<style >
*{
margin:0px; #外边距为0
}
table{
border-collapse:collapse; #折叠表格
}
table,th,td{
border:darkgray solid 1px; #边框为深灰色
}
table{
width:100%; #使表格铺满浏览器
}
th{
height:50px;
}
td{
text-align:center; #居中
height:50px;
background-color:black;
color:white
}
</style>
</head>
<body>
<table>
<tr>
<th>班级</th><th>姓名</th><th>年龄</th>
</tr>
<td>大班</td><td>小明</td><td>16</td>
</table>
</body>
</html>
定位
CSS 定位 (Positioning) 属性允许你对元素进行定位。
绝对定位
元素会脱离文档流,如果设置偏移量,会影响其他元素的位置定位
<html>
<head>
<title>This again title</title>
<style >
#left{
width:200px;
height:200px;
background-color:green;
position:absolute;
}
#right{
width:150px;
height:150px;
background-color:red;
position:absolute;
}
</style>
</head>
<body>
<div id="left"></div>
<div id="right"></div>
</body>
</html>
相对定位
相对于原来位置移动,元素设置此属性之后仍然处在文档流中,不影响其他元素的布局
<html>
<head>
<title>This again title</title>
<style >
#left{
width:200px;
height:200px;
background-color:green;
position:absolute;
}
#right{
width:150px;
height:150px;
background-color:red;
position:absolute;
top:20px;
left:20px;
}
</style>
</head>
<body>
<div id="left"></div>
<div id="right"></div>
</body>
</html>
浮动
- 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
- 由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样。
<html>
<head>
<title>This again title</title>
<style >
#left{
width:200px;
height:200px;
background-color:green;
float:left;
}
#right{
width:200px;
height:200px;
background-color:red;
float:right;
}
</style>
</head>
<body>
<div id="left"></div>
<div id="right"></div>
</body>
</html>