暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

CSS Grid 网格布局:「12」使用 align-items 属性垂直对齐网格所有项目

路条编程 2020-10-27
1864


欢迎关注路条编程网站,通过学习 CSS Grid 网格布局这一系列文章,你将会逐步掌握网格布局的容器和项目、行和列、单元格、网络线等相关的知识。


使用 align-items 属性垂直对齐网格所有项目。

align-items 指定了网格元素的垂直呈现方式,是垂直拉伸显示,还是顶部居中底部对齐,语法如下所示:
<style type="text/css">
.container {
align-items: start | end | center | stretch;
}
</style>
属性及相应描述如下所示:

  值                       描述
start     网格垂直尺寸收缩为内容大小,同时沿着上网格线顶部对齐显示。
end       网格垂直尺寸收缩为内容大小,同时沿着下网格线底部对齐显示。
center    网格垂直尺寸收缩为内容大小,同时在当前网格区域内部垂直居中对齐显示。
stretch   默认值,拉伸。表现为垂直填充。

左中右及拉伸对齐语法示例如下所示:
<style type="text/css">
.container {
align-items: start ;
}
</style>

<style type="text/css">
.container {
align-items: center ;
}
</style>

<style type="text/css">
.container {
align-items: end ;
}
</style>

<style type="text/css">
.container {
align-items: stretch ;
}
</style>

调整页面框架模块 9 ,增加一个 3 行 3 列容器,容器中包含 9 个项目。本示例只增加了垂直底端对齐方式,容器主样式类为 .grid-gap-main .align-items-end ,定义 3 列宽度分别为 100px、50px、100px, 定义 3 行高度分别为 80px、auto
、80px。列间距为 10px, 行间距为 15px。调整后的样式代码如下所示:
<style type="text/css">
.align-items-end {
align-items: end;
}
</style>
调整 html 代码如下所示:
<article class="article  ant-col ant-col-xs-24 ant-col-sm-12 ant-col-md-12 ant-col-lg-12 ant-col-xl-6">
<div class="card">
<div class="container grid-gap-main align-items-end">
<div class="box item1">1</div>
<div class="box item2">2</div>
<div class="box item3">3</div>
<div class="box item4">4</div>
<div class="box item5">5</div>
<div class="box item6">6</div>
<div class="box item7">7</div>
<div class="box item8">8</div>
<div class="box item9">9</div>
</div>
</div>
</article>
保存页面代码,预览页面,整体效果图如下所示:


重要提示:示例效果预览,请访问 https://www.icoderoad.com , 完整代码查看及下载,请访问  https://github.com/icoderoad/demo。


今天就讲到这里,如果有问题需要咨询,大家可以直接留言或扫下方二维码关注公众号。也可以添加 happyzjp 微信受邀加入学习社群,我们会尽力为你解答。



作者:路条编程(转载请获本公众号授权,并注明作者与出处)





文章转载自路条编程,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论