【CSS基础知识】flex布局、table 布局 和 grid布局

一、flex布局

参考文章

1.1 flex介绍

display:flex 或 display:inline-flex 声明为弹性容器

flex-wrap换行

justify-content

在这里插入图片描述

align-items

在这里插入图片描述

align-content多行对齐

子元素在交叉轴上的对齐方式
在这里插入图片描述

自我对齐 align-self (作用在子级元素

相当于覆盖父级的align-items
在这里插入图片描述

flex-grow

在容器主轴上存在剩余空间时, flex-grow才有意义
定义的是可放大的能力,0 (默认)禁止放大,大于 0 时按占的比重分放大,负数无效

flex-shrink

在这里插入图片描述###

1.2 flex布局实战

实现盒子3等分

在这里插入图片描述

<template>
  <div class="father">
    <div class="son1">Son111</div>
    <div class="son2">Son222</div>
    <div class="son3">Son333</div>
  </div>
</template>

<script setup></script>
<style lang="scss" scoped>
  .father {
    display: flex;
    height: 200px;
    border: 1px solid red;
    .son1 {

      flex: 1;
      background-color: purple;
    }
    .son2 {
      flex: 1;
      background-color: green;
    }
    .son3 {
      flex: 1;
      background-color: skyblue;
    }
  }
</style>

实现盒子2等分

同上

3栏布局,中间自适应

<template>
  <div class="father">
    <div class="son1">Son111</div>
    <div class="son2">Son222</div>
    <div class="son3">Son333</div>
  </div>
</template>

<script setup></script>
<style lang="scss" scoped>
  .father {
    --son1-width: 50px;
    --son3-width: 150px;

    display: flex;
    height: 200px;
    border: 1px solid red;
    .son1 {

      width: var(--son1-width);
      background-color: purple;
    }
    .son2 {
      flex: 1;
      background-color: green;
    }
    .son3 {
      width: var(--son3-width);
      background-color: skyblue;
    }
  }
</style>

2栏布局,右侧自适应

<template>
  <div class="father">
    <div class="son1">Son111</div>
    <div class="son2">Son222</div>
  </div>
</template>

<script setup></script>
<style lang="scss" scoped>
  .father {
    --son1-width: 50px;
    --son3-width: 150px;

    display: flex;
    height: 200px;
    border: 1px solid red;
    .son1 {

      width: var(--son1-width);
      background-color: purple;
    }
    .son2 {
      flex: 1;
      background-color: green;
    }
  }
</style>

水平垂直居中

在这里插入图片描述

<template>
  <div class="father">
    <div class="son1">Son111</div>
  </div>
</template>

<script setup></script>
<style lang="scss" scoped>
  .father {
    --son1-width: 50px;
    --son3-width: 150px;

    display: flex;
    align-items: center; 
    justify-content: center;
    height: 200px;
    border: 1px solid red;
    .son1 {
      width: var(--son1-width);
      height: 50px;
      background-color: purple;
    }
  }
</style>

二、table布局

实现盒子3等分

table布局默认就是等分的
在这里插入图片描述

 <style >
    .father{
        width: 600px;
        height: 200px;
        
        /* display: table-row;不行,是行内元素了 */
        display: table;
        background-color: #db7b7b;
    }
    .inner{
        display: table-cell;
    }
  </style>
<body>
    <div class="father"> 
        <div class="inner" style="background-color: #677e80;"> A </div>
        <div class="inner" style="background-color: #7bdb8d;"> B </div>
        <div class="inner" style="background-color: #d3c3c3;"> C </div>
    </div>
 
</body>

实现盒子2等分

同上

三栏布局,中间自适应

在这里插入图片描述

<style >
    .father{
        width: 100%;
        height: 200px;
        
        /* display: table-row;不行,是行内元素了 */
        display: table;
        background-color: #db7b7b;
    }
    .inner{
        display: table-cell;
    }
    .inner1{
        width: 100px;
    }
    .inner2{
        
    }
    .inner3{
        width: 50px;
    }
  </style>
<body>
    <div class="father"> 
        <div class="inner inner1" style="background-color: #677e80;"> A </div>
        <div class="inner"        style="background-color: #7bdb8d;"> B </div>
        <div class="inner inner3" style="background-color: #d3c3c3;"> C </div>
    </div>
 
</body>

两栏布局,右侧自适应

同上

水平垂直居中

<style >
    .father{
        width: 600px;
        height: 200px;

        display: table-cell;
        text-align: center;
        vertical-align: middle;

        background-color: #db7b7b;
    }
    .son{
        display: inline-block;
    }
   
  </style>
<body>
    <div class="father"> 
        <div class="son" style="background-color: #677e80;"> A </div>
        
    </div>

</body>

三、grid布局

大佬文章-超详细!!

3.1 grid布局介绍

号称是最强大的的 CSS 布局方案,是目前唯一一种 CSS 二维布局。

与flex布局的区别

flex是一维布局,一次只能处理一个维度上的元素布局,一行或者一列。
grid是二维布局,一次只能处理一个维度上的元素布局,一行或者一列。
在这里插入图片描述

创建Grid布局

display:grid 该容器是一个块级元素
display:inline-grid 该容器是一个行内块元素

设置行高、列宽

grid-template-rows 设置网格的行高
grid-template-columns 定义列宽

1. 固定列宽和行高

grid-template-columns: 200px 100px 200px;设置3列,列宽分别是200 、100、200
grid-template-rows: repeat(2, 50px);等价于 grid-template-rows: repeat(2, 50px);

2. 设置份数

grid-template-columns: 200px 1fr 2fr;
在这里插入图片描述

grid-template-columns: 1fr 2fr 3fr;
在这里插入图片描述

3. auto

grid-template-columns: 100px auto 100px;第一第三列为 100px,中间由浏览器决定长度

布局

justify-content 属性、align-content 属性 place-content

justify-content 属性是整个内容区域在容器里面的水平位置(左中右),align-content 属性是整个内容区域的垂直位置(上中下)。它们都有如下的属性值。
place-content属性是align-content属性和justify-content属性的合并简写形式。如果省略第二个值,浏览器就会假定第二个值等于第一个值。

.container {
  justify-content: start | end | center | stretch | space-around | space-between | space-evenly;
  align-content: start | end | center | stretch | space-around | space-between | space-evenly;  
}
  • space-around - 每个项目两侧的间隔相等。所以,项目之间的间隔比项目与容器边框的间隔大一倍
  • space-between - 项目与项目的间隔相等,项目与容器边框之间没有间隔
  • space-evenly - 项目与项目的间隔相等,项目与容器边框之间也是同样长度的间隔
  • stretch - 项目大小没有指定时,拉伸占据整个网格容器

在这里插入图片描述

justify-items 属性、align-items 和 place-items

justify-items 属性设置单元格内容的水平位置(左中右),align-items 属性设置单元格的垂直位置(上中下)
place-items属性是align-items属性和justify-items属性的合并简写形式。如果省略第二个值,则浏览器认为与第一个值相等。

.container {
  justify-items: start | end | center | stretch (默认;
  align-items: start | end | center | stretch (默认;
}

在这里插入图片描述

justify-self 属性、align-self 属性 place-self

justify-self 属性设置单元格内容的水平位置(左中右),跟 justify-items 属性的用法完全一致,但只作用于单个项目,所以属性要写在子级元素上

align-self 属性设置单元格内容的垂直位置(上中下),跟align-items属性的用法完全一致,也是只作用于单个项目
两者很相像,这里只拿 justify-self 属性演示,align-self 属性同理,只是作用于垂直方向
在这里插入图片描述

基本代码

<template>
  <div class="wrapper">
    <div class="one item">One</div>
    <div class="two item">Two</div>
    <div class="three item">Three</div>
    <div class="four item">Four</div>
    <div class="five item">Five</div>
    <div class="six item">Six</div>
  </div>
</template>
<style lang="scss" scoped>
  .wrapper {
    display: grid;
    grid-template-rows: 100px 200px;
    grid-template-columns: 50px 100px 200px;
    grid-gap: 20px;
    width: 800px;
    border: 1px solid red;
  }
  .one {
    background: #19caad;
  }
  .two {
    background: #8cc7b5;
  }
  .three {
    background: #d1ba74;
  }
  .four {
    background: #bee7e9;
  }
  .five {
    background: #e6ceac;
  }
  .six {
    background: #ecad9e;
  }
  .item {
    color: #ffffff;
  }
</style>


效果:
在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述

3.2 grid布局实战

实现盒子3等分

 <style >
    .father{
        width: 600px;
        height: 200px;

        display: grid;

        /* 这三个都行 */
        /* grid-template-columns:  repeat(3,33.3%)  ; */
        grid-template-columns: repeat(3,200px);
        /* grid-template-columns: 1fr 1fr 1fr; */
        /* grid-template-columns: repeat(3, 1fr); */
        background-color: #db7b7b;
    }
    
   
  </style>
<body>
    <div class="father"> 
        <div class="inner inner1" style="background-color: #677e80;"> A </div>
        <div class="inner inner2" style="background-color: #7bdb8d;"> B </div>
        <div class="inner inner2" style="background-color: #dbc07b;"> C </div>
    </div>

</body>

实现盒子2等分

同上

三栏布局,中间自适应

在这里插入图片描述

 <style >
    .father{
        width: 600px;
        height: 200px;

        display: grid;
       
        grid-template-columns: 100px auto 50px;
      
        background-color: #db7b7b;
    }
    
   
  </style>
<body>
    <div class="father"> 
        <div class="inner inner1" style="background-color: #677e80;"> A </div>
        <div class="inner inner2" style="background-color: #7bdb8d;"> B </div>
        <div class="inner inner2" style="background-color: #dbc07b;"> C </div>
    </div>

</body>

两栏布局,右侧自适应

同上

水平垂直居中

在这里插入图片描述

<style >
    .father{
        width: 600px;
        height: 200px;

        display: grid;
        place-items: center;
			// 相当于align-items: center;justify-items: center;
  
        background-color: #db7b7b;
    }
    
   
  </style>
<body>
    <div class="father"> 
        <div class="son" style="background-color: #677e80;"> A </div>
    </div>

</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值