float
):元素脱离标准流,移动到指定位置left
right
span
特性:会环绕图片,文字不会在图片下面<style>
footer {
margin: 0 auto;
width: 800px;
height: 300px;
border: 20px solid black;
padding: 50px;
}
.child1,
.child2 {
width: 100px;
height: 100px;
}
.child1 {
background-color: red;
float: left;
float: right;
}
.child2 {
background-color: blue;
float: right;
float: left;
}
</style>
<body>
<footer>
<div class="child1"></div>
<div class="child2"></div>
</footer>
</body>
<style>
.boxRed,
.boxBlue {
width: 300px;
height: 200px;
}
.boxRed {
background-color: red;
/*红色盒子左浮动 红色盒子在蓝色上面*/
float: left;
}
.boxBlue {
background-color: blue;
font-size: 20px;
text-align: center;
line-height: 200px;
}
</style>
<body>
<section>
<div class="boxRed"></div>
<div class="boxBlue">我是蓝色盒子</div>
</section>
</body>
overflow:hidden;
clear:left
:清除左浮动 clear:right
:清除右浮动::after
content:""
display:blockl
clear:both
overflow:hidden;
overflow:hidden;
的作用溢出的部分隐藏掉
解决嵌套元素外边距
清除浮动
<style>
* {
margin: 0;
padding: 0;
}
header {
width: 600px;
/* height: 100px; */
border: 1px solid black;
overflow: hidden;
}
.child1 {
width: 100px;
height: 100px;
background-color: red;
float: left;
}
.child2 {
width: 100px;
height: 100px;
background-color: blue;
float: right;
}
/*第二种:给父元素添加*/
/* .clearfix::after {
content: '';
display: block;
clear: both;
} */
ul {
width: 300px;
height: 200px;
border: 1px solid red;
font-size: 26px;
/*第三种:添加溢出隐藏*/
overflow: hidden;
}
</style>
<body>
<header class="clearfix">
<div class="child1"></div>
<div class="child2"></div>
<!-- 第一种:添加额外的元素 -->
<!-- <div style="clear: both"></div> -->
</header>
<ul>
<li>第一行</li>
<li>第二行</li>
<li>第三行</li>
<li>第四行</li>
<li>第五行</li>
<li>第六行</li>
<li>第七行</li>
</ul>
</body>
position
relative
、绝对定位:absolute
、固定定位:fixed
、静态定位static
(相当于标准流)left/right/top/bottom
相对于元素本身的位置
没有脱标
占位置
可以超出父元素的范围,只跟原来的位置相关
- 相对定位案例:
<style>
* {
margin: 0;
padding: 0;
}
header {
width: 300px;
height: 200px;
background-color: red;
position: relative;
top:20px;
left:10px;
}
section {
width: 300px;
height: 200px;
background-color: blue;
}
.parent {
margin: 0 auto;
width: 800px;
height: 200px;
border: 30px solid black;
}
.child {
width: 100px;
height: 100px;
background-color: green;
position: relative;
left: 1000px;
top: 300px;
}
</style>
</head>
<body>
<header>我是头部区域</header>
<section>我是主体内容</section>
<div class="parent">
<div class="child"></div>
</div>
</body>
相对于浏览器定位
脱标
不占位置
超出父元素的范围
固定定位案例:
<style>
* {
margin: 0;
padding: 0;
}
header {
width: 300px;
height: 200px;
background-color: red;
position: fixed;
left: 100px;
top: 100px;
bottom: 0px;
position: relative;
left: 100px;
top: 100px;
z-index: 2;
}
section {
position: relative;
width: 300px;
height: 200px;
background-color: blue;
/* 层叠顺序属性 一定要搭配定位同时使用*/
z-index: 3000;
}
.parent {
margin: 0 auto;
width: 800px;
height: 200px;
border: 30px solid black;
/* position: relative; */
}
.child {
width: 100px;
height: 100px;
background-color: green;
/* 固定定位 */
position: fixed;
top: 0;
right: 0;
}
</style>
<body style="height: 2000px">
<header>我是头部区域</header>
<section>我是主体内容</section>
<div class="parent">
<div class="child"></div>
</div>
</body>
固定定位效果图:
综合案例:
<style>
*{
margin: 0;
padding: 0;
}
.box{
/* 距上50px,左右居中 */
margin:50px auto;
width: 310px;
height: 190px;
border: 1px solid #333;
/* padding影响盒子大小 */
padding: 12px;
position: relative;
}
.img1,
.img2{
position: absolute;
}
.img1{
left: 0;
top: 0;
}
.img2{
right: 0;
bottom: 0;
}
</style>
<body>
<div class="box">
<img src="./adv.jpg" alt="">
<img src="./top_tu.gif" alt="" class="img1">
<img src="./br.gif" alt="" class="img2">
</div>
</body>
综合案例效果图:
z-index: 数字;
<style>
* {
margin: 0;
padding: 0;
}
header {
height: 80px;
margin-top: 0 !important;
line-height: 80px;
}
.banner {
height: 40px;
line-height: 40px;
}
.main {
overflow: hidden;
}
footer {
height: 120px;
line-height: 120px;
}
header,
.banner,
footer,
.leftBox,
.rightBox {
border: 1px solid black;
background-color: aqua;
text-align: center;
font-size: 20px;
color: blue;
margin-top: 10px;
}
.leftBox,
.rightBox {
height: 400px;
}
.leftBox {
width: 40%;
float: left;
}
.rightBox {
width: 55%;
float: right;
}
</style>
<body>
<!-- 由外到内,由大到小 -->
<header>top</header>
<section class="banner">banner</section>
<section class="main">
<div class="leftBox">left</div>
<div class="rightBox">right</div>
</section>
<footer>footer</footer>
</body>
布局效果图: