当前位置:网站首页>浮动引起的高度塌陷问题
浮动引起的高度塌陷问题
2022-07-26 21:35:00 【废物的自我修养记录】
正常页面布局
<style> *{
margin:0; padding: 0; } .content{
width:400px; border:1px solid #000; } .box{
width:200px; height:200px; background: greenyellow; } </style>
<body>
<div class="content">
<div class="box"></div>
</div>
<p>基苦阿斯蒂芬</p>
</body>

当给类名为.box的盒子加上浮动后
<style> *{
margin:0; padding: 0; } .content{
width:400px; border:1px solid #000; } .box{
width:200px; height:200px; background: greenyellow; float: left; } </style>

可以看到页面布局已经乱了,因为元素设置浮动后会脱离文档流
解决方案
1 利用BFC(给父元素加上overflow:hidden)
**缺点:父元素溢出的元素会隐藏,可能会影响页面显示 **
.content{
width:400px;
border:1px solid #000;
overflow: hidden;
}
2 加空div
要点:
1.加上一个空的块级元素
2.对块级元素进行清除浮动, 省事方法,不管理是左浮还是右浮,直接全清(clear:both)
<style> *{
margin:0; padding: 0; } .content{
width:400px; border:1px solid #000; /* overflow: hidden; */ } .box{
width:200px; height:200px; background: greenyellow; float: left; } .clear{
clear: both; } </style>
<body>
<div class="content">
<div class="box"></div>
<div class="clear"></div>
</div>
<p>基苦阿斯蒂芬</p>
</body>
3 利用伪元素
要点:
1 其实和加空div的原理是一致的,唯一要记住的就是把伪元素转为块级元素(display:block),否则会没有效果
2 还有伪元素的属性不要忘记加上(content:‘’)
<style> *{
margin:0; padding: 0; } .content{
width:400px; border:1px solid #000; /* overflow: hidden; */ } .content::after{
content: ''; display: block; // 必须要转换为块元素 clear: both; } .box{
width:200px; height:200px; background: greenyellow; float: left; } /* .clear{ clear: both; } */ </style>
<body>
<div class="content">
<div class="box"></div>
<!-- <div class="clear"></div> -->
</div>
<p>基苦阿斯蒂芬</p>
</body>
边栏推荐
- Matlab pitch period estimation post-processing
- Promise me, don't write shit code after reading it..
- 挖财钱堂和启牛学堂哪个更好一些?是安全的吗
- ORM同时使用不同数据源和不同命名转换
- Development to testing: a six-year road to automation from scratch
- Jd.com won the highest award for intelligent science and technology in China! Inventory JD system intelligent technology
- 学了那么久的用例设计,知不知道为什么设计测试用例
- 想让照片中的云飘起来?视频编辑服务一键动效3步就能实现
- 09 expr command
- Software Testing Technology: cross platform mobile UI automated testing (Part 2)
猜你喜欢

unity 安装失败:operation not permitted, mkdir......

2018 arXiv preprint | MolGAN: An implicit generative model for small molecular graphs

基于gRPC编写golang简单C2远控

Concept and classification of processes

yolov1

Operating guidelines and suggestions for spot gold (Part 1)

Matlab draws short-term average amplitude spectrum

09 expr 命令

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

【Qt多线程之线程的等待和唤醒】
随机推荐
Altium designer 22 modify the layer properties of the selected component
JS delay execution window.onload
Go ---- variable usage in go language
Xshell7 personal free download, use
同花顺手机炒股开户安全吗?怎么办理开户呢
基于gRPC编写golang简单C2远控
调试stc8a8k64d4单片机485通信总结
SQL injection less26 (filter spaces and comments, and use error injection without spaces)
The potential of emerging markets is unlimited, and advance.ai risk control products help Chinese offshore enterprises build a solid foundation for safe development
Pytoch -- used by visdom
: could not determine a constructor for the tag ! RootAdmin
jmeter -- response中文乱码处理
Altium designer 22 Chinese character garbled
Open source | arex Ctrip traffic playback practice without code intrusion
ORM uses different data sources and different naming transformations at the same time
Qt中为工程添加资源文件、给按钮添加图片
Unity operates on Explorer, opens explorer, selects files, and filters files
09.01 深度优先搜索
leetcode:857. 雇佣 K 名工人的最低成本【分块思考 + 由简单入手】
Proto basic grammar of protobuf