垂直外边距重叠解决方案和padding占用空间解决方案

248 阅读1分钟

一、垂直外边距重叠

<style>
	.patient{
	  width:100px;
	  height:100px;
	  background-color:pink;
	}
	.children{
	  width:50px;
	  height:50px;
	  background-color:green;
          margin-top:30px
      
	}
</style>
</head>
<body>

<div class="patient">
  <div class="children"></div>
</div>

Snipaste_2022-03-19_22-29-12.jpg

解决方案:加overflow属性;

<style>
	.patient{
	  width:100px;
	  height:100px;
	  background-color:pink;
          overflow:hidden;
	}
	.children{
	  width:50px;
	  height:50px;
	  background-color:green;
          margin-top:30px
      
	}
</style>
</head>
<body>

<div class="patient">
  <div class="children"></div>
</div>

Snipaste_2022-03-19_22-19-56.jpg

如果想用在父类里写padding解决会发现新的问题:padding占用空间

<style>
	.patient{
	  width:100px;
	  height:100px;
	  background-color:pink;
	 padding:30px;
	}
	.children{
	  width:50px;
	  height:50px;
	  background-color:green;
     
	}
</style>
</head>
<body>

<div class="patient">
  <div class="children"></div>
</div>

Snipaste_2022-03-19_22-22-28.jpg

解决方案: 在父类里写入 box-sizing:border-box;即可解决;