CSS实现左侧固定,右侧自适应(5种方法)

复制代码
<div class="father">
      <!-- 左右div不能调换顺序来写 -->
    <div class="left">固定宽度区</div>
    <div class="right">自适应区</div>
</div>

一、利用左侧浮动float+右侧margin-left

复制代码
 /* 利用浮动float+margin-left(左侧宽度需固定)*/
 /* 左边元素宽度固定(加入设置为300px)向左浮动 */
 /* 右边元素margin-left设置为100px,宽度不用设置 */
        .father{
            height: 300px;
        }
        .left{
            width: 300px;
            height: 300px;
            background-color: pink;
            float:left;   /*左侧设置浮动float*/
        }
        .right{
            margin-left: 300px;  /*右侧设置左边距margin-left等于左边盒子的宽度*/
            height: 300px;
            background-color: blue;
        }

二、利用左侧浮动float+右侧BFC

复制代码
/* 利用浮动+BFC */
/* 左边元素宽度固定(假如设置为300px),向左浮动  */
/* 右边元素设置overflow:hidden;  */
       .father {
           height: 300px;
        }
       .left {
           float: left;   /*左浮动,固定宽度*/
           width: 300px;
           height: 300px;
           background-color: pink;
        }
       .right {
           overflow:hidden;   /*设置 overflow:hidden触发BFC*/
           height: 300px;
           background-color: blue;
        }

三、利用flex布局

复制代码
 /* 利用flex布局 */
 /* 父元素设置flex布局 */
 /* 左边元素宽度固定 */
 /* 右边元素设置flex:1 */
        .father {
           display:flex;     /* 父元素设置flex布局 */ 
           height:300px;
        }
       .left {
            width: 300px;
            background-color: pink;
        }
        .right {   
             flex:1;           /* 右边元素设置flex:1 */
             background-color: blue;
        }

四、利用grid布局

复制代码
/* 利用grid布局 */
/* 父元素设置 display:gird; 属性、设置gird-template-columns:300px 1fr 属性*/
/* 表示第一列宽度始终为300px 第二列的宽度自适应 */
        .father {
           display:grid;    /*父元素设置 display:gird; */
           height:300px;
           grid-template-columns: 300px 1fr; /* 设置gird-template-columns:300px 1fr */
        }
       .left {
            background-color: pink;
        }
        .right {
             background-color: blue;
        }

五、利用定位

复制代码
/* 利用绝对定位 */
/* 父级设置为相对定位,子级设置为绝对定位 */
/* 左边子元素设置left为0,宽度300,右边子元素right设置为0 */
        .father {
            position: relative;  /* 父级设置为相对定位 */
        }
       .left {
            position: absolute; /* 子级设置为绝对定位 */
            width: 300px;
            height: 300px;
            left: 0;           /* 左边子元素设置left为0*/
            background-color: pink;
        }
        .right { 
             position: absolute;  /* 子级设置为绝对定位 */
             left: 300px;
             right: 0;           /* 右边子元素right设置为0*/
             height: 300px;
             background-color: blue;
        }

实现效果:

相关推荐
贾铭12 分钟前
如何实现一个网页版的剪映(五)如何跳转到视频某一帧
前端·后端
林恒smileZAZ15 分钟前
CSS 滚动驱动动画(scroll-timeline):无 JS 实现滚动特效
前端·javascript·css
俺不会敲代码啊啊啊16 分钟前
el-table实现行拖拽(包含展开项)
前端·vue.js·typescript
LIO16 分钟前
React Router 极简指南(v6+)
前端·react.js
明月_清风17 分钟前
从 AST 视角看透前端工程化:一条编译管线如何串联起所有工具
前端
架构源启18 分钟前
2026 进阶篇:Spring Boot响应式编程 + Spring AI 1.1.4 流式实战 + Vue前端完整实现(避坑指南)
java·前端·vue.js·人工智能·spring boot·spring·ai编程
白开水都有人用19 分钟前
前端 AES 加密 + 后端解密 + MD5 校验登录
前端
OpenTiny社区34 分钟前
还在手写 AI 聊天页?这款 Vue3 气泡组件,直接搞定流式对话!
前端·vue.js·ai编程
毛骗导演35 分钟前
Cladue Code 源码解析-键盘事件与 Vim 模式:parse-keypress 解析状态机
前端·架构
渐儿35 分钟前
GLB 模型压缩 — 完整流程与代码映射
前端