CSS实现三角形图标原理解析

CSS实现三角形图标原理解析:

border边框语法:

border 四条边框设置
border-left 设置左边框,一般单独设置左边框样式使用
border-right 设置右边框,一般单独设置右边框样式使用
border-top 设置上边框,一般单独设置上边框样式使用
border-bottom 设置下边框,一般单独设置下边框样式使用,有时可将下边框样式作为CSS下划线效果应用
基本用法:


// 为引用 class="border-four" 的盒子对象设置1px像素红色实线边框
.border-four { border: 1px solid #ff0000; };

// 同理,单独给左边框设置1px像素红色实线边框
.border-left { border-left: 1px solid #ff0000; };

// 同理,单独给右边框设置1px像素红色实线边框
.border-right { border-right: 1px solid #ff0000; };

// 同理,单独给上边框设置1px像素红色实线边框
.border-top { border-top: 1px solid #ff0000; };

// 同理,单独给底边框设置1px像素红色实线边框
.border-bottom { border-bottom: 1px solid #ff0000; };


推荐常用边框:

// 实线边框:solid
.solid { border: 1px solid #ff0000; }

// 虚线边框:dashed
.dashed { border: 1px dashed #ff0000; }

进入正题:

正常块元素设置四条不同颜色边框效果:为了效果明显,所有边框宽度均为50px;

上图 html 和 css 代码如下:

<div class="test-border">我是居中内容</div>

.test-border {
border-top: 50px solid #ff0000;
border-bottom: 50px solid #00a000;
border-left: 50px solid #ff7f50;
border-right: 50px solid #436eee;
text-align: center;
}

请各位记住四条边边框位置范围,这很重要。当去掉内容,效果显示会是什么样的?

再附上 html 代码,css代码同上没有任何变化

<div class="test-border"></div>
效果如下:

 

因为此处div是块级元素,所以 会撑满一行。接下来我们将宽和高设置为0,看看效果又是什么样?附上 html 和css 代码以及效果图:

<div class="test-border"></div>

.test-border {
width: 0;
height: 0;
border-top: 50px solid #ff0000;
border-bottom: 50px solid #00a000;
border-left: 50px solid #ff7f50;
border-right: 50px solid #436eee;
text-align: center;
}

 

看到上面效果图,距离我们实现三角形四个方向带箭头已经不远了,

如果我们将上图的下边框颜色设置为透明,即 border-right: 50px solid transparent;

效果如下:

 

如果我们我们去掉 border-bottom ,效果相信大家都能猜到,CSS 和 如下图所示:


.test-border {
width: 0;
height: 0;
border-top: 50px solid #ff0000;
border-bottom: 50px solid #00a000;
border-left: 50px solid #ff7f50;
}

备注:由于此时宽和高都设置为0,所以text-align: center 是多余的,去掉。

 

没错,就是这样的,和上面的透明的效果对比一下。

是不是离我们的三角箭头又更近一步了?此时有的人或许会想,我把上边框和下边框一起去掉不就是向右的三角箭头了?错,当我们去掉上边框和下边框,此时盒子模型的高度是为0,无法将左边框撑高,我们将开不到任何东西,所以,不能将上边框和下边框宽度设置为0或者去掉。

我们去掉下边框试一试。CSS 和 效果如下所示:

.test-border {
width: 0;
height: 0;
border-top: 50px solid #ff0000;
border-left: 50px solid #ff7f50;
}

 

没错,效果就是这样,变成正方形了,现在的效果是不是我们的三角箭头原来越远了?那我们继续没有去掉border-bottom上来展开思考,

怎么样才能达到我们想要的效果呢?既不能去掉上边框也不能去掉下边框,我们可不可以隐藏上边框和下边框呢?大家都知道css颜色有一个属性值 transparent

如果我们将上边框和下边框的颜色设置成透明会怎么样?自己在慢慢想象一下,效果如下:


.test-border {
width: 0;
height: 0;
border-top: 50px solid transparent;
border-bottom: 50px solid transparent;
border-left: 50px solid #ff7f50;
}

 

接下来给出其余三个三角箭头 CSS 代码和效果图。

箭头向左:


.test-border {
width: 0;
height: 0;
border-top: 50px solid transparent;
border-bottom: 50px solid transparent;
border-right: 50px solid #436eee;
}

 

箭头向下:


.test-border {
width: 0;
height: 0;
border-top: 50px solid #ff0000;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
}

 

箭头向上:


.test-border {
width: 0;
height: 0;
border-bottom: 50px solid #00a000;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
}

 

原文地址:http://www.cnblogs.com/jack-chan/p/5024129.html

posted on 2015-12-08 17:26  落叶归尘  阅读(1301)  评论(0编辑  收藏  举报

导航