两栏布局

1、绝对定位方式

<div class="box">
     <style>
         .box {
             position: relative;
         }
         .box > div {
             min-height: 100px;
             position: absolute;
         }
         .left {
             left: 0;
             width: 300px;
             background: red;
         }
         .right {
             left: 320px;
             right: 0;
             background: blue;
         }
     </style>
     <div class="left">left</div>
     <div class="right">
         <p>right</p>
         <p>right</p>
         <p>right</p>
         <p>right</p>
         <p>right</p>
         <p>right</p>
         <p>right</p>
         <p>right</p>
         <p>right</p>
         <p>right</p>
         <p>right</p>
         <p>right</p>
         <p>right</p>
     </div>
 </div>

2、网格布局

<div class="box">
    <style>
        .box {
            width: 100%;
            display: grid;
            grid-template-columns: 300px auto;
        }
        .box > div {
            
        }
        .left {
            background: red;
        }
        .right {
            background: blue;
        }
    </style>
    <div class="left">left</div>
    <div class="right">
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
    </div>
</div>

3、表格布局

<div class="box">
    <style>
        .box {
            width: 100%;
            display: table;
        }
        .box > div {
            display: table-cell;
        }
        .left {
            width: 300px;
            background: red;
        }
        .right {
            background: blue;
        }
    </style>
    <div class="left">left</div>
    <div class="right">
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
    </div>
</div>

4、flex布局

<div class="box">
    <style>
        .box {
            width: 100%;
            display: flex;
        }
        .left {
            width: 300px;
            background: red;
        }
        .right {
            flex: 1;
            background: blue;
        }
    </style>
    <div class="left">left</div>
    <div class="right">
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
    </div>
</div>

5、双inline-block

<div class="box">
    <style>
        .box {
            box-sizing: content-box;
            font-size: 0;
        }
        .box > div {
            display: inline-block;
            vertical-align: top;
            box-sizing: border-box;
            min-height: 100px;
            font-size: 14px;
        }
        .left {
            width: 300px;
            background: red;
        }
        .right {
            background: blue;
            width: calc(100% - 300px);
        }
    </style>
    <div class="left">left</div>
    <div class="right">
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
    </div>
</div>
  • 这种方法是通过width: calc(100% - 300px)来动态计算右侧盒子的宽度。需要知道右侧盒子距离左边的距离,以及左侧盒子具体的宽度(content+padding+border),以此计算父容器宽度的100%需要减去的数值。同时,还需要知道右侧盒子的宽度是否包含border的宽度。
  • 在这里,为了简单的计算右侧盒子准确的宽度,设置了子元素的box-sizing:border-box;以及父元素的box-sizing: content-box;。
  • 同时,作为两个inline-block的盒子,必须设置vertical-align来使其顶端对齐。
  • 另外,为了准确地应用计算出来的宽度,需要消除div之间的空格,需要通过设置父容器的font-size: 0;,或者用注释消除html中的空格等方法。

缺点

  • 需要知道左侧盒子的宽度,两个盒子的距离,还要设置各个元素的box-sizing
  • 需要消除空格字符的影响
  • 需要设置vertical-align: top满足顶端对齐。

6、左元素float,右元素margin-left

<div class="box">
    <style>
        .box {
            overflow: hidden;
        }
        .box > div {
            
        }
        .left {
            width: 300px;
            background: red;
            float: left;
        }
        .right {
            background: blue;
            margin-left: 320px;
        }
    </style>
    <div class="left">left</div>
    <div class="right">
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
    </div>
</div>

7、float + bfc

<div class="box">
    <style>
        .box {
            overflow: hidden;
        }
        .box > div {
            
        }
        .left {
            width: 300px;
            background: red;
            float: left;
        }
        .right {
            background: blue;
            overflow: hidden;
        }
    </style>
    <div class="left">left</div>
    <div class="right">
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
    </div>
</div>

8、双float方案

<div class="box">
    <style>
        .box {
            overflow: hidden;
            box-sizing: content-box;
        }
        .box > div {
            float: left;
            box-sizing: border-box;
        }
        .left {
            width: 300px;
            background: red;
        }
        .right {
            background: blue;
            width: calc(100% - 300px);
        }
    </style>
    <div class="left">left</div>
    <div class="right">
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
        <p>right</p>
    </div>
</div>
  • 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、付费专栏及课程。

余额充值