Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS实现三列布局(两边固定中间自适应) #27

Open
wl05 opened this issue May 24, 2019 · 0 comments
Open

CSS实现三列布局(两边固定中间自适应) #27

wl05 opened this issue May 24, 2019 · 0 comments

Comments

@wl05
Copy link
Owner

wl05 commented May 24, 2019

三列布局在开发中也是比较常见的布局方式,本篇总结一下几种常见的css三列布局的方式。我们要实现如下的布局。

1. BFC 三栏布局

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .container {
            border: 3px solid #888888;
            margin-top: 20px;
            overflow: hidden;
        }

        .left {
            float: left;
            height: 200px;
            width: 100px;
            background-color: red;
        }

        .right {
            width: 200px;
            height: 200px;
            float: right;
            background-color: blue;
        }

        .main {
            overflow: hidden;
            height: 200px;
            background-color: green;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="left"></div>
    <div class="right"></div>
    <div class="main"></div>
</div>
</body>
</html>

利用“BFC的区域不会与float box重叠。”的规则我们设置main的overflow: hidden; 。具体的BFC原理的请参考BFC原理
这里需要注意的是main标签的位置需要放在left、right的后面,同时也就造成了内容区域无法优先渲染的缺点。

2. 绝对定位布局

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .container {
            position: relative;
            border: 3px solid #888888;
        }

        .main {
            height: 400px;
            margin: 0 120px;
            background-color: green;
        }

        .left {
            position: absolute;
            width: 100px;
            height: 300px;
            left: 0;
            top: 0;
            background-color: red;
        }

        .right {
            position: absolute;
            width: 100px;
            height: 300px;
            background-color: blue;
            right: 0;
            top: 0;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="main"></div>
    <div class="left"></div>
    <div class="right"></div>
</div>
</body>
</html>

绝对定位布局相对来说就比较好理解了,也没什么好说的。

3. Flex布局

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .container {
            display: flex;
            border: 3px solid #888888;
        }

        .left {
            width: 200px;
            height: 300px;
            background-color: red;
        }

        .main {
            background-color: blue;
            flex: 1;
        }

        .right {
            width: 100px;
            height: 300px;
            background-color: green;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="left"></div>
    <div class="main"></div>
    <div class="right"></div>
</div>
</body>
</html>

flex布局也是比较容易理解的,left,right 定宽,main 设置flex: 1;占满剩下的区域。

4. table布局

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .container {
            display: table;
            width: 100%;
        }

        .left, .main, .right {
            display: table-cell;
        }

        .left {
            width: 200px;
            height: 300px;
            background-color: red;
        }

        .main {
            background-color: blue;
        }

        .right {
            width: 100px;
            height: 300px;
            background-color: green;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="left"></div>
    <div class="main"></div>
    <div class="right"></div>
</div>
</body>
</html>

table 布局相对来也比较好理解,也什么好说的。

5. 流体布局

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .left {
            float: left;
            height: 200px;
            width: 100px;
            background-color: red;
        }

        .right {
            width: 200px;
            height: 200px;
            background-color: blue;
            float: right;
        }

        .main {
            margin-left: 120px;
            margin-right: 220px;
            height: 200px;
            background-color: green;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="left"></div>
    <div class="right"></div>
    <div class="main"></div>
</div>
</body>
</html>

这种方式跟第一种方式比较类似。只是说这种方式通过设置main的margin-left:120px;margin-right: 220px;这种方式,强行将main和left,right分隔开。缺点也是main区域不能优先渲染。

6. 双飞翼布局

双飞翼布局相对来说代码比较难理解。

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .content {
            float: left;
            width: 100%;
        }
        .main {
            height: 200px;
            margin-left: 110px;
            margin-right: 220px;
            background-color: green;
        }
        .left {
            float: left;
            height: 200px;
            width: 100px;
            margin-left: -100%;
            background-color: red;
        }
        .right {
            width: 200px;
            height: 200px;
            float: right;
            margin-left: -200px;
            background-color: blue;
        }
    </style>
</head>
<body>
<div class="content">
    <div class="main"></div>
</div>
<div class="left"></div>
<div class="right"></div>
</body>
</html>

主要在于理解left,right 浮动后margin的设置。

7. 圣杯布局

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .container {
            margin-left: 120px;
            margin-right: 220px;
        }
        .main {
            float: left;
            width: 100%;
            height: 300px;
            background-color: red;
        }
        .left {
            float: left;
            width: 100px;
            height: 300px;
            margin-left: -100%;
            left: -120px;
            background-color: blue;
        }
        .right {
            float: left;
            width: 200px;
            height: 300px;
            margin-left: -200px;
            right: -220px;
            background-color: green;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="main"></div>
    <div class="left"></div>
    <div class="right"></div>
</div>
</body>
</html>

主要也是对margin的理解。

参考链接

  1. 详解 CSS 七种三栏布局技巧
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant