1 line of CSS Layouts

10 modern layouts in 1 line of CSS

1. 绝对居中布局


<div class="container" >
<div class="item" contenteditable></div>
</div>
.container {
display: grid;
place-items: center;
}

place-items:

place-items 是 和 简写形式

align-items 属性控制垂直对齐

justify-items 属性控制水平对齐


place-items: <align-items> <justify-items>; place-items: center;
// 等同于
place-items: center center;
// 等同于
align-items: center;
justify-items: center;

2. 并列式多栏布局

<div class="contianer">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>

.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
} .item {
/* flex-grow、flex-shrink、flex-basis */
flex: 1 1 150px;
/* Stretching: */
margin: 5px;
} .item {
/* flex-grow、flex-shrink、flex-basis */
flex: 0 1 150px;
/* No stretching: */
margin: 5px;
}

flex: 0 1

flex 是 、 、 简写形式

flex-shrink:如果容器宽度不足,项目是否可以缩小;

flex-grow:如果容器有多余宽度,项目是否可以扩大;

flex-basis:项目的初始宽度;


flex: <flex-grow> <flex-shrink> <flex-basis>; flex: 1 1 150px;
// 等同于
flex-grow: 1;
flex-shrink: 1;
flex-basis: 150px; flex: 0 1 150px
// 等同于
flex-grow: 0;
flex-shrink: 1;
flex-basis: 150px;

3. 左右两栏式布局

侧边栏固定宽度(可以在一定范围内动态变化),主栏宽度自适应

<div class="container">
<div class="item" contenteditable>
Min: 150px / Max: 25%
</div>
<div class="item" contenteditable>
This element takes the second grid position (1fr), meaning
it takes up the rest of the remaining space.
</div>
</div>
.container {
display: grid;
grid-template-columns: minmax(150px, 25%) 1fr;
}

grid-template-columns: minmax(, ) ...

CSS Units Functions: calc(), minmax(), var(--variable), repeat()

grid-template-columns 指定页面分成两列

第一列的宽度是 minmax(150px, 25%),即最小宽度为150px,最大宽度为总宽度的25%;

第二列为 1fr,即所有剩余宽度;

grid-template 是 、、 简写形式


// 等同于
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows:

https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template

CSS 1fr

CSS Grid Layout 引入了一个新的长度单位,称为 fr,它是“分数”的缩写。

MDN 将 fr 单位定义为一个表示网格容器中可用空间的一部分的单位。

Fractional unit / 小数单位

.container {
display: grid;
width: 800px;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 150px 150px;
grid-gap: 1rem;
} // 等同于 .container {
display: grid;
width: 800px;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 150px);
grid-gap: 1rem;
}

https://mozilladevelopers.github.io/playground/css-grid/04-fr-unit/

https://codepen.io/xgqfrms/pen/bGeydOY

4. 三明治布局, 上中下布局

页眉、内容区、页脚

header main footer

布局始终会占满整个页面高度

<div class="container">
<header class="item" contenteditable>
Header Content
</header>
<main class="itemn" contenteditable>
Main Content
</main>
<footer class="item" contenteditable>
Footer Content
</footer>
</div>
.container {
display: grid;
grid-template-rows: auto 1fr auto;
}

auto 由内容的高度决定

1fr 占用所有剩余空间的比例

grid-template-rows 指定垂直方向布局,这里是从上到下划分三部分。

第一部分(页眉)和第三部分(页脚)的高度都为 auto,即本来的内容高度;

第二部分(内容区)的高度为 1fr,即剩余的所有高度,这可以保证页脚始终在容器的底部。


// 等同于

5. 圣杯布局

页眉

内容区(左边栏、主栏、右边栏)

页脚

<div class="containert">
<header class="section">
Header
</header>
<div class="left-side section" contenteditable>
Left Sidebar
</div>
<main class="section" contenteditable>
Main Content
</main>
<div class="right-side section" contenteditable>
Right Sidebar
</div>
<footer class="section">
Footer
</footer>
</div>

.container {
display: grid;
grid-template: auto 1fr auto / auto 1fr auto;
}
header {
padding: 2rem;
grid-column: 1 / 4;
}
.left-side {
grid-column: 1 / 2;
}
main {
grid-column: 2 / 3;
}
.right-side {
grid-column: 3 / 4;
}
footer {
grid-column: 1 / 4;
}

grid-template 是 / 简写形式

grid-template-rows(垂直方向) 山下布局

grid-template-columns(水平方向)左右布局

grid-template: auto 1fr auto / auto 1fr auto; 表示页面在垂直方向和水平方向上,都分成三个部分

  1. 首先垂直方向将布局划分为 (上 中 下) 3 部分
  2. 其次水平方向再将 (上 中 下) 三部分,分别划分为 (左 中 右) 3 部分
grid-template: auto 1fr auto / auto 1fr auto;
// 等同于
grid-template-columns auto 1fr auto;
grid-template-rows:: auto 1fr auto;

转换成 三明治布局, 上中下布局 ???

<div class="containert">
<header class="section">
Header
</header>
<main class="section" contenteditable>
<div class="left-side section" contenteditable>
Left Sidebar
</div>
<div class="center section" contenteditable>
Main Content
</div>
<div class="right-side section" contenteditable>
Right Sidebar
</div>
</main>
<footer class="section">
Footer
</footer>
</div>

6.



7.



8.



9.



10.



Grid Layout



Flex Layout



refs

https://1linelayouts.glitch.me/

https://www.youtube.com/watch?v=qm0IfG1GyZU

https://codepen.io/una

http://www.ruanyifeng.com/blog/2020/08/five-css-layouts-in-one-line.html

http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

http://www.ruanyifeng.com/blog/2019/03/grid-layout-tutorial.html

