博客列表 >消除子元素浮动造成父元素高度折叠的影响,演示三列布局的实现原理( 绝对定位实现, 浮动定位实现)实列演示-2019年9月4号

消除子元素浮动造成父元素高度折叠的影响,演示三列布局的实现原理( 绝对定位实现, 浮动定位实现)实列演示-2019年9月4号

zzc111的博客
zzc111的博客原创
2019年09月05日 16:03:22488浏览

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

实例

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

<head>
    <meta charset="UTF-8">
    <title>消除子元素浮动</title>
    <style>
        .box1 {
            width: 300px;
            border: 5px dashed red;
            overflow: hidden;
        }
        
        .box2 {
            width: inherit;
            height: 300px;
            background-color: aquamarine;
            float: left;
        }
    </style>
</head>

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

</html>

运行实例 »

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

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">
    <title>三列布局(绝对定位)</title>
    <style>
        .container {
            width: 1000px;
            margin: 0 auto;
        }
        
        .header,
        .footer {
            height: 60px;
            background-color: lightgray;
        }
        
        .main {
            background-color: lightblue;
            margin: 5px auto;
            position: relative;
        }
        
        .left {
            width: 200px;
            min-height: 800px;
            background-color: lightgrey;
            position: absolute;
            left: 0;
            top: 0;
        }
        
        .content {
            min-height: 800px;
            background-color: lightgreen;
            margin-left: 200px;
            margin-right: 200px;
        }
        
        .right {
            width: 200px;
            min-height: 800px;
            background-color: lightgrey;
            position: absolute;
            top: 0;
            right: 0;
        }
    </style>
</head>

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

    </div>
</body>

</html>

运行实例 »

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

浮动实现:

实例

<!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">
    <title>三列布局(浮动)</title>
    <style>
        .container {
            width: 1000px;
            margin: 0 auto;
        }
        
        .header,
        .footer {
            height: 60px;
            background-color: lightgray;
        }
        
        .main {
            background-color: lightblue;
            margin: 5px auto;
            overflow: hidden;
        }
        
        .left {
            width: 200px;
            min-height: 800px;
            background-color: lightgrey;
            float: left;
        }
        
        .content {
            min-height: 800px;
            background-color: lightgreen;
            float: left;
            width: 580px;
            margin-left: 10px;
        }
        
        .right {
            width: 200px;
            min-height: 800px;
            background-color: lightgrey;
            float: right;
        }
    </style>
</head>

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

    </div>
</body>

</html>

运行实例 »

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


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议