文章目录
一 优先级
1.1 优先级介绍
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
color: aqua;
}
div{
color: green !important;
}
body{
color: red;
}
#box{
color: black;
}
</style>
</head>
<body>
<div class="box" id="box" style="color: pink;">测试优先级</div>
</body>
</html>
1.2 权重叠加计算
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div #one{
color: orange;
}
.father p{
color: blue;
}
.father .son{
color: aqua;
}
div p{
color: pink;
}
</style>
</head>
<body>
<div class="father">
<p class="son" id="one">标签</p>
</div>
</body>
</html>
1.3 拓展:查错流程
二 盒子模型
2.1 盒子模型介绍
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 300px;
height: 300px;
background-color: pink;
/* 边框线 */
border: 1px solid #000;
/* 内边距:内容与边框线之间 */
padding: 20px;
/* 外边距 */
margin: 50px;
}
</style>
</head>
<body>
<div>
你好
</div>
<div>
你好
</div>
</body>
</html>
2.2 内容区域的宽度和高度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 60px;
height: 60px;
background-color: red;
}
</style>
</head>
<body>
<div>pppp</div>
</body>
</html>
2.3 边框(border)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 100px;
height: 100px;
background-color: pink;
/* soild:实线 */
/* dashed:虚线 */
/* dotted:点线 */
border-bottom: 10px red dashed;
text-align: center;
line-height: 100px;
}
</style>
</head>
<body>
<div>lll</div>
</body>
</html>
2.4 内边距(padding)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 200px;
height: 200px;
background-color: pink;
/* padding属性可以当作复合属性来使用,表示单独设置某个方向的内边距 */
/* 最多取4个值 */
/* 上 右 下 左 */
/* padding: 50px; */
/* 上 左右 下 */
/* padding: 10px 10px 66px; */
/* 上下 左右 */
padding: 66px 33px;
}
</style>
</head>
<body>
<div>666</div>
</body>
</html>
2.5 盒子大小计算公式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
/* 撑大盒子的因素: border + padding */
width: 240px;
height: 240px;
background-color: pink;
border: 10px solid #000;
padding: 20px;
}
</style>
</head>
<body>
<div>文字</div>
</body>
</html>
2.6 自动內减
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 100px;
height: 100px;
background-color: pink;
border: 10px solid black;
padding: 20px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div>llll</div>
</body>
</html>
2.7 案例:新浪导航
初版代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
height: 400px;
border-top: 3px solid #ff8500;
border-bottom: 1px solid #edeef0;
}
/* 后代:box里面的a */
.box a{
width: 80px;
height: 40px;
display: inline-block;
text-align: center;
line-height: 40px;
font-size: 12px;
color: #4c4c4c;
text-decoration: none;
}
.box a:hover{
background-color: #edeef0;
color: #ff8400;
}
</style>
<!-- 布局从外到内:先宽高背景色,放内容,调节内容的位置:控制文字细节 -->
</head>
<body>
<div class="box">
<a href="#">百度</a>
<a href="#">百度</a>
<a href="#">百度</a>
<a href="#">百度</a>
</div>
</body>
</html>
修改
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
height: 400px;
border-top: 3px solid #ff8500;
border-bottom: 1px solid #edeef0;
}
/* 后代:box里面的a */
.box a{
padding: 0 16px;
height: 40px;
display: inline-block;
text-align: center;
line-height: 40px;
font-size: 12px;
color: #4c4c4c;
text-decoration: none;
}
.box a:hover{
background-color: #edeef0;
color: #ff8400;
}
</style>
<!-- 布局从外到内:先宽高背景色,放内容,调节内容的位置:控制文字细节 -->
</head>
<body>
<div class="box">
<a href="#">百度</a>
<a href="#">百度666666666</a>
<a href="#">百度</a>
<a href="#">百度</a>
<a href="#">百度</a>
</div>
</body>
</html>
2.8 外边距(margin)
用法与内边距一样
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 100px;
height: 100px;
background-color: pink;
margin: 50px;
}
</style>
</head>
<body>
<div>666</div>
</body>
</html>
2.9 清除默认内外边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
</body>
</html>
2.10 版心居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 1000px;
height: 100px;
background-color: pink;
margin: 0 auto;
}
</style>
</head>
<body>
<div>你好</div>
</body>
</html>
2.11 网页新闻列表案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
/* 所有的标签都可能添加内边距或border,都内减模式 */
box-sizing:
border-box;
}
.news{
width: 500px;
height: 400px;
border: 1px solid #ccc;
margin: 50px auto;
padding: 42px 30px 0;
}
.news h2{
border-bottom: 1px solid #ccc;
font-size: 30px;
/* 行高是1倍,就是字号的大小 */
line-height: 1;
padding-bottom: 9px;
}
/* 去掉点符号 */
ul{
list-style: none;
}
.news li{
height: 50px;
border-bottom: 1px dashed #ccc;
padding-left: 28px;
line-height: 50px;
}
.news a{
text-decoration:
none;
color: #666;
font-size: 18px;
}
</style>
</head>
<body>
<div class="news">
<h2>新闻</h2>
<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>
<li><a href="#">新闻5</a></li>
</ul>
</div>
</body>
</html>
2.12 外边距问题
2.12.1 合并现象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 100px;
height: 100px;
background-color: pink;
}
.one{
margin-bottom: 100px;
}
</style>
</head>
<body>
<div class="one">666</div>
<div class="two">333</div>
</body>
</html>
2.12.2 塌陷问题
2.13 行内元素的垂直内外边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
span{
/* margin:100px */
/* padding:100px */
line-height: 100px;
}
</style>
</head>
<body>
<!-- 行内元素 内外边距 margin padding -->
<!-- 如果想要通过margin或padding改变行内标签的垂直位置,无法生效 -->
<!-- 行内标签的margin-top和bottom 不生效 -->
<!-- 行内标签的padding-top和bottom 不生效 -->
<span>span</span>
<span>span</span>
</body>
</html>