https://www.mozilla.org/zh-CN/firefox/browsers/

https://www.mozilla.org/en-US/firefox/channel/desktop/#nightly

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Basic_Concepts_of_Grid_Layout

https://stackoverflow.com/questions/49145552/how-is-grid-template-rows-auto-auto-1fr-auto-interpreted

https://university.webflow.com/lesson/css-grid-fractional-unit-fr-overview

https://www.digitalocean.com/community/tutorials/css-css-grid-layout-fr-unit

https://css-tricks.com/introduction-fr-css-unit/

https://css-tricks.com/snippets/css/complete-guide-grid/

https://www.smashingmagazine.com/understanding-css-grid-template-areas/

https://grid.layoutit.com/

https://github.com/Leniolabs/layoutit-grid



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


1 line of CSS Layouts的更多相关文章

  1. [CSS] Build Responsive CSS Layouts with Tachyons

    Building responsive css layouts is critical in any modern website. Tachyons makes this easy by desig ...

  2. Designing CSS Layouts With Flexbox Is As Easy As Pie

    This article is an updated excerpt of the chapter “Restyle, Recode, Reimagine With CSS3″ from our Sm ...

  3. 6 种CSS设置居中的方法

    原文 Demos of each of the methods below by clicking here. Horizontal centering with css is rather easy ...

  4. 只要一行代码,实现五种 CSS 经典布局

    常用的页面布局,其实就那么几个.下面我会介绍5个经典布局,只要掌握了它们,就能应对绝大多数常规页面. 这几个布局都是自适应的,自动适配桌面设备和移动设备.代码实现很简单,核心代码只有一行,有很大的学习 ...

  5. [css]实现垂直居中水平居中的几种方式

    转自博客 http://blog.csdn.net/freshlover/article/details/11579669 居中方式: 一.容器内(Within Container) 内容块的父容器设 ...

  6. 盘点8种CSS实现垂直居中水平居中的绝对定位居中技术

    Ⅰ.绝对定位居中(Absolute Centering)技术 我们经常用margin:0 auto来实现水平居中,而一直认为margin:auto不能实现垂直居中--实际上,实现垂直居中仅需要声明元素 ...

  7. CSS水平居中/垂直居中的N个方法

    我看最近微博流行CSS居中技术,老外码农争相写相关的文章,一篇赛一篇的长啊,我把几篇归纳总结了一下,算是笔记. 孔乙己曾说:"茴香豆的回字有四种写法",万一哪天有个面试官问你:&q ...

  8. CSS网页布局全精通

    在本文中将使用四种常见的做法,结合CSS于结构化标记语法制作两栏布局.很快地就会发现,不用嵌套表格,间隔用的GIF也能做出分栏版面布局. 相关文章:CSS网页布局开发小技巧24则 稍后在"技 ...

  9. 使用自己的CSS框架(转)

    [经典推介]CSS框架选择向导 不少CSS框架已经存在了一段时间,但大多数Web开发人员避免使用它们. 相反最有经验的开发者希望创建自己的CSS框架,提供个性化解决方案的优势,并减少对第三方的解决方案 ...

随机推荐

  1. jvm源码解析java对象头

    认真学习过java的同学应该都知道,java对象由三个部分组成:对象头,实例数据,对齐填充,这三大部分扛起了java的大旗对象,实例数据其实就是我们对象中的数据,对齐填充是由于为了规则分配内存空间,j ...

  2. Windows Server 2008 R2系统安装

    把系统U盘插到服务器上,然后启动服务器进入BIOS界面选择U盘启动. 根据服务器的不同,进入BIOS界面的按钮也不一样,主流的有F10.F11.F12.F2.ESC.delete等. 在从BIOS启动 ...

  3.  打开APP  04 | 网络通信:RPC框架在网络通信上更倾向于哪种网络IO模型? 2020-02-26 何小锋

     打开APP  04 | 网络通信:RPC框架在网络通信上更倾向于哪种网络IO模型? 2020-02-26 何小锋

  4. windows命令行关闭IE代理

    打开:reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnab ...

  5. Linux踩坑填坑记录

    Linux踩坑填坑记录 yum安装失败[Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.aliyun.com'" ...

  6. Java 复习整理day04

    在我们的日常生活中,方法可以理解为要做某件事情, 而采取的解决办法. 如:小明同学在路边准备坐车来学校学习.这就面临 着一件事情(坐车到学校这件事情)需要解决,解决办法 呢?可采用坐公交车或坐出租车的 ...

  7. 如何将下载到本地的JAR包手动添加到Maven仓库,妈妈再也不用担心我下载不下来依赖啦

    我们有时候使用maven下载jar包的时候,可能maven配置都正确,但是部分jar包就是不能下载下来,如果maven设置都不正确的,可以查看我的maven系列文章,这里仅针对maven配置正确,但是 ...

  8. 给jekyll博客添加搜索功能

    使用SWIFTYPE为jekyll博客添加搜索引擎 步骤 1.首先去swiftype注册一个账号 2.接着添加自己想要配置的网站地址并为新设定的引擎添加一个名字(非会员只能设置一个引擎). 3.收到验 ...

  9. Flink-v1.12官方网站翻译-P018-Event Time

    事件时间 在本节中,您将学习如何编写时间感知的Flink程序.请看一下及时流处理,了解及时流处理背后的概念. 关于如何在Flink程序中使用时间的信息请参考windowing和ProcessFunct ...

  10. Codeforces Round #658 (Div. 2) D. Unmerge(dp)

    题目链接:https://codeforces.com/contest/1382/problem/D 题意 给出一个大小为 $2n$ 的排列,判断能否找到两个长为 $n$ 的子序列,使得二者归并排序后 ...