如何利用css画出一个三角形


本文摘自PHP中文网,作者V,侵删。

首先我们来看一下效果图:

(视频教程推荐:css视频教程)

e7a50c59168c5aa9767846b933a0e57.png

实现代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <style type="text/css">

        /* css3绘制三角形 */

        .triangle{

            width: 0px;                           /*设置宽高为0,所以div的内容为空,从才能形成三角形尖角*/

            height: 0px;

            border-bottom: 200px solid #00a3af;

            border-left: 200px solid transparent;    /*transparent 表示透明*/

            border-right: 200px solid transparent;

        }

    </style>

</head>

<body>

    <div class="triangle"></div>

</body>

</html>

还是不理解的小伙伴可以看下面

1、设置div有一定宽高,四边设置边框

1

2

3

4

5

6

7

8

.triangle{

    width: 50px;

    height: 50px;

    border-top: 200px solid #00a497;

    border-bottom: 200px solid #cc7eb1;

    border-left: 200px solid #165e83;

    border-right: 200px solid #c85179;

}

上面代码设置div有一定宽高,四边设置边框时,效果如下:

6de8fb982be26d21623931800653d1b.png

2、设置div宽高为0,四边设置边框宽度为200px

1

2

3

4

5

6

7

8

.triangle{

   width: 0px;

   height: 0px;

   border-top: 200px solid #00a497;

   border-bottom: 200px solid #cc7eb1;

   border-left: 200px solid #165e83;

   border-right: 200px solid #c85179;

}

上面代码设置div宽高为0,四边边框设置不同颜色时,效果如下:

2f1102942d61ff52e181d3a6d851af4.png

(相关教程推荐:CSS教程)

3、接下来div宽高仍为0,去掉border-top

1

2

3

4

5

6

7

.triangle{

   width: 0px;

   height: 0px;

   border-bottom: 200px solid #cc7eb1;

   border-left: 200px solid #165e83;

   border-right: 200px solid #c85179;

}

上面代码设置div宽高为0,只设置下边框和左右边框时,效果如下:

f6f662a6cfca8e3fa16b8c910540ec1.png

4、最后发现,只将border-bottom设置颜色,左右边框透明,既可得到三角形

1

2

3

4

5

6

7

.triangle{

    width: 0px;

    height: 0px;

    border-bottom: 200px solid #cc7eb1;

    border-left: 200px solid transparent;

    border-right: 200px solid transparent;

}

最终效果:

d0a734948ed82379a2db29f35d83fca.png

以上就是如何利用css画出一个三角形的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

原生js如何修改css

css比html更复杂为什么还要用?

html5 css 需不需要js

自学 html5 要多久

css如何设置背景图片灰度

css如何让文字居于div的底部

css如何去掉图片边框

css怎么取消颜色

css不让字体倾斜怎么实现

css实现分页条

更多相关阅读请进入《css》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...