Evan's blog Evan's blog
首页
关于
  • 分类
  • 标签
  • 归档
  • H5&CSS3
  • JS
  • TS
  • Node
  • Webpack
  • Vue2
  • Vue3
  • 微信小程序
  • Andorid
  • Flutter
推荐
GitHub (opens new window)

conanan

真相只有一个
首页
关于
  • 分类
  • 标签
  • 归档
  • H5&CSS3
  • JS
  • TS
  • Node
  • Webpack
  • Vue2
  • Vue3
  • 微信小程序
  • Andorid
  • Flutter
推荐
GitHub (opens new window)
  • HTML
  • CSS基础

  • CSS布局

  • 高级技巧
    • CSS 三角
      • 使用1
      • 使用2
      • 京东三角案例1
      • 京东三角案例2
  • 代码规范
  • H5&CSS3
xugaoyi
2019-12-29
目录

高级技巧

# 高级技巧

# CSS 三角

# 使用1

看如下代码:

<!DOCTYPE html>
<html lang="en">
  <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>CSS三角</title>
    <style>
      .demo1 {
        width: 0;
        height: 0;
        border-top: 100px solid red;
        border-right: 100px solid green;
        border-bottom: 100px solid blue;
        border-left: 100px solid pink;
      }
    </style>
  </head>
  <body>
    <div class="demo1"></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

其结果如下图:

<html>
    <div class="demo1"></div>
</html>

<style>
    .demo1 {
        width: 0;
        height: 0;
        border-top: 100px solid red;
        border-right: 100px solid green;
        border-bottom: 100px solid blue;
        border-left: 100px solid pink;
    }
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

由此可画出三角形,如向右的三角,具体代码如下:

.demo1-2 {
  width: 0;
  height: 0;
  border: 100px solid transparent;/* transparent 的使用*/
  border-left: 100px solid red;
  /* 照顾兼容性问题 */
  line-height: 0;
  font-size: 0;
}
1
2
3
4
5
6
7
8
9
<html>
    <div class="demo1-2"></div>
</html>

<style>
    .demo1-2 {
        width: 0;
        height: 0;
        border: 100px solid transparent;/* transparent 的使用*/
        border-left: 100px solid red;
        /* 照顾兼容性问题 */
        line-height: 0;
        font-size: 0;
    }
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 使用2

<!DOCTYPE html>
<html lang="en">

  <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>CSS三角</title>
    <style>
      .demo2 {
        width: 0;
        height: 0;
        border-top: 100px solid red;
        border-left: 50px solid green;
        border-right: 50px solid blue;
      }
    </style>
  </head>

  <body>
    <div class="demo2"></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

效果如下:

<html>
    <div class="demo2"></div>
</html>

<style>
    .demo2 {
        width: 0;
        height: 0;
        border-top: 100px solid red;
        border-left: 50px solid green;
        border-right: 50px solid blue;
      }
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13

# 京东三角案例1

使用 position 和 CSS 三角完成,如下:

<!DOCTYPE html>
<html lang="en">

  <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>CSS三角</title>
    <style>
      .demo3 .jd {
        position: relative;
        width: 50px;
        height: 100px;
        background-color: pink;
        margin: 50px auto;
      }

      .demo3 .jd span {
        position: absolute;
        left: 35px;
        top: -10px;
        width: 0;
        height: 0;
        border: 5px solid transparent;
        border-bottom: 5px solid pink;
      }
    </style>
  </head>

  <body>
    <div class="demo3">
        <div class="jd">
            <span></span>
        </div>
    </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

具体效果如下:

<html>
    <div class="demo3">
        <div class="jd">
            <span></span>
        </div>
    </div>
</html>

<style>
    .demo3 .jd {
        position: relative;
        width: 50px;
        height: 100px;
        background-color: pink;
        margin: 50px auto;
      }

      .demo3 .jd span {
        position: absolute;
        left: 35px;
        top: -10px;
        width: 0;
        height: 0;
        border: 5px solid transparent;
        border-bottom: 5px solid pink;
      }
</style>
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

# 京东三角案例2

<!DOCTYPE html>
<html lang="en">

  <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>CSS三角强化的巧妙运用</title>
    <style>
      .price {
        width: 160px;
        height: 24px;
        line-height: 24px;
        border: 1px solid red;
        margin: 0 auto;
      }

      .miaosha {
        position: relative;
        float: left;
        width: 90px;
        height: 100%;
        background-color: red;
        text-align: center;
        color: #fff;
        font-weight: 700;
        margin-right: 8px;

      }

      .miaosha i {
        position: absolute;
        right: 0;
        top: 0;
        width: 0;
        height: 0;
        border-color: transparent #fff transparent transparent;
        border-style: solid;
        border-width: 24px 10px 0 0;
      }

      .origin {
        font-size: 12px;
        color: gray;
        text-decoration: line-through;
      }
    </style>
  </head>

  <body>
    <div class="price">
      <span class="miaosha">
        ¥1650
        <i></i>
      </span>
      <span class="origin">¥5650</span>
    </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

效果如下:

<html>
    <div class="price">
        <span class="miaosha">
            ¥1650
            <i></i>
        </span>
        <span class="origin">¥5650</span>
    </div>
</html>

<style>
    .price {
        width: 160px;
        height: 24px;
        line-height: 24px;
        border: 1px solid red;
        margin: 0 auto;
    }

    .miaosha {
        position: relative;
        float: left;
        width: 90px;
        height: 100%;
        background-color: red;
        text-align: center;
        color: #fff;
        font-weight: 700;
        margin-right: 8px;

    }

    .miaosha i {
        position: absolute;
        right: 0;
        top: 0;
        width: 0;
        height: 0;
        border-color: transparent #fff transparent transparent;
        border-style: solid;
        border-width: 24px 10px 0 0;
    }

    .origin {
        font-size: 12px;
        color: gray;
        text-decoration: line-through;
    }
</style>
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
编辑 (opens new window)
上次更新: 2022/03/23, 17:55:39
考拉
代码规范

← 考拉 代码规范→

最近更新
01
重点
04-12
02
搭建项目
04-04
03
TS补充
03-30
更多文章>
Theme by Vdoing | Copyright © 2019-2022 conanan | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式