DAY22 搞样式--CSS Gird小进阶(对齐)

前言

CSS Gird 给我们非常大的弹性,我们可以决定元件(item)要在容器(Container)中的哪个位置之外,还可以决定元件(item)要如何对齐。
而且对齐的功能也设置的很弹性,我们可以选择在容器(Container)设定所有元件(item)统一的对齐方式,也可以为个别的元件设立属於他们自己的对齐方式,那一样直接让我们来试看看吧!
https://ithelp.ithome.com.tw/upload/images/20210922/20136833KJb55TmlnF.jpg

水平修改

  • 容器(Container)统一修改请配置 justify-items
  • 元件(item)个别特制请配置 justify-self

我们可以配置以下四种属性:

  1. start:startLine,对齐起始线
    https://ithelp.ithome.com.tw/upload/images/20210922/20136833KJb55TmlnF.jpg

  2. end:endLine,对齐终点线
    https://ithelp.ithome.com.tw/upload/images/20210922/20136833HVFArm8cDm.jpg

  3. center:center,对齐中间
    https://ithelp.ithome.com.tw/upload/images/20210922/20136833yE6cGFTXvp.jpg

  4. stretch:延展填满整个元件(item)(预设属性)
    https://ithelp.ithome.com.tw/upload/images/20210922/20136833jsqXzrwYls.jpg

垂直修改

  • 容器(Container)统一修改请配置 align-items
  • 元件(item)个别特制请配置 align-self

一样可以配置以下四种属性:

  1. start:startLine,对齐起始线
    https://ithelp.ithome.com.tw/upload/images/20210922/20136833KJb55TmlnF.jpg

  2. end:endLine,对齐终点线
    https://ithelp.ithome.com.tw/upload/images/20210922/20136833tI81q9vSfz.jpg

  3. center:center,对齐中间
    https://ithelp.ithome.com.tw/upload/images/20210922/2013683323eZ15YIDU.jpg

  4. stretch:延展填满整个元件(item)(预设属性)
    https://ithelp.ithome.com.tw/upload/images/20210922/20136833glDFAbOMtd.jpg

简单范例

  • 所有元件(item)水平/垂直都置中对齐
    https://ithelp.ithome.com.tw/upload/images/20210922/20136833aYQnpLuEad.jpg
    我们可以这样写:
.container {
  justify-items: center;
  align-items: center;
}
  • 个别元件(item)水平/垂直都置中对齐
    https://ithelp.ithome.com.tw/upload/images/20210922/20136833swX76EqtqH.jpg
    我们可以这样写:
.itemA {
  justify-self: center;
  align-self: center;
}

懒人补充

有没有觉得水平/垂直要分开写两行很麻烦的人?请举手!
好唷恭喜你,懒人缩写来了,我们来只写一行吧!

  • 容器(Container)统一修改请配置 place-items
  • 元件(item)个别特制请配置 place-self

垂直水平置中直接写:

.container{
    place-items: center;
}

阿捏有没有简单?

  • 如果只设定一个值,就会同时设定垂直 align-items 跟水平 justify-items 两个属性
  • 如果放两个值, 前面是垂直 align-items 属性/後面是水平 justify-items 属性

像这样就是垂直置中,水平置尾

.container{
    place-items: center end;
}

https://ithelp.ithome.com.tw/upload/images/20210922/20136833srvBpewElk.jpg

这样有没有很简单呢?
恭喜大家今天会 CSS Gird 对齐罗!可喜可贺!
明天见罗!


<<:  威胁建模作业的前奏

>>:  DAY7 Ngrok运行原理&安装Ngrok

Day07:部门与工程团队间协作的技巧(上)

一、前言   所谓的团队合作,我想最重要的就是如何沟通与使用有效之协作工具!沟通属於较偏人性与软性之...

Day15 用python写UI-聊聊Spinbox

第15天~~~完成了一半的铁人赛,之後也要继续加油! 今天要讲的内容是Spinbox,後面有几个实例...

企业资料通讯Week6 (2) | P2P architecture

P2P architecture P2P(Peer-to-peer,点对点的传输),这是一个无中心服...

新新新手阅读 Angular 文件 - Day07

学习目标 本文章将会是阅读官方文件Add navigation with routing 内容所做的...

Day22 切版笔记- 互动图文卡片

运用到的观念: 利用vertical-align: middle;调整图片预设多余的空间 使用po...