圣杯布局的实现方式

Posted Her...

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了圣杯布局的实现方式相关的知识,希望对你有一定的参考价值。

1.什么是圣杯布局?

左右盒子固定,中间盒子自适应

2.实现方式

(1)flex布局

思路:左右盒子给固定的宽高,中间盒子flex:1

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  *
    margin: 0;
    padding: 0;
 

  .box
    display: flex;
    width: 100vw;
    height: 100vh;
 

  .one
    width: 20vw;
    height: 20vh;
    background-color: yellowgreen;
 

  .two
    flex: 1;
    height: 20vh;
    background-color: plum;
 

  .three
    width: 20vw;
    height: 20vh;
    background-color: pink;
 

</style>

<body>

<div class="box">
  <div class="one">盒子1</div>
  <div class="two">盒子2</div>
  <div class="three">盒子3</div>
</div>

</body>

</html>

(2)浮动

思路:左盒子左浮,右盒子右浮,中间盒子不动,要注意布局顺序,盒子3在盒子2前面

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  *
    margin: 0;
    padding: 0;
 

  .box
    width: 100vw;
    height: 100vh;
 

  .one
    float: left;
    width: 20vw;
    height: 20vh;
    background-color: yellowgreen;
 

  .two
    height: 20vh;
    background-color: yellow;
 

  .three
    float: right;
    width: 20vw;
    height: 20vh;
    background-color: pink;
 
</style>

<body>

<div class="box">
  <div class="one">盒子1</div>
  <div class="three">盒子3</div>
  <div class="two">盒子2</div>
</div>

</body>

</html>

(3)定位

思路:父级相对定位、左右绝对定位,中间盒子不动

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  *
    margin: 0;
    padding: 0;
 

  .box
    position: relative;
    width: 100vw;
    height: 100vh;
 

  .one
    position: absolute;
    top: 0;
    left: 0;
    width: 20vw;
    height: 20vh;
    background-color: yellowgreen;
 

  .two
    height: 20vh;
    background-color: plum;
 

  .three
    position: absolute;
    top: 0;
    right: 0;
    width: 20vw;
    height: 20vh;
    background-color: pink;
 
</style>

<body>
  <div class="box">
    <div class="one">盒子1</div>
    <div class="two">盒子2</div>
    <div class="three">盒子3</div>
  </div>
</body>

</html>

以上是关于圣杯布局的实现方式的主要内容,如果未能解决你的问题,请参考以下文章

CSS3--Flex弹性盒子布局: 实例篇-圣杯布局

响应式布局之浮动圣杯布局(双飞翼布局)—-自适应宽度布局

三种方式实现圣杯布局

三种方式实现圣杯布局

css圣杯布局的三种方式与双飞翼布局

圣杯布局和双飞翼布局