CSS中两栏布局的实现

一般两栏布局指的是左边一栏宽度固定,右边一栏宽度自适应,两栏布局的具体实现:

  • 利用浮动,将左边元素宽度设置为200px,并且设置向左浮动。将右边元素的margin-left设置为200px,宽度设置为auto(默认为auto,撑满整个父元素)。
.outer {
  height: 100px;
}
.left {
  float: left;
  width: 200px;
  background: tomato;
}
.right {
  margin-left: 200px;
  width: auto;
  background: gold;
}
  • 利用浮动,左侧元素设置固定大小,并左浮动,右侧元素设置overflow: hidden; 这样右边就触发了BFC,BFC的区域不会与浮动元素发生重叠,所以两侧就不会发生重叠。
.left{
     width: 100px;
     height: 200px;
     background: red;
     float: left;
 }
 .right{
     height: 300px;
     background: blue;
     overflow: hidden;
 }
  • 利用flex布局,将左边元素设置为固定宽度200px,将右边的元素设置为flex:1。
.outer {
  display: flex;
  height: 100px;
}
.left {
  width: 200px;
  background: tomato;
}
.right {
  flex: 1;
  background: gold;
}
  • 利用绝对定位,将父级元素设置为相对定位。左边元素设置为absolute定位,并且宽度设置为200px。将右边元素的margin-left的值设置为200px。
.outer {
  position: relative;
  height: 100px;
}
.left {
  position: absolute;
  width: 200px;
  height: 100px;
  background: tomato;
}
.right {
  margin-left: 200px;
  background: gold;
}
  • 利用绝对定位,将父级元素设置为相对定位。左边元素宽度设置为200px,右边元素设置为绝对定位,左边定位为200px,其余方向定位为0。
.outer {
  position: relative;
  height: 100px;
}
.left {
  width: 200px;
  background: tomato;
}
.right {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 200px;
  background: gold;
}

相关推荐

  1. CSS布局实现

    2024-03-15 09:58:02       47 阅读
  2. CSS布局实现

    2024-03-15 09:58:02       43 阅读
  3. 实现布局

    2024-03-15 09:58:02       35 阅读
  4. 如何用css实现布局?

    2024-03-15 09:58:02       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 09:58:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 09:58:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 09:58:02       82 阅读
  4. Python语言-面向对象

    2024-03-15 09:58:02       91 阅读

热门阅读

  1. Union和union导致的数据不一致

    2024-03-15 09:58:02       41 阅读
  2. pxe安装mini centos系统

    2024-03-15 09:58:02       39 阅读
  3. 【备忘录】kafka常用命令维护

    2024-03-15 09:58:02       36 阅读
  4. postman学习

    2024-03-15 09:58:02       33 阅读
  5. html5&css&js代码 011 个人简历二

    2024-03-15 09:58:02       37 阅读
  6. c 语言stdlib.h介绍

    2024-03-15 09:58:02       35 阅读