怎么利用CSS3绘制三角形

html-css010

怎么利用CSS3绘制三角形,第1张

1、新建一个html5网页,名称为index.html,在<body>代码中写上四个div,分别是向上、向下、向左,向右四个三角形,代码如下:

<div class="triangle-up"><!--向上的三角--></div>

<div class="triangle-down"> <!--向下的三角--></div>

<div class="triangle-left"> <!--向左的三角--></div>

<div class="triangle-right"> <!--向右的三角--></div>

2、然后新建一个css文件style.css,并在index.html中引入,引入代码:<link rel="stylesheet" type="text/css" href="style.css">

3、先做向上的三角形,这里有两种写法,大家可以参考下。在css文件中输入以下代码:

第一种: .triangle-up {

width:0

height:0

border-left:30px solid transparent

border-right:30px solid transparent

border-bottom:30px solid #fff

}

第二种:.triangle-up {

width:0

height:0

border:30px solid transparent

border-bottom-color:#fff

}

4、接下来写向下的三角形,继续在css文件中输入以下代码:

.triangle-down {

width:0

height:0

border-left:20px solid transparent

border-right:20px solid transparent

border-top:20px solid #0066cc

}

5、然后是向左的三角形,代码为:

.triangle-left {

width:0

height:0

border-top:30px solid transparent

border-bottom:30px solid transparent

border-right:30px solid yellow

}

6、最后是向右的三角形,代码为:

.triangle-right {

width:0

height:0

border-top:50px solid transparent

border-bottom: 50px solid transparent

border-left: 50px solid green

}

你好,我这里用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>

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