如何使用CSS画一个小三角图标

html-css04

如何使用CSS画一个小三角图标,第1张

其实很简单

HTML代码:

[html] view plain copy

<div class="sanjiao"></div>

CSS代码:

[css] view plain copy

.sanjiao {

width: 0

height: 0

overflow: hidden

border-width: 10px

border-color: transparent transparent #000 transparent

border-style: dotted dotted solid dotted

}

其原理就是给元素加一个比较大的边框,箭头的方向就是border-color四个参数的方向(上、右、下、左),箭头指向那一边就给哪一边设置颜色,其他边透明。

这样就很方便的做了一个小的三角形图标。

其中需要注意的地方是:由于IE6不支持overflow属性,会将其他边框也显示出来,所以将不需要显示的边框的border-style属性设置为dotted就可以完美兼容IE6啦!

你好,我这里用CSS实现了三种带边框三角,效果分别如图:

实例代码如下,根据你个人的情况调整代码吧:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Arrow</title>

</head>

<body>

<style>

/* scale */

.arrow,

.arrow:after{

    position: relative

    display: inline-block

    width: 0

    height: 0

    border-top: 0

    border-left: 30px dotted transparent

    border-right: 30px dotted transparent

    border-bottom: 30px dashed #000

}

.arrow:after {

    position: absolute

    top: 0

    content: ''

    transform: translateX(-50%) scale(.8)

    border-bottom: 30px dashed #fff

}

/* width & height */

.arrow1,

.arrow1:after {

    position: relative

    display: inline-block

    width: 0

    height: 0

    border-top: 0

    border-left: 30px dotted transparent

    border-right: 30px dotted transparent

    border-bottom: 30px dashed #000

}

.arrow1:after {

    position: absolute

    left: -26px 

    top: 2px

    content: ''

    width: 0 

    height: 0 

    border-top: 0 

    border-left: 26px dotted transparent 

    border-right: 26px dotted transparent 

    border-bottom: 26px dashed #fff

}

/* border & after */

.arrow2 {

    position: relative

    display: inline-block

    width: 28px

    height: 28px

    border: 0

    border-top: 2px solid #000

    border-right: 2px solid #000

    -webkit-transform: translate(7px, 14px) rotate(-45deg)

    -ms-transform: translate(7px, 14px) rotate(-45deg)

    -o-transform: translate(7px, 14px) rotate(-45deg)

    transform: translate(7px, 14px) rotate(-45deg)

}

.arrow2:after {

    position: absolute

    left: 0

    top: -2px

    width: 42px

    height: 2px

    content: ''

    border-radius: 2px

    background-color: #000

    -webkit-transform-origin: left top

    -moz-transform-origin: left top

    -ms-transform-origin: left top

    -o-transform-origin: left top

    transform-origin: left top

    -webkit-transform: rotate(45deg)

    -ms-transform: rotate(45deg)

    -o-transform: rotate(45deg)

    transform: rotate(45deg)

}

</style>

<span class="arrow"></span>

<span class="arrow1"></span>

<span class="arrow2"></span>

</body>

</html>

希望能帮到你,如有疑问请追问,望采纳~

使用CSS制作小三角形实际就是通过控制块元素的边框来实现的。

例如:

<style>

.triangle-up {

    width: 0

    height: 0

    border-left: 50px solid transparent

    border-right: 50px solid transparent

    border-bottom: 100px solid red

}

.triangle-down {

    width: 0

    height: 0

    border-left: 50px solid transparent

    border-right: 50px solid transparent

    border-top: 100px solid red

}

.triangle-left {

    width: 0

    height: 0

    border-top: 50px solid transparent

    border-right: 100px solid red

    border-bottom: 50px solid transparent

}

.triangle-right {

    width: 0

    height: 0

    border-top: 50px solid transparent

    border-left: 100px solid red

    border-bottom: 50px solid transparent

}

</style>

<div class='triangle-down'></div> <!--向下三角形-->

<div class='triangle-up'></div> <!--向上三角形-->

<div class='triangle-left'></div> <!--向左三角形-->

<div class='triangle-right'></div> <!--向右三角形-->

原理就是设置块元素的三条边透明掉(tranparent)