当前位置: 首页 > news >正文

CSS | CSS实现两栏布局(左边定宽 右边自适应,左右成比自适应)

目录

一、左边定宽 右边自适应

1.浮动

2.利用浮动+margin

3.定位+margin

4.flex布局

5.table 布局

二、左右成比自适应

1:1

1flex布局

table布局

1:2

flex布局


<div class="father"><div class="left">左边自适应</div><div class="right">右边自适应</div></div>

一、左边定宽 右边自适应

实现效果:

   1.浮动

.left {width: 100px;background-color: rebeccapurple;float: left;
}
.right {width: calc(100%-100px);background-color: green;
}

   2.利用浮动+margin

.left {width: 100px;background-color: rebeccapurple;float: left;
}.right {background-color: green;margin-left: 100px;
}

3.定位+margin

.father {position: relative;
}.left {position: absolute;width: 100px;left: 0;background-color: rebeccapurple;
}.right {background-color: green;margin-left: 100px;
}

4.flex布局

.father {display: flex;
}.left {width: 100px;background-color: rebeccapurple;
}.right {flex: 1;/* flex: 1可以让其填充剩余的空间,以达到拉伸的效果 */background-color: green;
}

5.table 布局

.father {width: 100%;display: table;
}.left {display: table-cell;width:300px;background-color: rebeccapurple;
}.right {display: table-cell;background-color: green;
}

二、左右成比自适应

1:1

1flex布局
.father {display: flex;
}
.left {width: 200px;height: 200px;background-color: rebeccapurple;flex: 1;}
.right {width: 200px;height: 200px;background-color: green;flex: 1;/* 放大且缩小并等分所有空间 *//* flex:1等于flex-grow: 1、flex-shrink: 1、flex-basic: 0% */
}
table布局
.father {width: 100%;display: table;
}.left {display: table-cell;height: 200px;background-color: rebeccapurple;
}.right {display: table-cell;height: 200px;background-color: green;
}

1:2

flex布局
.father {display: flex;
}
.left {width: 200px;height: 200px;background-color: rebeccapurple;flex: 1;
}
.right {width: 200px;height: 200px;background-color: green;flex: 2;/* flex:1等于flex-grow: 2、flex-shrink: 2、flex-basic: 0% */}

等分所有空间,宽度比左右=1:2

flex 布局的默认主轴方向为 row ,所以 flex-basis 属性 默认是控制元素的 width

http://www.lryc.cn/news/519901.html

相关文章:

  • acwing_3195_有趣的数
  • Liunx-搭建安装VSOMEIP环境教程 执行 运行VSOMEIP示例demo
  • Git | git revert命令详解
  • ASP.NET Core 中,Cookie 认证在集群环境下的应用
  • Flyte工作流平台调研(五)——扩展集成
  • 【AUTOSAR 基础软件】软件组件的建立与使用(“代理”SWC)
  • java通过ocr实现识别pdf中的文字
  • Git 命令代码管理详解
  • Docker的安装和使用
  • Flink系统知识讲解之:Flink内存管理详解
  • 使用JMeter模拟多IP发送请求!
  • 【Ubuntu与Linux操作系统:六、软件包管理】
  • 【数据结构-堆】力扣1834. 单线程 CPU
  • 【前端动效】原生js实现拖拽排课效果
  • C#使用OpenTK绘制3D可拖动旋转图形三棱锥
  • 排序的本质、数据类型及算法选择
  • Python的列表基础知识点(超详细流程)
  • HarmonyOS鸿蒙开发 弹窗及加载中指示器HUD功能实现
  • 【Ubuntu与Linux操作系统:一、Ubuntu安装与基本使用】
  • React 元素渲染
  • 【2024年华为OD机试】 (C卷,100分)- 括号匹配(Java JS PythonC/C++)
  • 解锁企业数字化转型新力量:OpenCoze(开源扣子)
  • 【网络云SRE运维开发】2025第2周-每日【2025/01/12】小测-【第12章 rip路由协议】理论和实操考试题解析
  • 【微服务】8、分布式事务 ( XA 和 AT )
  • CVE-2025-22777 (CVSS 9.8):WordPress | GiveWP 插件的严重漏洞
  • TypeScript Jest 单元测试 搭建
  • 基于 SSH 的任务调度系统
  • filestream安装使用全套+filebeat的模块用法
  • java项目之房屋租赁系统源码(springboot+mysql+vue)
  • sap mm学习笔记