一、父元素设置overflow:hidden
<style>
.outer {
background-color: cornflowerblue;
/* 父元素设置这个属性 */
overflow: hidden;
}
.inner {
width: 150px;
height: 150px;
background-color: yellowgreen;
opacity: 0.5;
float: left;
}
</style>
<body>
<div class="outer">
<div class="inner">
我是子元素
</div>
</div>
</body>
二、添加一个块 无意义的标签块
<style>
.outer {
background-color: cornflowerblue;
}
.inner {
width: 150px;
height: 150px;
background-color: yellowgreen;
opacity: 0.5;
float: left;
}
</style>
<body>
<div class="outer">
<div class="inner"></div>
<!-- 添加一个块 -->
<div style="clear: both;"></div>
</div>
</body>
三、父级添加伪类
<style>
.outer {
/* width: 300px; */
background-color: cornflowerblue;
}
/* 父级伪类 */
.outer::after {
content: "";
display: block;
clear: both;
}
.inner {
width: 150px;
height: 150px;
background-color: yellowgreen;
opacity: 0.5;
float: left;
}
</style>
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
四、父级设置一个高度