布局

本文深入探讨了网页设计中常见的三栏布局实现方法,包括浮动布局、绝对定位布局、flex布局、表格布局、网格布局以及圣杯布局和双飞翼布局。每种布局的优缺点及其适用场景均有详细解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

涉及的范围

页面布局 CSS盒模型 DOM事件 HTTP协议 面向对象 原型链
通信 算法 安全

一面/二面

页面布局

1 假设高度已知,请写出三栏布局,其中左栏,右栏宽度各位300px,中间自适应
题目的延伸

1 每个方案的优缺点

写在代码下面

2 假设把高度去掉,考虑纵向,哪个方案不适用?

定位和表格能用

3 兼容性,真正在业务中,使用哪种?

看情况!!

浮动布局
<!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>三栏布局-float</title>
</head>

<body>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .left {
            width: 300px;
            height: 600px;
            float: left;
            background: red
        }
        
        .right {
            width: 300px;
            height: 600px;
            float: right;
            background: red
        }
        
        .center {
            width: 100%;
            height: 600px;
            background: yellow
        }
    </style>
    <div class="left"></div>
    <div class="right"></div>
    <div class="center"></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>
</head>

<body>
    <style>
        * {
            padding: 0;
            margin: 0
        }
        
        .parent>div {
            position: absolute
        }
        
        .left {
            width: 300px;
            left: 0;
            height: 100px;
            background: red
        }
        
        .center {
            left: 300px;
            right: 300px;
            height: 100px;
            background: yellow;
        }
        
        .right {
            right: 0;
            width: 300px;
            height: 100px;
            background: red;
        }
    </style>
    <div class="parent">
        <div class="left"></div>
        <div class="center">
            <h2>绝对定位解决方案</h2>
            1 这个三栏布局绝对定位的中间部分 1 这个三栏布局绝对定位的中间部分
        </div>
        <div class="right"></div>
    </div>
</body>

</html>

在这里插入图片描述
绝对定位布局优点,很快捷,设置很方便,而且也不容易出问题,你可以很快的就能想出这种布局方式。
缺点就是,绝对定位是脱离文档流的,意味着下面的所有子元素也会脱离文档流,这就导致了这种方法的有效性和可使用性是比较差的。

flex布局
<!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>三栏布局-flex</title>
</head>

<body>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .parent {
            display: flex
        }
        
        .left {
            width: 300px;
            background: red;
        }
        
        .right {
            width: 300px;
            background: red;
        }
        
        .center {
            flex: 1;
            background: yellow
        }
    </style>

    <div class="parent">
        <div class="left"></div>
        <div class="center">
            <h2>三栏布局中的flex</h2>
            打开的看到
        </div>
        <div class="right"></div>
    </div>
</body>

</html>

在这里插入图片描述
felxbox布局是css3里新出的一个,它就是为了解决上述两种方式的不足出现的,是比较完美的一个。目前移动端的布局也都是用flexbox。
felxbox的缺点就是不能兼容IE8及以下浏览器。

表格布局
<!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>
</head>

<body>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .parent {
            display: table;
            width: 100%
        }
        
        .parent>div {
            display: table-cell
        }
        
        .left {
            width: 300px;
            background: red
        }
        
        .right {
            width: 300px;
            background: red
        }
        
        .center {
            background: yellow
        }
    </style>
    <div class="parent">
        <div class="left"></div>
        <div class="center">
            <h2>三栏布局中的表格</h2>
            打开的看到
        </div>
        <div class="right"></div>
    </div>
</body>

</html>

在这里插入图片描述
表格布局在历史上遭到很多人的摒弃,说表格布局麻烦,操作比较繁琐,其实这是一种误解,在很多场景中,表格布局还是很适用的,比如这个三栏布局,用表格布局就轻易写出来了。还有表格布局的兼容性很好,在flex布局不兼容的时候,可以尝试表格布局。
表格布局也是有缺陷的,当其中一个单元格高度超出的时候,两侧的单元格也是会跟着一起变高的,而有时候这种效果不是我们想要的

网格布局
<!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>
</head>

<body>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .parent {
            display: grid;
            width: 100%;
            grid-template-rows: 100px;
            grid-template-columns: 300px auto 300px;
        }
        
        .left {
            background: red;
        }
        
        .center {
            background: yellow;
        }
        
        .right {
            background: red;
        }
    </style>
    <div class="parent">
        <div class="left"></div>
        <div class="center">
            <h2>三栏布局中的网格</h2>
            打开的看到
        </div>
        <div class="right"></div>
    </div>
</body>

</html>

在这里插入图片描述
网格布局也是新出的一种布局方式,如果你答出这种方式,也就证明了你的实力,证明你对技术热点是有追求的,也说明你有很强的学习能力。

圣杯布局和双飞翼布局

圣杯布局和双飞翼布局基本上是一致的,都是两边固定宽度,中间自适应的三栏布局,其中,中间栏放到文档流前面,保证先行渲染。解决方案大体相同,都是三栏全部float:left浮动,区别在于解决中间栏div的内容不被遮挡上,圣杯布局是中间栏在添加相对定位,并配合left和right属性,效果上表现为三栏是单独分开的(如果可以看到空隙的话),而双飞翼布局是在中间栏的div中嵌套一个div,内容写在嵌套的div里,然后对嵌套的div设置margin-left和margin-right,效果上表现为左右两栏在中间栏的上面,中间栏还是100%宽度,只不过中间栏的内容通过margin的值显示在中间。
在这里插入图片描述

圣杯布局
<!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>
</head>
<style>
    .container,
    html,
    body {
        height: 800px;
        padding: 0;
        margin: 0;
    }
    
    .col {
        float: left;
        /* 三个都设置浮动,为了把left和right定位到左右部分*/
        position: relative;
    }
    
    .container {
        padding: 0 200px 0 100px;
    }
    
    .left {
        left: -100px;
        width: 100px;
        height: 100%;
        margin-left: -100%;
        background: #ff69b4
    }
    
    .main {
        width: 100%;
        height: 100%;
        background: #659;
    }
    
    .right {
        right: -200px;
        width: 200px;
        height: 100%;
        margin-left: -200px;
        background: #ff69b4
    }
    
    .header,
    .footer {
        height: 50px;
        background: #666;
        text-align: center;
    }
</style>

<body>
    <div class="header">header</div>
    <div class="container">
        <div class="main col">Main</div>
        <div class="left col">Left</div>
        <div class="right col">Right</div>
    </div>
    <div class="footer">footer</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>
</head>

<body>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .header,
        .footer {
            height: 50px;
            background: gray;
            text-align: center;
        }
        
        .middle {
            height: 900px;
            width: 100%;
            float: left;
            background: blue;
        }
        
        .inset {
            margin: 0 200px 0 200px;
            height: 100px;
        }
        
        .left {
            float: left;
            margin-left: -100%;
            width: 200px;
            background: #0c9;
            height: 900px;
        }
        
        .right {
            float: left;
            margin-left: -200px;
            width: 200px;
            background: #0c9;
            height: 900px;
        }
        
        .footer {
            clear: both;
        }
    </style>
    <div class="header">header</div>

    <div class="middle">
        <div class="inset">middle</div>
    </div>
    <div class="left">left</div>
    <div class="right">right</div>
    <div class="footer">footer</div>

</body>

</html>

在这里插入图片描述

页面布局的变通
三栏布局
  • 左右宽度固定,中间自适应
  • 上下宽度固定,中间自适应
两栏布局
  • 左宽度固定,右自适应
  • 右宽度固定,左自适应
  • 上宽度固定,下自适应
  • 下宽度固定,上自适应
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值