Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:布局思路很好
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>盒模型基础和box-sizing属性</title>
<style>
.box {
width: 200px;
height: 150px;
}
.box.one {
border: 5px solid black;
background-color: lightgreen;
margin: 10px;
padding: 15px;
float: left;
box-sizing: content-box;
/* 默认盒模型计算公式 = 内容区 + 内边距 + 边框 */
/* 盒子宽度 = width + padding-left + padding-right + border-left + border-right = 200 + 15 + 15 + 5 + 5 = 240 */
}
.box.two {
border: 5px solid black;
background-color: lightblue;
margin: 10px;
padding: 15px;
float: left;
box-sizing: border-box;
/* 自定义盒模型大小: */
/* box-sizing: border-box,此时,盒子宽度等于width,高度等于height,内容区被压缩; */
}
</style>
</head>
<body>
<div class="box one">box1</div>
<div class="box two">box2</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>绝对定位、相对定位、固定定位</title>
<style>
.box {
width: 200px;
height: 200px;
border: 1px solid black;
}
.box.one {
background-color: lightgreen;
/* 固定定位,始终对于当前窗口定位 */
position: fixed;
top: 50px;
left: 100px;
}
.box.two {
background-color: lightblue;
width: 50px;
height: 50px;
/* 绝对定位,以父级包含块为参照物定位(父级参照物必须包含定位属性,否则再往上找寻定位父级,直到body/html) */
position: absolute;
top: 30px;
right: 20px;
}
.box.three {
background-color: lightcyan;
/* 相对定位,相对于自己之前的位置进行定位 */
position: relative;
top: 120px;
left: 10px;
}
</style>
</head>
<body>
<div class="box one">
box1
<div class="box two">box2</div>
</div>
<div class="box three">box3</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>绝对定位实现垂直居中</title>
<style>
* {
margin: 0;
padding: 0;
}
.box.one {
width: 400px;
height: 500px;
border: 5px solid black;
background-color: lightgreen;
position: relative;
}
.box.two {
width: 200px;
height: 200px;
border: 5px dashed black;
background-color: lightcyan;
box-sizing: border-box;
/* 使用绝对定位实现垂直居中。 */
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin-top: auto;
/* margin-right: auto; */
margin-bottom: auto;
/* margin-left: auto; */
/* margin: auto; */
}
</style>
</head>
<body>
<div class="box one">
box1
<div class="box two">box2</div>
</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>使用绝对定位实现二列布局</title>
<style>
* {
margin: 0;
padding: 0;
}
.header,
.footer {
width: 100%;
height: 40px;
background-color: cyan;
}
.container {
width: 1000px;
min-height: 600px;
background-color: #eeeeee;
margin: 10px auto;
position: relative;
}
.left {
width: 480px;
min-height: 600px;
background-color: lightgreen;
position: absolute;
margin-right: 20px;
}
.right {
width: 480px;
min-height: 600px;
background-color: lightgreen;
position: absolute;
right: 0;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="container">
<div class="left">左侧</div>
<div class="right">右侧</div>
</div>
<div class="footer"></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>使用浮动实现三列布局</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header,
.footer {
height: 40px;
background-color: lightgreen;
}
.container {
width: 1000px;
background-color: #eeeeee;
margin: 10px auto;
overflow: hidden;
}
.left,
.right {
width: 200px;
min-height: 600px;
background-color: lightyellow;
}
.main {
width: 600px;
min-height: 600px;
background-color: limegreen;
float: left;
}
.left {
float: left;
}
.right {
float: right;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="container">
<div class="left">左侧</div>
<div class="main">主体</div>
<div class="right">右侧</div>
</div>
<div class="footer"></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>圣杯布局实现三列布局及其思想</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header,
.footer {
height: 40px;
background-color: cyan;
}
.container {
margin: 10px auto;
/* 包裹浮动子元素 */
overflow: hidden;
/* padding挤出两侧区域 */
padding: 0 200px;
}
.container * {
min-height: 600px;
/* 内容区、左右两侧全部浮动 */
float: left;
}
.main {
/* 内容区自适应 */
width: 100%;
background-color: yellow;
}
.left,
.right {
width: 200px;
background-color: cyan;
}
.left {
/* margin负值,回到容器 */
margin-left: -100%;
/* 相对定位,回到正确位置 */
position: relative;
right: 200px;
}
.right {
/* margin负值,回到容器 */
margin-left: -200px;
/* 相对定位,回到正确位置 */
position: relative;
left: 200px;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="container">
<!-- 主体区块优先渲染 -->
<div class="main">主体</div>
<div class="left">左侧</div>
<div class="right">右侧</div>
</div>
<div class="footer"></div>
</body>
</html>