HTML和CSS基础

一、HTML

1. 如何理解HTML语义化

  • 让人更容易读懂(增加代码可读性)
  • 让搜索引擎更容易读懂(SEO) cf7da04bb7e6f58c5e89881d023c7a05.png

2. 默认情况下的块级元素和行内元素

  • 块级元素:display: block/table,有div h1 h2 table ul ol p
  • 行内元素:display: inline/inline-block,有span img input button

二、CSS

1. 布局

(1)盒模型

/** offsetWidth = 122px **/
div {
   width: 100px;
   padding: 10px;
   border: 1px solid #fff;
}

 /** offsetWidth = 100px **/
div {
   width: 100px;
   padding: 10px;
   border: 1px solid #fff;
   box-sizing: border-box;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  • 默认情况下,offsetWidth = (内容宽度 + 内边距 + 边框),无外边距
  • box-sizing: border-box情况下,offsetWidth = width

(2)margin纵向重叠问题

相邻元素的margin-topmargin-bottom会发生重叠 空白内容的<p></p>也会重叠 如下代码,AAA和BBB之间的距离是多少?

<style>
p {
    font-size: 16px;
    line-height: 1;
    margin-top: 10px;
    margin-bottom: 15px;
}
</style>

<p>AAA</p>
<p></p>
<p></p>
<p></p>
<p>BBB</p>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

**答案:**15px

(3)margin负值问题

  • margin-topmargin-left负值,元素向上、向左移动
  • margin-right负值,右侧元素左移,自身不受影响
  • margin-bottom负值,下方元素上移,自身不受影响

(4)BFC理解与应用

BFC: Block Format Context,块级格式化上下文 一块独立渲染区域,内部元素的渲染不会影响边界以外的元素

形成BFC的常见条件:

  • float不是none
  • positionabsolutefixed
  • overflow不是visible
  • displayflex inline-block

BFC的常见应用:

  • 清除浮动

(5)float布局

1. 圣杯布局和双飞翼布局的目的
  • 三栏布局,中间一栏最先加载和渲染(内容最重要)
  • 两侧内容固定,中间内容随着宽度自适应
  • 一般用于PC网页
2. 两者的技术总结
  • 使用 float 布局
  • 两侧使用 margin 负值,以便和中间内容横线重叠
  • 防止中间内容被两侧覆盖,圣杯布局使用padding,双飞翼布局使用 margin
3. 圣杯布局的实现
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>圣杯布局</title>
    <style type="text/css">
        body {
            min-width: 550px;
        }
        #header {
            text-align: center;
            background-color: #f1f1f1;
        }

        #container {
            padding-left: 200px;
            padding-right: 150px;
        }
        #container .column {
            float: left;
        }

        #center {
            background-color: #ccc;
            width: 100%;
        }
        #left {
            position: relative;
            background-color: yellow;
            width: 200px;
            margin-left: -100%;
            right: 200px;
        }
        #right {
            background-color: red;
            width: 150px;
            margin-right: -150px;
        }

        #footer {
            text-align: center;
            background-color: #f1f1f1;
        }

        /* 手写 clearfix */
        .clearfix:after {
            content: '';
            display: table;
            clear: both;
        }
    </style>
</head>
<body>
    <div id="header">this is header</div>
    <div id="container" class="clearfix">
        <div id="center" class="column">this is center</div>
        <div id="left" class="column">this is left</div>
        <div id="right" class="column">this is right</div>
    </div>
    <div id="footer">this is footer</div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
4. 双飞翼布局
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>双飞翼布局</title>
    <style type="text/css">
        body {
            min-width: 550px;
        }
        .col {
            float: left;
        }

        #main {
            width: 100%;
            height: 200px;
            background-color: #ccc;
        }
        #main-wrap {
            margin: 0 190px 0 190px;
        }

        #left {
            width: 190px;
            height: 200px;
            background-color: #0000FF;
            margin-left: -100%;
        }
        #right {
            width: 190px;
            height: 200px;
            background-color: #FF0000;
            margin-left: -190px;
        }
    </style>
</head>
<body>
    <div id="main" class="col">
        <div id="main-wrap">
            this is main
        </div>
    </div>
    <div id="left" class="col">
        this is left
    </div>
    <div id="right" class="col">
        this is right
    </div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
5. 手写clearfix
<style type="text/css">

.clearfix:after {
    content: '';
    display: table;
    clear: both;
}

.clearfix {
    *zoom: 1; /* 兼容 IE 低版本 */
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12

(6)flex布局

2. 定位

(1)absolute和relative定位

  • relative 依据自身定位
  • absolute 依据最近一层的定位元素定位

定位元素:

  • absolute relative fixed
  • body

(2)居中对齐的方式

水平居中:

  1. inline 元素:text-align: center
  2. block 元素:margin: auto
  3. absolute 元素:left: 50% + margin-left负值(需要子元素宽度)

垂直居中:

  1. inline 元素:line-height的值等于height值
  2. absolute 元素:top: 50% + margin-top负值(需要子元素高度)
  3. absolute 元素:transform: translate(-50%, -50%)(需要子元素尺寸)
  4. absolute 元素:top,left,bottom,right = 0 + margin: auto

3. 图文样式

line-height继承

  • 父元素line-height为具体的单位数值,如30px,则子元素line-height继承该值
  • 父元素line-height为比例数值,如2或1.5,则继承该比例,子元素 line-height = 父元素 line-height * 子元素 font-size
  • 父元素line-height为百分比,如200%,则子元素 line-height = 父元素 line-height * 父元素 font-size

4. 响应式

(1)media-query和rem

  • media-query,根据不同的屏幕宽度设置根元素 font-size
  • rem,基于根元素的相对单位
  1. px,绝对长度单位,最常用
  2. em,相对长度单位,相对于父元素,不常用
  3. rem,相对长度单位,相对于根元素,常用于响应式布局

定义:

html {
    {/* 定义 1 rem = 100 px */}
    font-size: 100px
}

div1 {
    {/* 10 px */}
    width: 0.1rem;
}

div2 {
    {/* 20 px */}
    width: 0.2rem;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

media-query:

<style type="text/css">
@media only screen and (max-width: 374px) {
    /* ipone5 或者更小的尺寸,以 ipone5 的宽度(320px)比例设置 font-size */
    html {
        font-size: 86px;
    }
}

@media only screen and (min-width: 375px) and (max-width: 413px) {
    /* ipone6/7/8 和 ipone x */
    html {
        font-size: 100px;
    }
}

@media only screen and (min-width: 414px) {
    /* ipone6p 或更大尺寸 */
    html {
        font-size: 110px;
    }
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

(2)vw/vh

1. rem的弊端

屏幕尺寸多样性,适配各种尺寸程阶梯性适配

2. 网页视口尺寸
  • window.screen.height 屏幕高度
  • window.innerHeight 网页视口高度
  • document.body.clientHeight body高度
3. vm/vh
  • vh:网页视口高度的 1/100
  • vw:网页视口宽度的 1/100
  • vmax:取两者最大值
  • vmin:取两者最小值

5. CSS3