CSS布局那些事儿,两栏三栏我全会!

今天讲一讲css中常考的两栏布局和三栏布局。

两栏布局

是什么

两栏布局是一种常见的网页布局,用于将页面内容分成左右两个部分

实现方式

看下面代码,如果我们要实现左边内容固定200px,右边占据剩下的空间。应该怎么做呢?

html 复制代码
<div class="wrap">
    <div class="left">left</div>
    <div class="right">right</div> 
 </div>

flex

css 复制代码
<style>
    .wrap{
      height: 200px;
      display: flex;
    }
    .left{
      height: 100%;
      background-color: aqua;
      flex-basis: 200px;
    }
    .right{
      height: 100%;
      background-color: bisque;
      flex: 1;
    }
  </style>

grid

css 复制代码
<style>
    .wrap{
      height: 200px;
      display: grid;
      grid-template-columns: 200px 1fr;
    }
    .left{
      height: 100%;
      background-color: aqua;
    }
    .right{
      height: 100%;
      background-color: bisque;
    }
  </style>

float + BFC || margin-left

css 复制代码
<style>
    .wrap{
      height: 200px;
    }
    .left{
      height: 100%;
      width: 200px;
      background-color: aqua;
      float: left;
    }
    .right{
      height: 100%;
      background-color: bisque;
      overflow: auto; // margin-left: 200px;
    }
  </style>

效果图:

三栏布局

是什么

和两栏布局一样,是一种常见的网页布局,只是将页面内容分成了左中右三部分。

实现方式

  1. flex
  2. grid
  3. 左右先加载中间后加载:float
  4. 圣杯布局:float + margin负值 + position:relative(中间内容先加载)
  5. 双飞翼布局:float + margin负值(中间内容先加载)

前三种就过多赘述了,和两栏布局一样的,这里重点讲一下圣杯布局和双飞翼布局。

圣杯布局

html 复制代码
<style>
    .wrap{
      height: 200px;
      margin: 0 200px; // 左右两边留下边距用于放左右的内容
    }
    .left{
      height: 100%;
      width: 200px;
      background-color: aqua;
      float: left;
      position: relative;
      right: 200px;
      margin-left: -100%;
    }
    .right{
      height: 100%;
      width: 200px;
      background-color: bisque;
      float: right;
      position: relative;
      left: 200px;
      margin-left: -100%;
    }
    .content{
      height: 100%;
      width: 100%;
      background-color: rgb(15, 186, 55);
      float: left;
    }
    
  </style>
</head>
<body>
  <div class="wrap">
    <div class="content">content</div>
    <div class="left">left</div>
    <div class="right">right</div> 
  </div>
</body>

双飞翼布局

html 复制代码
<style>
    .wrap{
      height: 200px;
    }
    .left{
      height: 100%;
      width: 200px;
      background-color: aqua;
      float: left;
      margin-left: -100%;
    }
    .right{
      height: 100%;
      width: 200px;
      background-color: bisque;
      float: right;
      margin-left: -100%;
    }
    .container{
      height: 100%;
      width: 100%;
      background-color: rgb(15, 186, 55);
      padding: 0 200px;
      box-sizing: border-box;
      float: left;
    }
    
  </style>
</head>
<body>
  <div class="wrap">
    <div class="container">
      <div class="content">content</div>
    </div>
    <div class="left">left</div>
    <div class="right">right</div> 
  </div>
</body>

效果图

假如您也和我一样,在准备春招。欢迎加我微信shunwuyu,这里有几十位一心去大厂的友友可以相互鼓励,分享信息,模拟面试,共读源码,齐刷算法,手撕面经。来吧,友友们!

相关推荐
SchuylerEX1 分钟前
第六章 JavaScript 互操(2).NET调用JS
前端·c#·.net·blazor·ui框架
东风西巷1 小时前
Rubick:基于Electron的开源桌面效率工具箱
前端·javascript·electron·软件需求
探码科技1 小时前
AI知识管理软件推荐:九大解决方案与企业应用
前端·ruby
编程小黑马1 小时前
解决flutter 在控制器如controller 无法直接访问私有类方法的问题
前端
unfetteredman2 小时前
Error: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found
前端·javascript·vite
云存储小精灵2 小时前
Dify x 腾讯云 COS MCP:自然语言解锁智能数据处理,零代码构建 AI 新世界
前端·开源
山间板栗2 小时前
微信小程序环境变量设置方案
前端
电商API大数据接口开发Cris2 小时前
Java Spring Boot 集成淘宝 SDK:实现稳定可靠的商品信息查询服务
前端·数据挖掘·api
pepedd8642 小时前
LangChain:大模型开发框架的全方位解析与实践
前端·llm·trae
HANK2 小时前
KLineChart 绘制教程
前端·vue.js