CSS
Layout
Position
通过 position 可以对正常的文档流产生影响。
child 用 absolute,parent 就需要 relative
使用 absolute 时,是相对于父级元素而言;对于 fixed ,则相对于视窗(viewport)
position 属性不是默认继承的,color 是;inherit 可以让 position 变得可继承
static 是默认情况,left/right/top/bottom/z-index 不起作用
relative 可以使得 z-index 起作用,根据 left/right/top/bottom 属性,将元素从默认位置推走
absolute 从正常文档流移除,left/right/top/bottom/z-index 起作用
fixed 像 absolute 同样从文档流中移除,但滚动页面并不移动元素,仅相对于整个页面变化位置
sticky ,先是 relative,直到滚动到 viewport 的指定位置,此时的行为更像 fixed
Responsive Design
https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Responsive_Design HTML 包含基础的响应式部分。例如,一段文字一开始位于大窗口,后来宽度变小,文字会被浏览器重新布局以适应。这叫「流体」布局。但是,当文字呈现在更窄的屏幕上时,文字呈现变得不自然。如果设定固定大小的布局,在多个屏幕上呈现会有水平滚动条,和很多被浪费掉的闲置空间。
除去「流式」布局,还有「固定宽度」构建页面方法。
在响应式设计之前的灵活布局,有根据分辨率判断布局的,等等。
「响应式设计」,由 Ethan Marcotte 第一次于 2010 年提出。有三点:
- 流体网格
- 流式图像
- media query
以下是在创建响应式站点时,可能用到的技术。
一、Media Queries
Media query 根据断点(breakpoints)切换布局。一个简单相反:为窄屏设计单列内容,为宽屏设计多列内容。
二、Flexible grids
响应式站点除去「断点」,还依赖灵活的网格。使用 float 设置。
三、Modern layout tech
多列、Flexbox、Grid 布局是默认响应式的。
四、Responsive images
1img {
2 max-width: 100%;
3}
这种方法,可能导致图片过大超过视窗,进而浪费带宽。
五、Responsive typography 响应式排版
先对根元素 :root 也是 html,设置基本字体大小——单位可以是 px, %, em, rem。然后,对其他元素根据需要设计字体大小,单位是 rem,也就是相对于根元素。此外,还要通过 media query 调整不同屏幕大小上的同一元素大小。
另一种方法,是把字体单位改为 vw,1vw 等于当前视窗宽度的 1%。也就是,如果窗口宽度改变,对应的使用 vw 单位的元素也会发生改变。缺陷:无法放大/缩小。因此,不该单独使用 vw、vh。可这样用 calc(1.5rem + 3vw)
。最佳实践!
六、viewport meta tag
1<meta name="viewport" content="width=device-width,initial-scale=1" />
这段代码,告诉移动端浏览器,应该将视窗宽度设置为设备宽度,显示文档的对移动端优化后的大小。
这段代码是必备的,在想要展示在移动端的情况下。
https://alistapart.com/article/responsive-web-design/
Unlike the web, which often feels like aiming for next week, architecture is a discipline very much defined by its permanence.
2010 年之后,网页出现在更多不同屏幕的设备上。
一、Meet media query
1<link rel="stylesheet" type="text/css" href="*.css" media="screen" />
2<link rel="stylesheet" type="text/css" href="*.css" media="print" />
这是最初的最直接的方法,后来 CSS 标准发展出以下写法:
1<link rel="stylesheet" type="text/css" href="*.css" media="screen and (max-device-width: 480px)" />
而且,不仅在 <link>
标签中写,还可以在 CSS 样式文件中写。
1@media screen and (max-device-width: 480px) {
2 .column {
3 float: none;
4 }
5}
6/* OR */
7@import url("*.css") screen and (max-device-width: 480px);
Media queries
https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Media_queries
1@media media-type and (media-feature-rule) {
2 /* CSS rules go here */
3}
4
5/* media-type 是可以省略的,如果省略,该样式默认适用于 screen, print */
而 meida-feature-rule 最常用于创建响应式的是宽度,高度更少见。有 max-width, min-width, width, max-height, min-height, height。width、height 几乎不用。
还有 orientation(portrait or landscape 纵向/横向);hover
一、逻辑
and、or、not 逻辑: and
, ,
, not
二、选择断点
两种方式进行响应式设计:一种是从宽屏到窄屏,一种是从窄屏到宽屏。后者被称为 mobile first。
特定 CSS 能产生什么效果
居中对齐
https://css-tricks.com/centering-css-complete-guide/ https://www.w3schools.com/csS/css_align.asp
水平居中
1elem { 2 margin-left: auto; 3 margin-right: auto; 4 width: 50%; 5} 6 7elem { 8 margin: 0 auto; 9 width: 50%; 10} 11 12/* inline, inline-* 元素 */ 13.text { 14 text-align: center; 15} 16 17/* 块级元素 需要指定宽度,前2个也是块级元素居中样式 */ 18img { 19 display: block; 20 margin: 0 auto; 21} 22/* 多个块级元素排成一列横排 需要水平居中 23main > div 24.inline-block-center(main), .flex-center(main) 251. 设置好标准的 div 元素样式 26,*/ 27.inline-block-center { 28 text-align: center; 29 div { 30 display: inline-block; 31 text-align: left; 32 } 33} 34.flex-center { 35 display: flex; 36 justify-content: center; 37} 38/* 多个块级元素排成一列竖排 39main > div 40,*/ 41main div { 42 margin: 5px auto; 43} 44/* 之后一定要指定每个 div 元素的宽度 */
垂直居中
1/* inline, inline-*, 文本、链接等 */ 2 /* single line */ 3 .center-single-line-vertically { 4 padding: 50px 0; 5 } 6 .center-single-line-vertically { 7 height: 100px; 8 line-height: 100px; 9 white-space: nowrap; 10 } 11 /* multiple lines */ 12 .center-multiple-lines-vertically { 13 padding: 50px 0; 14 } 15 /* 如果对多行设置 padding 无效,使用以下办法 */ 16 .center-multiple-lines-vertically { 17 display: table; 18 height: 350px; /* 大于文字高度*/ 19 p { 20 display: table-cell; 21 margin: 0; 22 vertical-align: middle; 23 } 24 } 25 .center-multiple-lines-vertically { 26 display: flex; 27 justify-center: center; 28 flex-direction: column; 29 height: 300px; /* 大于文字高度 */ 30 } 31 .center-multiple-lines-vertically { 32 position: relative; 33 &::before { 34 content: " "; 35 display: inline-block; 36 height: 100%; 37 width: 1%; 38 vertical-align: middle; 39 } 40 p { 41 display: inline-block; 42 vertical-align: middle; 43 } 44 } 45/* 块级元素的垂直居中 */ 46 /* height 已知 */ 47 /* https://codepen.io/chriscoyier/pen/GRBVOj */ 48 .center-vertically { 49 position: relative; 50 .child { 51 position: absolute; 52 top: 50%; 53 height: 100px; 54 margin-top: -70px; /* box-sizing 非 border-box 的解决办法 */ 55 } 56 } 57 /* height 未知 */ 58 .center-vertically { 59 position: relative; 60 .child { 61 position: absolute; 62 top: 50%; 63 transform: translateY(-50%); 64 } 65 } 66 /* 元素高度拉伸到父元素 */ 67 .center-vertically { 68 display: table; 69 height: 300px; 70 div { 71 display: table-cell; 72 vertical-align: middle; 73 } 74 } 75 /* 与前文多行居中相同,使用 Flexbox */ 76 .center-vertically { 77 display: flex; 78 flex-direction: column; 79 justify-content: center; 80 } 81 /* 或者这样写 */ 82 .center-vertically { 83 display: flex; 84 .child { 85 margin: auto 0; 86 } 87 }
水平+垂直居中
1.center {
2 padding: 50px 0;
3 text-align: center;
4}
5/* 元素宽高固定 */
6.center {
7 position: relative;
8 .child {
9 width: 300px;
10 height: 100px;
11 padding: 20px;
12
13 position: absolute;
14 top: 50%;
15 left: 50%;
16
17 margin: -70px 0 0 -170px; /* 这个缩进值是如何计算的 */
18 }
19}
20/* 元素宽高未知 */
21.center {
22 height: 200px;
23 width: 300px; /* 宽度要有 */
24 position: relative;
25 p {
26 margin: 0;
27 position: absolute;
28 top: 50%;
29 left: 50%;
30 transform: translate(-50%, -50%);
31 }
32}
33/* Flexbox */
34.center {
35 display: flex;
36 justify-content: center;
37 align-items: center;
38}
39/* Gird */
40body, html {
41 height: 100%;
42 display: grid;
43 .child {
44 margin: auto;
45 }
46}
左右对齐
1.right {
2 position: absolute;
3 right: 0;
4}
5
6.right {
7 float: right;
8}
9
10.left {
11 position: absolute;
12 left: 0;
13}
14
15.left {
16 float: left;
17}
Clearing boxes wrapped around a float
https://www.w3schools.com/csS/css_align.asp https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Floats#clearing_boxes_wrapped_around_a_float 当使用 float 时,有时内部图片会溢出到父元素之外,这时就需要一些 hack