怎么在CSS中实现三角形和箭头
**例1、**一般设置高度、宽度及边框后,盒子呈现如下图:
.triangle { width: 25px; height: 25px; overflow: hidden; font-size: 0; line-height: 0; border-width: 50px; border-style: solid; border-color: rgb(235, 54, 241) rgb(86, 245, 139) rgb(76, 0, 255) rgb(46, 205, 245); }
注:设置overflow、font-size、line-height,是因为在IE6下会具有默认的字体大小和行高, 导致盒子呈现被撑开的长矩形。
例2、将例1中的宽度和高度设置为0后,盒子呈现如下图:
.triangle { width: 0; height: 0; overflow: hidden; font-size: 0; line-height: 0; border-width: 50px; border-style: solid; border-color: rgb(235, 54, 241) rgb(86, 245, 139) rgb(76, 0, 255) rgb(46, 205, 245); }
此时,可以看到盒子是由四个三角形组成的。如果只保留一种颜色, 将其他3种颜色设置为透明或与背景同色, 就能实现三角形。根据选择留下不同位置的边,可以呈现出不同朝向的三角形。
例3、只保留底边
.triangle { width: 0; height: 0; overflow: hidden; font-size: 0; line-height: 0; border-width: 50px; border-style: solid; border-color: transparent transparent rgb(76, 0, 255) transparent; }
例4:在例3中的宽度和高度保留,可以得到梯形
width: 0; height: 0;
例5、实现箭头
箭头其实是通过2个三角形错位叠加来实现的。
用错开1px的白色三角形覆盖蓝色三角形,形成箭头。
下面的样式实现了一个向上箭头:
. arrow { position: absolute; } . arrow:before,. arrow:after{ position: absolute; content: ''; border-top: 10px transparent solid; border-left: 10px transparent solid; border-right: 10px transparent solid; border-bottom: 10px #fff solid; } . arrow:before{ border-bottom: 10px #0099CC solid; } . arrow:after{ top: 1px; /*覆盖并错开1px*/ border-bottom: 10px #fff solid; }
css的基本语法是什么
css的基本语法是:1、css规则由选择器和一条或多条声明两个部分构成;2、选择器通常是需要改变样式的HTML元素;3、每条声明由一个属性和一个值组成;4、属性和属性值被冒号分隔开。
以上就是怎么在CSS中实现三角形和箭头,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
网站标题:怎么在CSS中实现三角形和箭头
文章转载:http://cqcxhl.cn/article/pdpjco.html