两个经典bug

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Untitled2.css">
  <title>Document</title>
 </head>
 <body>
	 <div class="right"> </div>
	 <div class="left"> </div>
</body>
</html>


*{
  margin:0;
  padding:0;
}

.right{
   position:absolute;
   right:0;
   width:100px;
   height:100px;
   background-color:#fcc;
   opacity:0.5;
}

.left{
   
   height:100px;
   background-color:#123;
}



在这里插入图片描述
该图说明两个图像重叠在一起了,为了不重叠,应改成如下:

*{
  margin:0;
  padding:0;
}

.right{
   position:absolute;
   right:0;
   width:100px;
   height:100px;
   background-color:#fcc;
   opacity:0.5;
}

.left{
   margin-right:100px;
   height:100px;
   background-color:#123;
}


在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Untitled2.css">
  <title>Document</title>
 </head>
 <body>
	 <div class="wrapper">
	 <div class="content"> </div>
	 </div>
</body>
</html>

*{
  margin:0;
  padding:0;
}

.wrapper{
   
   width:100px;
   height:100px;
   background-color:black;
   
  
}

.content{
  
   width:50px;
   height:50px;
   background-color:green;
}


在这里插入图片描述

*{
  margin:0;
  padding:0;
}

.wrapper{
   margin-left:100px;
   margin-top:100px;
   width:100px;
   height:100px;
   background-color:black;
   
  
}

.content{
  
   width:50px;
   height:50px;
   background-color:green;
}


wrapper带着content一起移动
在这里插入图片描述

*{
  margin:0;
  padding:0;
}

.wrapper{
   margin-left:100px;
   margin-top:100px;
   width:100px;
   height:100px;
   background-color:black;
   
  
}

.content{
   margin-left:50px;
   margin-top:50px;
   width:50px;
   height:50px;
   background-color:green;
}



content相对于wrapper水平移动
在这里插入图片描述

*{
  margin:0;
  padding:0;
}

.wrapper{
   margin-left:100px;
   margin-top:100px;
   width:100px;
   height:100px;
   background-color:black;
   
  
}

.content{
   margin-left:50px;
   margin-top:100px;
   width:50px;
   height:50px;
   background-color:green;
}


当content相对于wrapper垂直移动时,不管用
在这里插入图片描述

*{
  margin:0;
  padding:0;
}

.wrapper{
   margin-left:100px;
   margin-top:100px;
   width:100px;
   height:100px;
   background-color:black;
   
  
}

.content{
   margin-left:50px;
   margin-top:150px;
   width:50px;
   height:50px;
   background-color:green;
}


但当content的margin-top大于wrapper的margin-top的值时,整体都向下移动了。

在这里插入图片描述
总结,在有父子关系的元素中,垂直方向的移动是取父子中margin-top中的最大值做变化。就相当于子级元素不以父级的顶部为界限,即父级的顶部相当于没有(即margin塌陷)。如果想要以父级顶部为界限相对移动,可以在父级顶部设一个线,如下:

*{
  margin:0;
  padding:0;
}

.wrapper{
   margin-left:100px;
   margin-top:100px;
   width:100px;
   height:100px;
   background-color:black;
   border-top:1px solid red;
  
}

.content{
   margin-left:50px;
   margin-top:150px;
   width:50px;
   height:50px;
   background-color:green;
}


在这里插入图片描述
这个可以解决问题,但不推荐使用。故使用BFC(block format context块级格式化上下文)是通过改变html元素中的渲染规则。
如何触发一个盒子的BFC.
1.position:absolute;
2.display:inline-block;
3.float:left/right;
4.overflow:hidden;(溢出部分隐藏)

*{
  margin:0;
  padding:0;
}

.wrapper{
   margin-left:100px;
   margin-top:100px;
   width:100px;
   height:100px;
   background-color:black;
 
  
}

.content{
   margin-left:70px;
   
   width:50px;
   height:50px;
   background-color:green;
}


在这里插入图片描述

*{
  margin:0;
  padding:0;
}

.wrapper{
   margin-left:100px;
   margin-top:100px;
   width:100px;
   height:100px;
   background-color:black;
   overflow:hidden;
  
}

.content{
   margin-left:70px;
   
   width:50px;
   height:50px;
   background-color:green;
}


在这里插入图片描述

*{
  margin:0;
  padding:0;
}

.wrapper{
   margin-left:100px;
   margin-top:100px;
   width:100px;
   height:100px;
   background-color:black;
   overflow:hidden;
  
}

.content{
   margin-left:50px;
   margin-top:50px;
   width:50px;
   height:50px;
   background-color:green;
}


通过改变父级的渲染规则,触发其BFC即可达到理想效果,如下:
在这里插入图片描述

*{
  margin:0;
  padding:0;
}

.wrapper{
   margin-left:100px;
   margin-top:100px;
   width:100px;
   height:100px;
   background-color:black;
  display:inline-block;
  
}

.content{
   margin-left:50px;
   margin-top:50px;
   width:50px;
   height:50px;
   background-color:green;
}


效果如上图,其他的方法照样可以解决上述问题。虽然四种方法都可以解决上述问题,但同时也引入了新的问题。
1.position:absolute;(后面元素不能罗列在其下面)
2.display:inline-block;(不能独占一行)
3.float:left/right;
4.overflow:hidden;(溢出部分隐藏)(如果要求是子类部分溢出展示,则达不到要求)

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Untitled2.css">
  <title>Document</title>
 </head>
 <body>
	<span class="box1">123</span>
	<span class="box2">345</span>
</body>
</html>



*{
  margin:0;
  padding:0;
}

