页面布局的三大核心:
盒子模型本质还是一个盒子,包括边框border、外边距margin、内边距padding和实际内容content

组成:颜色border-color、边框宽度border-width、样式(实线、虚线)border-style
| 属性 | 作用 |
|---|---|
| border-width | 边框粗细,单位px |
| border-style | 边框样式,none\solid\dashed\dotted |
| border-color | 边框颜色 |
边框的复合写法:
没有先后顺序
border:1px solid red;
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
div{
width: 100px;
height: 100px;
}
.div1{
border-width: 2px;
border-color: red;
border-style: dashed;
}
.div2{
border:2px green solid;
}
style>
head>
<body>
<div class="div1">div>
<div class="div2">div>
body>
html>

top\left\right\bottom
border-top:1px solid red;//只设置上边框
练习:
给一个200*200的盒子,设置上边框为红色,其余边框为蓝色
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
div{
width: 200px;
height: 200px;
}
.div2{
border:2px blue solid;
border-top:red 2px solid;
}
style>
head>
<body>
<div class="div2">div>
body>
html>

border-collapse:collapse;//表示相邻边框合并在一起
练习:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
table{
width: 400px;
height: 100px;
}
th{
height: 25px;
}
table,
td,
th {
border: 1px red solid;
border-collapse: collapse;
font-size: 14px;
text-align: center;
}
style>
head>
<body>
<table align="center" cellspacing="0">
<thead>
<tr>
<th>排名th>
<th>关键词th>
<th>趋势th>
<th>今日搜索th>
tr>
thead>
<tbody>
<tr>
<td>1td>
<td>西游记td>
<td>uptd>
<td>456td>
tr>
<tr>
<td>2td>
<td>乌合之众td>
<td>uptd>
<td>333td>
tr>
<tr>
<td>3td>
<td>三体td>
<td>downtd>
<td>123td>
tr>
tbody>
table>
body>
html>

因此要保证盒子实际做出来的效果与效果图一致:
测量效果图边框时,不量边框。或者加上边框减去边框的距离
用来设置内边距,即边框与内容之间的距离
padding-top:上内边距
padding-bottom:下内边距
padding-left:左内边距
padding-right:右内边距
练习:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
div{
width: 200px;
height: 200px;
background-color: pink;
padding-top: 20px;
padding-left: 20px;
padding-bottom: 20px;
padding-right: 20px;
}
style>
head>
<body>
<div>盒子模型的内容contentdiv>
body>
html>

顺序:上右下左
padding:5px;//上下左右
padding:5px 10px;//上下是5,左右是10px
padding:5px 10px 20px;//上-5,左右-10,下-20
padding:5px 10px 20px 30px;//上-5,右-10,下-20 左30
如果盒子已经有了宽度和高度设置,指定padding内边距时,会撑大盒子
因此要保证盒子实际做出来的效果与效果图一致,需要让width、height减去多出来的内边距
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
div{
width: 200px;
height: 200px;
background-color: pink;
padding-top: 20px;
padding-left: 20px;
padding-bottom: 20px;
padding-right: 20px;
}
style>
head>
<body>
<div>盒子模型的内容contentdiv>
body>
html>

应用:
导航栏里字数不一样多,如果给每个盒子设置一样的宽度,就会导致相邻两个盒子中内容之间的距离可能有的大有的小,看起来不美观。可以采用padding
给盒子设置相同宽度:

练习:新浪导航
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.nav {
border-top: 3px solid #ff8500;
border-bottom: 1px solid #edeef0;
height: 41px;
background-color: #fcfcfc;
line-height: 41px;
}
.nav a{
display: inline-block;
height: 41px;
padding: 0 20px;
font-size: 12px;
text-decoration: none;
color:#4c4c4c;
}
.nav a:hover{
color: #ff8500;
background-color: #eee;
}
style>
head>
<body>
<div class="nav">
<a href="#">新浪导航a>
<a href="#">手机新浪端a>
<a href="#">移动客户端a>
<a href="#">微博a>
<a href="#">看微博a>
div>
body>
html>

如果盒子本身没有指定width/height属性,padding可以改变元素的宽度和高度
1、
宽度:没有设置时,可以继承父类的宽度(不受padding影响)
高度:不能继承父类高度(由padding控制)
父类设置宽度300,高度200。
子类没有设置宽高,设置了padding,宽度是300,高度=padding*2
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
div{
width: 300px;
height: 200px;
background-color: #3e3e3e;
}
p{
background-color: green;
/* height:100px;
width: 100%; */
padding: 20px;
}
style>
head>
<body>
<div>
<p>p>
div>
body>
html>

2、
宽度:子类设置宽度时,取设置的宽度+padding2(受padding影响)
高度:子类设置高度,取设置的高度+padding2
父类设置宽度300,高度200。
子类没有设置宽高,设置了padding,宽度是300,高度=padding*2
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
div{
width: 300px;
height: 200px;
background-color: #3e3e3e;
}
p{
background-color: green;
width: 100%;
padding: 20px;
}
style>
head>
<body>
<div>
<p>p>
div>
body>
html>

有内容:
宽度:不受width属性的限制,宽度由行内元素内容和padding决定。宽度=行内元素宽度+padding*2
高度:不受height属性的限制,有默认高度,且可以由line-height和font-size改变高度
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
body{
width: 500px;
height: 500px;
}
span{
padding: 20px;
background-color: green;
font-size: 30px;
}
a{
background-color: coral;
padding: 0px 30px;
font-size: 10px;
}
style>
head>
<body>
<span>有内容span>
<a href="#">有内容a>
body>
html>

没有内容:
宽度:不受width属性的限制,没有内容时。宽度=padding2
高度:不受height属性的限制,有默认高度,没有设置line-height和font-size时,高度=padding2+默认高度
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
body{
width: 500px;
height: 500px;
}
span{
padding: 20px;
background-color: green;
}
a{
background-color: coral;
padding: 0px 30px;
}
style>
head>
<body>
<span>span>
<a href="#">a>
body>
html>
