简介
三角的做法有好几种:
这里主要介绍的纯代码写的。
优点
原理
原理就是使用css的盒模型中的border属性
使用border属性就可以实现一个兼容性很好的三角图形效果,其底层受到border-style的inseet/outset影响,边框3D效果在互联网早期还是很流行的,。
1. 先创建一个div
<div></div>
2. 然后给div设定边框。
div{ width:200px; height:100px; border:10px solid red; }
可以看到效果:
3. 给div的四个边框都设置不同的颜色
div{ width:200px; height:100px; border-left:10px solid red; border-top:10px solid green; border-right:10px solid blue; border-bottom:10px solid yellow; }
可以看到以下效果:
可以看到两个border交叉的地方,有斜边存在。
4. 把宽度和高度都变成0
div{ width:0px; height:0px; border-left:10px solid red; border-top:10px solid green; border-right:10px solid blue; border-bottom:10px solid yellow; } /*也可以这么写*/ div{ width:0px; height:0px; border:10px solid; border-color:red green blue yellow; }
可以看到以下效果:
这个时候就看得很明显了,出现了四个三角。那如果要出现一个,那么就将其他的三个弄成透明色就可以了。
这个就是三角形的由来。
5. 其余角为透明
这里的设置也遵循 上右下左 的顺序,把不需要的角弄成透明色。
div{ width:0px; height:0px; border-width:10px; border-color:#f00 transparent transparent transparent; border-style:solid; } /*也可以再进行合并*/ div{ width:0px; height:0px; border:10px solid; border-color:#f00 transparent transparent transparent; }
这样一个三角就完成了。 那么问题来了,那就是兼容问题,IE6的兼容问题,如果不要求兼容IE6可以忽略下一步。
6. 兼容IE6浏览器
同样的一个三角,在IE6的显示是什么呢?
造成这样的原因是:
在IE6下面,如果想把元素例如div设置成19像素以下的高度就设置不了了。这是因为IE6浏览器里面有个默认的高度,IE6下这个问题是因为默认的行高造成的。
最简单的解决办法:(后面添加)
div{ /*不支持transparent*/ border-style:solid dashed dashed dashed; /*高度最小不为0*/ overflow:hidden; }
div{ border:solid 1px transparent; _border-color:tomato; _filter:chroma(color=tomato); }
所以我觉得用在这里也可以, BUT没有亲测过,如果哪位小可爱测过可以请告知我^ ^。
div{ width:0px; height:0px; margin:100px auto; border-width:10px; border-style:solid; border-color:#f00 transparent transparent transparent; _border-color:#f00 tomato tomato tomato; _filter:chroma(color=tomato); }
div{ height:0; font-size:0; line-height:0; overflow:hidden; }
7. 解决内联元素的三角显示问题
为什么会有这个问题
因为我们刚才用 <div> 去制作三角,当然我们经常会使用 <em><i> 或者伪元素去做一些小图标。那么在显示上面,可能会有问题。 下面的代码:
<style> em{ width: 0; height: 0; border-width: 50px; border-color: transparent transparent transparent #f40; border-style: solid; } </style> <em></em>
可以看到页面是这个样子的:
为什么是这个样子的,那么我们再看的仔细一点。 它实际是这个样子的。
造成这样的原因
解决办法
这个有很多的办法:
1. 去掉固定的内容高度
使用font-size:0;可以去掉内容的固定高度。
em{ border-width: 50px; border-color: transparent transparent transparent #f40; border-style: solid; font-size: 0; }
2. 将内联元素转化为块级元素或者行内块元素
em{ border-width: 50px; border-color: transparent transparent transparent #f40; border-style: solid; display: block; /*也可以是inline-block*/ }
3. 将元素脱标(如果涉及特殊的布局可以直接使用)
/*脱标*/ em{ border-width: 50px; border-color: transparent transparent transparent #f40; border-style: solid; position: absolute; top:0; left:0; } /*or 浮动*/ em{ border-width: 50px; border-color: transparent transparent transparent #f40; border-style: solid; float:left; }
最终代码
下面就是兼容了IE6版本的三角代码。
div{ width:0px; /*设置宽高为0*/ height:0px;/*可不写*/ border-width:10px; /*数值控制三角的大小,垂直的位置*/ border-color:#f00 transparent transparent transparent;/*上右下左,transparent是透明的*/ border-style:solid dashed dashed dashed;/*设置边框样式,dashed是兼容IE6写的*/ overflow:hidden;/*兼容IE6最小高度不为0写的*/ }
改变border-width,三角变大,是不失真的。很清晰。
== 三角制作完成 。==
扩展
有角度的三角
上面制作的都是45度的三角,三角可以通过border的高度宽度确定角度。
比如这样一个三角,只需要确定上下的和左右的宽度不一样即可。
div{ width: 0px; height: 0px; margin: 100px auto; border-width:10px 30px; border-color:transparent transparent transparent red; border-style:solid; }
有一个角是直角的三角
观察可以看到,是上面和右面的三角同时设置成一个颜色。就会出现直角的三角。
div{ width: 0; border-width: 20px 10px; border-style: solid; border-color: red red transparent transparent; }
箭头
其实原理也简单,就是两个三角重叠在一起。上面的三角就是背景的颜色
<style type="text/css"> .san { border-width: 50px; border-color: transparent transparent transparent #f40; border-style: solid; position: relative; } .si { border-width: 30px; border-color: transparent transparent transparent #fff; border-style: solid; position: absolute; left: -50px; top: -30px; } </style> <!--html结构--> <div class="san"> <div class="si"></div> </div>
对话框
这个使用伪元素去做就很方便。
常大家对css制作圆形的方法比较熟悉:
<div id="circle"></div> //html代码
//css代码
#circle{
width:100px;
height:100px;
border-radius: 50px;
background:#000 ;
}
结果:
总结:在制作圆形之前,首先需要保证这个元素是正方形,即width===height; 其次border-radius=1/2width即可(border-radius的默认基准点是几何中心)
下面即将步入我们今天所说的正题:css制作三角形
首先css制作三角形我们需要了解border这个属性
<div id="triangle"></div>
#triangle{
width:50px;
height:50px;
border-width:50px;
border-style:solid;
border-color:#000 #ccc #933 #0C0;
}
这时我们发现四边都成了梯形,如果我们把这个div的height和width都设为0 ,此时四边都成了一个三角形
#triangle{
width:0px;
height:0px;
font-size:0px; //兼容性
line-height:0px;//兼容性
border-width:50px;
border-style:solid;
border-color:#000 #ccc #933 #0C0;
}
此时我们已经看到了有三角形出现,不过离我们所说的三角形还有一定差距,不过,我们可以发现只要将其它三个三角形设为透明不就到达我们的结果了么。沿着这个想法,我们将样式设为如下:
#triangle{
width:0px;
height:0px;
font-size:0px; //兼容性
line-height:0px;//兼容性
border-width:50px;
border-style:solid;
border-color:#000 transparent transparent;
}
这都是我们做出来的比较规则的三角形。那么不规则的我们该如何来做呢?例如下面这个图:
首先,我们需要做出一个规则三角形
#triangle{
width:0px;
height:0px;
font-size:0px; //兼容性
line-height:0px;//兼容性
border-width:50px;
border-style:solid;
border-color:#000 transparent transparent #000 ;
position:relative;
}
其次我们将另一个三角形叠在上面即可,
代码如下:
#triangle{
width:0px;
height:0px;
font-size:0px; //兼容性
line-height:0px;//兼容性
border-width:50px;
border-style:solid;
border-color:#000 transparent transparent #000 ;
position:relative;
}
#triangle1{
width:0px;
height:0px;
font-size:0px; //兼容性
line-height:0px;//兼容性
border-width:30px 50px;
border-style:solid;
border-color:red transparent transparent red;
position: absolute;
left:-50px;
top:-50px;
}
<div id="triangle">
<div id="triangle1"></div>
</div>
最终我们只需将我们案例中第二个三角形的红色换成第一个三角形的背景色即可 (示例中为白色 即#fff即可)。
常见的聊天对话框,如微信,都是由一个长方形加一个三角形组合而成,重点就在于三角形的制作
三角形的实现原理是元素宽高设置为0,再设置边框宽度、样式和颜色。
例如:
只设置一条边的颜色,其他三边颜色设置为透明
例如:
只能看见边线,内部透明的三角形该如何用css实现呢
实现思路:使用css伪元素定位,用两个不同颜色、不同大小的实心三角形叠加,以达到“空心”的效果
*请认真填写需求信息,我们会在24小时内与您取得联系。