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;
        }

实现效果:

相关推荐
lizhongxuan3 小时前
Claude Code 防上下文爆炸:源码级深度解析
前端·后端
柳杉5 小时前
震惊!字符串还能这么玩!
前端·javascript
是上好佳佳佳呀5 小时前
【前端(五)】CSS 知识梳理:浮动与定位
前端·css
wefly20176 小时前
纯前端架构深度解析:jsontop.cn,JSON 格式化与全栈开发效率平台
java·前端·python·架构·正则表达式·json·php
我命由我123457 小时前
React - 类组件 setState 的 2 种写法、LazyLoad、useState
前端·javascript·react.js·html·ecmascript·html5·js
自由生长20248 小时前
IndexedDB的观察
前端
IT_陈寒8 小时前
Vite热更新坑了我三天,原来配置要这么写
前端·人工智能·后端
斯班奇的好朋友阿法法8 小时前
离线ollama导入Qwen3.5-9B.Q8_0.gguf模型
开发语言·前端·javascript
掘金一周8 小时前
每月固定续订,但是token根本不够用,掘友们有无算力焦虑啊 | 沸点周刊 4.2
前端·aigc·openai
小村儿8 小时前
连载加餐01-claude code 源码泄漏 ---一起吃透 Claude Code,告别 AI coding 迷茫
前端·后端·ai编程