圣杯布局和双飞翼布局

简介: 圣杯布局和双飞翼布局

54. 圣杯布局和双飞翼布局

一、圣杯布局实现

1. 使用浮动和负边距
<style>
    .container {
    
      padding-left: 200px;
      padding-right: 200px;
    }
    .left, .right {
    
      width: 200px;
      height: 200px;
      background: tomato;
      float: left;
      position: relative;
    }
    .left {
    
      margin-left: -100%;
      right: 200px;
    }
    .right {
    
      margin-left: -200px;
      left: 200px;
    }
    .center {
    
      float: left;
      width: 100%;
      height: 200px;
      background: skyblue;
    }
</style>
<div class="container">
  <div class="center">中央列</div>
  <div class="left">左侧列</div>
  <div class="right">右侧列</div>
</div>

这种实现方式使用了浮动和负边距。中央列占据整个父容器的宽度,左右两侧列使用了负的margin-left将它们移到正确的位置。同时,为了防止左右两侧列遮挡中央列的内容,需要给父容器添加左右两侧的padding

2. 使用Flex布局
<style>
    .container {
    
      display: flex;
      flex-direction: row;
      justify-content: space-between;
    }
    .left, .right {
    
      width: 200px;
      height: 200px;
      background: tomato;
    }
    .center {
    
      flex: 1;
      height: 200px;
      background: skyblue;
    }
</style>
<div class="container">
  <div class="left">左侧列</div>
  <div class="center">中央列</div>
  <div class="right">右侧列</div>
</div>

这种实现方式使用了flex布局。通过给父容器设置display: flex,将中央列设置为flex: 1,使其自动占据剩余的宽度。

3. 使用定位
<style>
    .container {
    
      position: relative;
    }

    .center {
    
      height: 200px;
      background: skyblue;
      margin-left: 200px;
      margin-right: 200px;
    }
    .left, .right {
    
      width: 200px;
      height: 200px;
      position: absolute;
      top: 0;
      background: tomato;
    }
    .left {
    
      left: 0;
    }
    .right {
    
      right: 0;
    }
</style>
<div class="container">
  <div class="center">中央列</div>
  <div class="left">左侧列</div>
  <div class="right">右侧列</div>
</div>

这种实现方式使用了position。中央列通过设置左右两侧的margin来使其占据正确的位置,其中,左侧列的left值为0,而右侧列的right值为0,这样可以将它们固定在页面的左右两侧。

二、双飞翼布局实现

<style>
/* 双飞翼 */
  .container {
    
    width: 100%;
    float: left;
  }
  .center {
    
    margin-left: 200px;
    margin-right: 200px;
    height: 200px;
    background: skyblue;
  }
  .left {
    
    width: 200px;
    height: 200px;
    background: tomato;
    float: left;
    margin-left: -100%;
  }
  .right {
    
    width: 200px;
    height: 200px;
    background: tomato;
    float: left;
    margin-left: -200px;
  }
</style>
<!-- 双飞翼 -->
<div class="container">
  <div class="center">中央列</div>
</div>
<div class="left">左侧列</div>
<div class="right">右侧列</div>

这种实现方式使用了浮动和负边距。中央列占据整个父容器的宽度,并设置了左右两侧的margin使其不会被遮挡。左右两侧列使用了负的margin-left将它们移到正确的位置,而为了防止左右两侧列遮挡中央列的内容,需要给中央列设置左右两侧的margin

需要注意的是,由于左右两侧列是浮动的,所以它们的高度无法撑开父容器,需要在父容器上添加overflow:hidden来防止内容溢出。此外,由于左右两侧列的宽度是固定的,如果需要适应不同的屏幕尺寸,可以使用媒体查询来改变它们的宽度。

相关文章
|
开发框架 缓存 NoSQL
基于SqlSugar的数据库访问处理的封装,在.net6框架的Web API上开发应用
基于SqlSugar的数据库访问处理的封装,在.net6框架的Web API上开发应用
|
11月前
|
JavaScript 前端开发 UED
Vue与uni-app开发中通过@font-face巧妙引入自定义字体
Vue与uni-app开发中通过@font-face巧妙引入自定义字体
727 9
IDEA之Stream表达式生成、调试
IDEA之Stream表达式生成、调试
413 1
|
Unix Linux 开发者
Python系统编程大挑战:轻松应对跨平台难题,让你的代码无处不在
【8月更文挑战第6天】Python 以简洁的语法和强大的库支持著称,为系统编程提供高效且易实现跨平台应用的途径。通过 `platform`、`os` 和 `subprocess` 模块,Python 可以优雅地处理不同操作系统间的差异,如获取操作系统信息。`pathlib` 模块则简化了文件路径处理,自动适配不同系统的路径格式。这些特性使得 Python 成为编写可移植代码的理想选择。
161 0
|
运维 架构师 算法
全球仅通过不到 2000 位的 Elastic 认证工程师,到底难不难?
全球仅通过不到 2000 位的 Elastic 认证工程师,到底难不难?
|
Java Linux Maven
表弟使用nexus搭建Maven私服全过程(含所遇问题及解决方案)下
表弟使用nexus搭建Maven私服全过程(含所遇问题及解决方案)
396 0
Yu9
|
弹性计算 Ubuntu Linux
我的ECS使用体验报告
阿里云这边更换操作系统特别方便,速度也快
Yu9
234 0
|
IDE 开发工具
PM - Axure RP 基础篇
PM - Axure RP 基础篇
186 0
PM - Axure RP 基础篇