.box1{
   margin-right:100px;
   background-color:red;
}

.box2{

   background-color:green;
}

在这里插入图片描述

*{
  margin:0;
  padding:0;
}

.box1{
   margin-right:100px;
   background-color:red;
}

.box2{
   margin-left:150px;
   background-color:green;
}

在这里插入图片描述

上述两张图说明它们之间没有共用区域(是区域累加)。但接下来看看div

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Untitled2.css">
  <title>Document</title>
 </head>
 <body>
	
	<div class="demo1">1</div>
	<div class="demo2">2</div>
</body>
</html>


*{
  margin:0;
  padding:0;
}


.demo1{
   margin-bottom:100px;
   background-color:orange;
}
.demo2{
  
   background-color:black;
}

在这里插入图片描述

*{
  margin:0;
  padding:0;
}


.demo1{
   margin-bottom:100px;
   background-color:orange;
}
.demo2{
   margin-top:100px;
   background-color:black;
}

效果和上一张一样。
上图可以看出它们的区域合并了,这叫margin合并。如何让div和span一样不共用区域呢,可使用BFC,在开发中一般不解决,可使用这个bug隔开两个元素。

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Untitled2.css">
  <title>Document</title>
 </head>
 <body>
	<div class="demo1">1</div>
	<div class="wrapper">
	<div class="demo2">2</div>
	</div>
</body>
</html>

*{
  margin:0;
  padding:0;
}

.demo1{
   margin-bottom:100px;
   background-color:orange;
}
.demo2{
   margin-top:100px;
   background-color:black;
}

.wrapper{
overflow:hidden;
}

在这里插入图片描述
浮动模型(float)
float:left/right

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Untitled2.css">
  <title>Document</title>
 </head>
 <body>
	<div class="wrapper">
	<div class="content">1</div>
	<div class="content">2</div>
	<div class="content">3</div>
	<div class="content">4</div>
	<div class="content">5</div>
	<div class="content">6</div>
	<div class="content">7</div>
	<div class="content">8</div>
	<div class="content">9</div>
	</div>
</body>
</html>



.wrapper{
  width:300px;
  height:300px;
  border:1px solid black;
}
.content{
  
  width:100px;
  height:100px;
  background-color:black;
  color:#fff;
}
.wrapper{
  width:350px;
  height:300px;
  border:1px solid black;
}
.content{
  float:right;
  width:100px;
  height:100px;
  background-color:black;
  color:#fff;
}

在这里插入图片描述

.wrapper{
  width:350px;
  height:300px;
  border:1px solid black;
}
.content{
  float:left;
  width:100px;
  height:100px;
  background-color:black;
  color:#fff;
}

在这里插入图片描述

由上图可知float:left/right是将元素按从左到右或从右到左的顺序依次排列。即使是块级元素等也可以排成一排。

.wrapper{
  width:350px;
  height:300px;
  border:1px solid black;
}
.content{
  float:right;
  width:100px;
  height:100px;
  margin-left:10px;
  margin-bottom:10px;
  background-color:black;
  color:#fff;
}

在这里插入图片描述

它也可以在浮动的过程中加一些修饰。

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Untitled2.css">
  <title>Document</title>
 </head>
 <body>
	
	<div class="box"></div>
	<div class="demo"></div>
	
</body>
</html>



.box{
  width:100px;
  height:100px;
  background-color:black;
}
.demo{
  width:150px;
  height:150px;
  background-color:green;
}

在这里插入图片描述

.box{
 float:left;
  width:100px;
  height:100px;
  background-color:black;
  opacity:0.5
}
.demo{
  width:150px;
  height:150px;
  background-color:green;
}

在这里插入图片描述

这是分层吗?不是。原理如下:浮动元素产生浮动流。所有产生浮动流的元素,块级元素看不到它们,故可以重叠在一起如上图。产生bfc的元素和文本类属性的元素(带有inline的元素)以及文本都能看到浮动元素。

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Untitled2.css">
  <title>Document</title>
 </head>
 <body>
	
	<div class="box"></div>
	<img src="123.png">
	
	
</body>
</html>

在这里插入图片描述

	<!DOCTYPE html>
	<html lang="en">
	 <head>
	  <meta charset="UTF-8">
	  <link rel="stylesheet" type="text/css" href="Untitled2.css">
	  <title>Document</title>
	 </head>
	 <body>
		
		<div class="box">
		  <div class="demo">1</div>
		  <div class="demo">2</div>
		  <div class="demo">3</div>
		  </div>
	</body>
	</html>


.box{
  border:1px solid black;
}
.demo{
  width:100px;
  height:100px;
  background-color:black;
  color:#fff;
}

在这里插入图片描述

.box{
  border:1px solid black;
}
.demo{
  float:left;
  width:100px;
  height:100px;
  background-color:black;
  color:#fff;
}

在这里插入图片描述
父级没有框住子类,怎么解决,原因在浮动元素产生了浮动流,如何去除浮动流呢。用p标签的相关语法,如下:

*{
  margin:0;
  padding:0;
}
.box{
  border:1px solid black;
}
.demo{
  float:left;
  width:100px;
  height:100px;
  background-color:black;
  color:#fff;
}
p{
  border-top: 0 solid green;/*该句可写可不写*/
  clear:both;
}

在这里插入图片描述

你也可以在box中设置高为100px 但如果是多行呢,这样不合适,所以上面的方法较好。父级包住浮动元素,要有边框就是这样解决的。
在这里插入图片描述
伪元素:存在与任意一个元素之间。伪元素本来就有,我们可以选中伪元素对它进行修改。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值