登录  /  注册
博主信息
博文 22
粉丝 0
评论 0
访问量 17720
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
浮动与定位的简单认知--2019-0904
sjgbctjda的博客
原创
567人浏览过

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">
    <link rel="stylesheet" href="static/css/style1.css">
    <title>浮动设置与清除</title>
</head>

<body>
    <div class="container">
        <div class="con1">
            <h4>父级元素设置overflow属性</h4>
            <div class="box11">1</div>
            <div class="box12">2</div>
        </div>
        <div class="con3">
            <h4>添加一个具有clear属性区块</h4>
            <div class="box31">1</div>
            <div class="box32">2</div>
            <div class="clear"></div>
        </div>
        <div class="con4">
            <h4>将父元素的高度设置为浮动的子元素中的最大高度</h4>
            <div class="box41">1</div>
            <div class="box42">2</div>
        </div>
        <div class="con2">
            <h4>父级元素同样设置float属性</h4>
            <div class="box21">1</div>
            <div class="box22">2</div>
        </div>
    </div>
</body>

</html>

运行实例 »

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

css代码

.container {
    border: 2px solid black;
    padding: 10px;
    position: relative;
    overflow: hidden;
}

.con1 {
    width: 300px;
    border: 2px solid red;
    overflow: hidden;
    margin-bottom: 20px;
}

.box11 {
    width: 150px;
    height: 200px;
    float: left;
    background: lime;
}

.box12 {
    width: 100px;
    height: 300px;
    float: right;
    background: lightsalmon;
}

.con3 {
    width: 300px;
    border: 2px solid blue;
    margin-bottom: 20px;
}

.box31 {
    width: 150px;
    height: 200px;
    float: left;
    background: lime;
}

.box32 {
    width: 100px;
    height: 300px;
    float: right;
    background: lightsalmon;
}

.clear {
    clear: both;
}

.con4 {
    width: 300px;
    height: 400px;
    border: 2px solid blueviolet;
    margin-bottom: 20px;
}

.box41 {
    width: 150px;
    height: 200px;
    float: left;
    background: lime;
}

.box42 {
    width: 100px;
    height: 300px;
    float: right;
    background: lightsalmon;
}

.con2 {
    width: 300px;
    /* height: 150px; */
    border: 2px solid black;
    float: left;
    margin-bottom: 20px;
}

.box21 {
    width: 150px;
    height: 200px;
    float: left;
    background: lime;
}

.box22 {
    width: 100px;
    height: 300px;
    float: right;
    background: lightsalmon;
}

运行实例 »

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

运行结果:

task1运行结果1.pngtask1运行结果2.pngtask1运行结果3.pngtask1运行结果4.pngtask1运行结果5.png

浮动是将元素向左或者向右移动直到碰到另一个浮动元素或者父级元素的边框位置;元素浮动之后再水平方向将脱离文档流,并且会在竖直方向上对下面元素的布局造成影响。


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">
    <link rel="stylesheet" href="static/css/style2.css">
    <title>定位</title>
</head>

<body>
    <div class="box1">
        <h2>定位实现三列布局</h2>
        <div class="top">head</div>
        <div class="main">
            <div class="left">左侧</div>
            <div class="center">中</div>
            <div class="right">右侧</div>
        </div>
        <div class="footer">footer</div>
    </div>
    <div class="box2">
        <h2>浮动实现三列布局</h2>
        <div class="top">head</div>
        <div class="main">
            <div class="left">左侧</div>
            <div class="center">中</div>
            <div class="right">右侧</div>
        </div>
        <div class="footer">footer</div>
    </div>
</body>

</html>

运行实例 »

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

css代码

/* 定位实现三类布局 */

.body {
    margin: 0;
    padding: 0;
}

.box1 {
    margin-bottom: 20px;
    text-align: center;
}

.top,
.footer {
    width: 100%;
    height: 30px;
    background: lightgreen;
}

.top {
    margin-bottom: 20px;
}

.main {
    width: 80%;
    /* height: 500px; */
    margin: auto;
    /* border: 1px solid black; */
    margin-bottom: 20px;
    position: relative;
}

.left {
    width: 20%;
    height: 200px;
    background: #aaddaa;
    position: absolute;
    top: 0;
    left: 0;
}

.center {
    height: 200px;
    background: lightslategrey;
    margin-left: 20.5%;
    margin-right: 30.5%;
}

.right {
    width: 30%;
    height: 200px;
    background: lightseagreen;
    position: absolute;
    top: 0px;
    right: 0px;
}


/* 浮动实现三列布局 */

.box2 {
    text-align: center;
    margin-bottom: 50px;
}

.box2 .top,
.box2 .footer {
    width: 100%;
    height: 30px;
    background: lightseagreen;
}

.box2 .top {
    margin-bottom: 20px;
}

.box2 .main {
    width: 80%;
    /* height: 500px; */
    margin: auto;
    /* border: 1px solid black; */
    margin-bottom: 20px;
    overflow: hidden;
}

.box2 .left {
    width: 20%;
    /* min-height: 200px; */
    background: #aaddaa;
    float: left;
}

.box2 .center {
    height: 200px;
    background: lightslategrey;
    margin-left: 21%;
    margin-right: 31%;
}

.box2 .right {
    width: 30%;
    /* min-height: 200px; */
    background: lightseagreen;
    float: right;
}

;

运行实例 »

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

运行结果:

task2运行结果1.pngtask2运行结果2.png

在使用定位方式进行三列布局的时候,左中右三列的父级元素的高度是由未进行绝对定位区块“中”的高度来决定的,需要注意的是:当中间区块定义的高度比两侧区域高度小的时候,两侧区块超出部分会超出父级区块的范围,从而影响到整个页面的布局,如图所示:

task2运行结果3.png

可以通过给父级元素设置overflow属性将多余的部分进行隐藏。

同样,在通过浮动的方式进行页面布局的时候,应该通过overflow属性的设置来尽量减少由浮动操作给后面元素的布局带来麻烦。





批改状态:合格

老师批语:你的布局 有点意思, 不错, 加油
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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

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