实现两栏布局

几个实现两栏布局的方法(左侧固定,右侧自适应)

一、position(定位)

<!doctype html>
<html>

<head>
    <meta http-equiv="ContentType" content="text/html;charset=utf-8">
    <title>两栏布局</title>
    <style>
    .container{
        position: relative;
    }

    .left{
        position: absolute;
        width: 200px;
        background-color:brown; 
        height: 300px;
    }

    .right{
        width: 100%;
        background-color: blue;
        height: 300px;
    }
    </style>
</head>

<body>
    <div class="container">
        <div class="left"></div>
    </div>
    <div class="right"></div>
</body>

</html>

二、float(浮动)

<!doctype html>
<html>

<head>
    <meta http-equiv="ContentType" content="text/html;charset=utf-8">
    <title>两栏布局</title>
    <style>

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

    .right{
        width: 100%;
        background-color: blue;
        height: 300px;
    }
    </style>
</head>

<body>
        <div class="left"></div>
    <div class="right"></div>
</body>

</html>

浮动元素会对后一个元素产生影响,对前一个不会,后一个元素会围着float元素。解决办法:clear属性或BFC。

三、flex(弹性盒子)

<!doctype html>
<html>

<head>
    <meta http-equiv="ContentType" content="text/html;charset=utf-8">
    <title>两栏布局</title>
    <style>
        .container {
            display: flex;
        }

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

        .right {
            flex: 1;
            background-color: blue;
            height: 300px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>

</html>

四、margin-left(类似双飞翼布局)

<!doctype html>
<html>

<head>
    <meta http-equiv="ContentType" content="text/html;charset=utf-8">
    <title>两栏布局</title>
    <style>
        .left {
            float: left;
            width: 200px;
            background-color: brown;
            height: 300px;
            margin-left: -100%;
        }

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

        .inner {
            height: 300px;
            padding: 0 0 0 200px;
        }
    </style>
</head>

<body>
    <div class="right"></div>
    <div class="left">
        <div class="inner"></div>
    </div>
    </div>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用flex实现布局,你可以使用flex布局的属性来实现。具体的实现方式有多种,以下是其中的一种方法: 首先,在父元素上应用flex布局属性,比如设置display为flex,这样父元素的子元素就可以通过flex属性进行布局。 然后,设置左边元素的固定宽度,可以使用flex属性中的flex-basis来指定宽度,比如设置为200px。 接着,设置右边元素的flex属性为1,这样它会自动填充剩余的空间。 最后,根据需要设置元素的高度和背景颜色等样式。 以下是一个使用flex实现布局的示例代码: ```html <div class="outer"> <div class="left"></div> <div class="right"></div> </div> ``` ```css .outer { display: flex; height: 100px; } .left { width: 200px; height: 100%; background: lightcoral; } .right { flex: 1; height: 100%; background: lightseagreen; } ``` 这样,左边的元素会固定宽度为200px,右边的元素会自动填充剩余的空间。你可以根据实际需求调整元素的高度和样式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [实现常用的两布局(左侧固定+右侧自适应)以及三布局(圣杯布局和双飞翼布局)](https://blog.csdn.net/hzy199772/article/details/125639600)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [经典布局 (flex和传统两种实现) 左右两式](https://blog.csdn.net/m0_49515138/article/details/129136780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值