前言
本文将以一个盒子模型的案例来完成对CSS相关知识的总结
以下是本篇文章正文内容,内容为个人见解,仅供参考。
概念图及效果图
实操
- 要完成一个复杂的盒子模型可以进行分步处理,首先可以绘制最外层边框
/*样式*/
<style type="text/css">
.First{
width:1690px;
height: 900px;
border: solid 10px grey;
margin: 0 auto;
background-color: khaki;
position: absolute;
left: 50%;
transform:translate(-50%);
}
</style>
/*代码*/
<div class="First"></div>
- 然后划分左右两大区域
/*样式*/
<style type="text/css">
.SecondLeft{
float: left;
width: 530px;
height: 860px;
margin: 20px 0 20px 20px;
background-color: #fff;
}
.SecondRight{
float: right;
width: 1100px;
height: 860px;
margin: 20px;
background-color: #fff;
}
</style>
/*代码*/
<div class="SecondLeft"></div>
<div class="SecondRight"></div>
- 接下来细分左边的上下区域
/*样式*/
<style type="text/css">
.SLtop{
width: 528px;
height: 228px;
border: 1px solid black;
background-color: green;
}
.SLbottom{
width: 528px;
height: 608px;
border: 1px solid black;
margin-top: 20px;
background-color: green;
}
</style>
/*代码*/
<div class="SLtop"></div>
<div class="SLbottom"></div>
- 再次将右边上下区域进行划分
/*样式*/
<style type="text/css">
.SRtop{
width: 1098px;
height: 328px;
border: 1px solid black;
background-color: green;
margin-bottom: 20px;
}
.SRbottom{
width: 1100px;
height: 510px;
background-color:violet;
}
</style>
/*代码*/
<div class="SRtop"></div>
<div class="SRbottom"></div>
- 再将右下的区域划分左右
/*样式*/
<style type="text/css">
.SRBleft{
float: left;
width: 538px;
height: 508px;
border: 1px solid black;
margin-right: 20px;
background-color: green;
position: absolute;
}
.SRBright{
float: right;
width: 540px;
height: 510px;
background-color:cornflowerblue;
}
</style>
/*代码*/
<div class="SRBleft"></div>
<div class="SRBright"></div>
- 最后将蓝色区域划分上下
/*样式*/
<style type="text/css">
.SRBRtop{
width: 538px;
height: 243px;
border: 1px solid black;
background-color: green;
margin-bottom: 20px;
}
.SRBRbottom{
width: 538px;
height: 243px;
border: 1px solid black;
background-color: green;
}
</style>
/*代码*/
<div class="SRBRtop"></div>
<div class="SRBRbottom"></div>
- 7号盒子的设置可以用相对定位来定位,其并未脱离标准文档流
/*样式*/
<style type="text/css">
.seven{
width: 198px;
height: 148px;
border: 1px solid black;
background-color: orange;
position: relative;
left: 50px;
top: 75px;
}
</style>
/*代码*/
<div class="seven"></div>
- 8号盒子覆盖于所有盒子之上,需要用绝对定位来设置,该种方式会脱离标准文档流
/*样式*/
<style type="text/css">
.eight{
width: 248px;
height: 148px;
border: 1px solid black;
background-color: orange;
position: absolute;
top: -50px;
left: 1400px;
}
</style>
/*代码*/
<div class="eight"></div>
- 9号盒子也对部分盒子进行了压盖,所以也要进行绝对定位,但是注意其也被其他盒子压盖,所以要注意盒子的放置顺序
/*样式*/
<style type="text/css">
.nine{
width: 248px;
height: 148px;
border: 1px solid black;
background-color: orange;
position: absolute;
bottom: -50px;
left: 640px;
}
</style>
/*代码*/
<div class="nine"></div>
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>盒子模型</title>
<style type="text/css">
.First{
width:1690px;
height: 900px;
border: solid 10px grey;
margin: 0 auto;
background-color: khaki;
position: absolute;
left: 50%;
transform:translate(-50%);
}
.SecondLeft{
float: left;
width: 530px;
height: 860px;
margin: 20px 0 20px 20px;
background-color: #fff;
}
.SLtop{
width: 528px;
height: 228px;
border: 1px solid black;
background-color: green;
}
.SLbottom{
width: 528px;
height: 608px;
border: 1px solid black;
margin-top: 20px;
background-color: green;
}
.SecondRight{
float: right;
width: 1100px;
height: 860px;
margin: 20px;
background-color: #fff;
}
.SRtop{
width: 1098px;
height: 328px;
border: 1px solid black;
background-color: green;
margin-bottom: 20px;
}
.SRbottom{
width: 1100px;
height: 510px;
background-color:violet;
}
.SRBleft{
float: left;
width: 538px;
height: 508px;
border: 1px solid black;
margin-right: 20px;
background-color: green;
position: absolute;
}
.SRBright{
float: right;
width: 540px;
height: 510px;
background-color:cornflowerblue;
}
.SRBRtop{
width: 538px;
height: 243px;
border: 1px solid black;
background-color: green;
margin-bottom: 20px;
}
.SRBRbottom{
width: 538px;
height: 243px;
border: 1px solid black;
background-color: green;
}
.seven{
width: 198px;
height: 148px;
border: 1px solid black;
background-color: orange;
position: relative;
left: 50px;
top: 75px;
}
.eight{
width: 248px;
height: 148px;
border: 1px solid black;
background-color: orange;
position: absolute;
top: -50px;
left: 1400px;
}
.nine{
width: 248px;
height: 148px;
border: 1px solid black;
background-color: orange;
position: absolute;
bottom: -50px;
left: 640px;
}
</style>
</head>
<body>
<div class="First">
<div class="SecondLeft">
<div class="SLtop"></div>
<div class="SLbottom"></div>
</div>
<div class="SecondRight">
<div class="SRtop">
<div class="seven"></div>
</div>
<div class="SRbottom">
<div class="nine"></div>
<div class="SRBleft"></div>
<div class="SRBright">
<div class="SRBRtop"></div>
<div class="SRBRbottom"></div>
</div>
</div>
</div>
</div>
<div class="eight"></div>
</body>
</html>
总结
什么是盒模型?
- css moudle这一术语是用来设计和布局时使用的
- 所有的HTML元素都可以看作是盒子
- 盒模型包括:外边距(margin),边框(border),内填充(padding),实际内容
- 块级元素都具备盒子模型的特征
盒模型的属性和特征
- 内容具有宽高
- 内容的页边距(padding)
代表盒子边框到内容的距离
padding内设置一个值:代表 “上下左右”
padding内设置两个值:代表 “上下” “左右”
padding内设置三个值:代表 “上” “左右” “下”
padding内设置四个值:代表 “上” “右” “下” “左”- 内容的外边距(margin)
margin垂直方向上会出现外边距合并,外边距塌陷,所以在设置时如果在上面设置了margin-bottom,那么在下面就不用设置margin-top- 内容的边框(border)
粗细(width)
样式(style)
颜色(color)
定位
- 静态定位 position:static;
- 相对定位 position:relative;
不脱离标准文档流,可以调整元素
参考点:以原来的位置为参考点- 绝对定位 position:absolute;
脱离标准文档流,不在页面占位置
层级越高的,会出现压盖现象
参考点:相对于最近的非static祖元素定位,如果没有非static祖元素,那么以页面根元素左上角进行定位
推荐使用 “子绝父相” 的布局方法- 固定定位 position:fixed;
脱离标准文档流
一旦设置固定定位,在页面中滚动网页时,其位置固定不变
参考点:以浏览器的左上角为参考
- 作者:CEMER216
- 本文版权归作者和CSDN共有,欢迎转载,且在文章页面明显位置给出原文链接,未经作者同意必须保留此段声明,否则保留追究法律责任的权利。