博主信息
博文 10
粉丝 0
评论 0
访问量 8144
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
清除子元素浮动方法及三列布局的实现原理--2019年9月4日
小毅小开的博客
原创
790人浏览过


1,实例演示如何消除子元素浮动造成父元素高度折叠的影响

实例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <style>
        .box1 {
            width: 300px;
            border: 3px dashed blue;
            background: cadetblue;
        }
        
        .box2 {
            width: inherit;
            height: 300px;
            background: coral;
        }
        
        .box2 {
            float: left;
        }
    </style>
    <title>Document</title>
</head>

<body>
    <div class="box1">
        <div class="box2">子元素</div>
    </div>
</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

当子元素使用了float属性时,父容器无法根据子元素高度而自适应高度,为了解决该问题,通常有以下三种做法:

1,让父元素浮动。

实例

<style>
        .box1 {
            width: 300px;
            border: 5px dashed red;
        }        
        .box2 {
            width: inherit;
            height: 300px;
            background-color: lightblue;
        }        
        .box2 {
            float: left;
        }        
        .box1 {
            float: left;
        }
    </style>

运行实例 »

点击 "运行实例" 按钮查看在线实例

2,在浮动元素后面加一个清楚浮动的空标签

实例

    <style>
        .box1 {
            width: 300px;
            border: 5px dashed red;
        }        
        .box2 {
            width: inherit;
            height: 300px;
            background-color: lightblue;
        }        
        .box2 {
            float: left;
        }        
        .clear {
            clear: both;
        }
    </style>

运行实例 »

点击 "运行实例" 按钮查看在线实例

3,在父元素中清除浮动

实例

    <style>
        .box1 {
            width: 300px;
            border: 5px dashed red;
        }        
        .box2 {
            width: inherit;
            height: 300px;
            background-color: lightblue;
        }        
        .box2 {
            float: left;
        }        
        .box1 {
            overflow: hidden;
        }
    </style>

运行实例 »

点击 "运行实例" 按钮查看在线实例


2,实例演示三列布局的实现原理( 绝对定位实现, 浮动定位实现)

实例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <style>
        .container {
            margin: 0 auto;
            width: 800px;
        }
        
        .header {
            height: 40px;
            background: khaki;
        }
        
        .footer {
            height: 50px;
            background: lightsalmon;
        }
        
        .main {
            background: lightseagreen;
            margin: 5px auto;
        }
        
        .left {
            width: 200px;
            min-height: 600px;
            background: lightgreen;
        }
        
        .cent {
            min-height: 600px;
            background: lime;
        }
        
        .right {
            width: 200px;
            min-height: 600px;
            background: lightpink;
        }
        /* 使用 Position 分别把图片定为在左边、中间、右边 */
        
        .main {
            position: relative;
        }
        
        .left {
            position: absolute;
            left: 0;
            top: 0;
        }
        
        .right {
            position: absolute;
            right: 0;
            top: 0;
        }
        
        .cent {
            margin-left: 200px;
            margin-right: 200px;
        }
    </style>
    <title>Document</title>
</head>

<body>
    <div class="container">
        <div class="header">头部</div>
        <!-- 内容区 -->
        <div class="main">
            <div class="left">左侧</div>
            <div class="cent">内容区</div>
            <div class="right">右侧</div>
        </div>
        <div class="footer">底部</div>
    </div>
</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例



批改状态:合格

老师批语:其实清除子元素浮动还有一种利用伪元素实现, 有空可以上网查一下
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学