css如何做三角形


本文摘自PHP中文网,作者青灯夜游,侵删。

css做三角形的方法:首先创建一个div元素,设置div的width和height为0,只用边框宽来填充,边框样式设置为实线“solid”;然后顶部边框设置颜色,剩下的三个边框的颜色设置为透明“transparent”值即可。

本教程操作环境:windows7系统、CSS3&&HTML5版、Dell G3电脑。

使用CSS画三角形

第一步

首先,先来一个div,然后给这个div加一层border,并且给上下左右border分别加上不同颜色,以便观察,代码和效果如下:

1

2

3

4

5

6

7

8

9

10

.trangle{

  width: 100px;

  height: 100px;

  border: 100px solid #000;

  border-top-color: red;

  border-bottom-color: yellow;

  border-left-color: blue;

  border-right-color: green;

}

<div class="trangle"></div>

第二步

接着,将这个div的width变为0,我们再来看看效果。可以看到,由于div的宽度变成了0,左右两边的border“吸”在了一起,同时上下的border变成了三角形,好像快要完成了,别急,再看看第三步。

1

2

3

4

5

6

7

8

9

10

.trangle{

  width: 0px;

  height: 100px;

  border: 100px solid #000;

  border-top-color: red;

  border-bottom-color: yellow;

  border-left-color: blue;

  border-right-color: green;

}

<div class="trangle"></div>

第三步

然后,再将这个div的height变为0,效果如下。我们可以看到,由于div的高度也变成了0,上下两个border也“吸”在了一起,同时上下的border也变成了三角形,到现在为止,四个三角形已经出来了

1

2

3

4

5

6

7

8

9

10

.trangle{

  width: 0px;

  height: 0px;

  border: 100px solid #000;

  border-top-color: red;

  border-bottom-color: yellow;

  border-left-color: blue;

  border-right-color: green;

}

<div class="trangle"></div>

第四步

最后,就看你想要哪个角啦,想要哪个角就把其余三个border设为透明即可。例如,我想要最上面的三角形,那就把下、左、右设为透明,代码和效果如下:

1

2

3

4

5

6

7

8

9

10

.trangle{

  width: 0px;

  height: 0px;

  border: 100px solid #000;

  border-top-color: red;

  border-bottom-color: transparent;

  border-left-color: transparent;

  border-right-color: transparent;

}

<div class="trangle"></div>

简化代码

其实,我们不需要把四个border都设置一遍,只需设置你想要画的三角形所涉及到的三条边的border即可。以上步的画最上面的三角形为例,只需设置上、左、右三条边即可,并且要上三角形,就把左右border设为透明,代码和效果如下:

1

2

3

4

5

6

7

8

.trangle{

  width: 0px;

  height: 0px;

  border-top: 100px solid red;

  border-left: 100px solid transparent;

  border-right: 100px solid transparent;

}

<div class="trangle"></div>

(学习视频分享:css视频教程)

CSS 实现绘制各种三角形(各种角度)

Triangle Up


1

2

3

4

5

6

7

#triangle-up {

    width: 0;

    height: 0;

    border-left: 50px solid transparent;

    border-right: 50px solid transparent;

    border-bottom: 100px solid red;

}

Triangle Down

1

2

3

4

5

6

7

#triangle-down {

    width: 0;

    height: 0;

    border-left: 50px solid transparent;

    border-right: 50px solid transparent;

    border-top: 100px solid red;

}

Triangle Left

1

2

3

4

5

6

7

#triangle-left {

    width: 0;

    height: 0;

    border-top: 50px solid transparent;

    border-right: 100px solid red;

    border-bottom: 50px solid transparent;

}

Triangle Right

1

2

3

4

5

6

7

#triangle-right {

    width: 0;

    height: 0;

    border-top: 50px solid transparent;

    border-left: 100px solid red;

    border-bottom: 50px solid transparent;

}

Triangle Top Left

1

2

3

4

5

6

#triangle-topleft {

    width: 0;

    height: 0;

    border-top: 100px solid red;

    border-right: 100px solid transparent;

}

Triangle Top Right

1

2

3

4

5

6

7

#triangle-topright {

    width: 0;

    height: 0;

    border-top: 100px solid red;

    border-left: 100px solid transparent;

  

}

Triangle Bottom Left

1

2

3

4

5

6

#triangle-bottomleft {

    width: 0;

    height: 0;

    border-bottom: 100px solid red;

    border-right: 100px solid transparent;

}

Triangle Bottom Right

1

2

3

4

5

6

#triangle-bottomright {

    width: 0;

    height: 0;

    border-bottom: 100px solid red;

    border-left: 100px solid transparent;

}

更多编程相关知识,请访问:编程视频!!

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

相关阅读 >>

css样式怎么插入背景图片

css如何设置br高度

css中文字的颜色怎么设置

css modules是啥子东西?一起来了解一下!

css如何实现模糊背景效果

css最大的优点是什么

css命名规范是什么

css中如何给整个页面添加背景颜色

css如何不显示元素

css有哪些版本

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




打赏

取消

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

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

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

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

评论

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