zl程序教程

您现在的位置是:首页 >  前端

当前栏目

css:两栏三栏布局

CSS 布局
2023-09-27 14:27:10 时间

两栏布局左边固定右边自适应

在这里插入图片描述

1、浮动方式

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

2、定位方式

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .father {
            position: relative;
            height: 200px;
        }
        
        .left {
            position:absolute;
            width: 200px;
            height: 100%;
            background-color: blue;
        }

        .right {
            position:absolute;
            height: 100%;
            left:200px;
            right: 0;
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>
</html>

3、flex方式

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .father {
            height: 300px;
            width: 100%;
            display: flex;
        }

        .left {
            width: 300px;
            height: 100%;
            background-color: blue;
        }

        .right {
            flex: 1;
            height: 100%;
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>
</html>

三栏布局左右固定中自适应

在这里插入图片描述

1、浮动方式

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .father{
            height: 50px;
        }
        .left,.right,.main {
            height: 100%;
        }

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

        .main {
            margin-left: 200px;
            margin-right: 200px;
            background-color: blue;
        }

        .right {
            float: right;
            width: 200px;
            background-color: yellow;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="left"></div>
        <div class="right"></div>
        <div class="main"> </div>
    </div>
</body>
</html>

2、定位方式

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .father{
            position: relative;
            height: 50px;
        }
        .left,.right,.main {
            position: absolute;
            height: 100%;
        }

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

        .main {
            left: 200px;
            right: 200px;
            background-color: blue
        }

        .right {
            right: 0;
            width: 200px;
            background-color: yellow
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="left"></div>
        <div class="right"></div>
        <div class="main"> </div>
    </div>
</body>
</html>

3、flex方式

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .father {
            display: flex;
            height: 50px;
        }

        .left,.right,.main {
            height: 100%;
        }

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

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

        .right {
            width: 200px;
            background-color: yellow
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="left"></div>
        <div class="main"> </div>
        <div class="right"></div>
    </div>
</body>
</html>
```