怎么利用CSS3绘制三角形

html-css014

怎么利用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

}

你试试用背景渐变和边框以及2d旋转来试试三角

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>带边线的三角</title>

<style>

*{padding:0margin: 0}

.box{

width: 400px

height: 400px

background: pink

margin: 200px

}

.arrow{

width: 30px

height: 30px

box-sizing: border-box

overflow: hidden

border-width: 1px 1px 0 0

border-color: #000

border-style: solid

background: linear-gradient(225deg,#f00 50%, transparent 50%)

transform: rotate(45deg)

}

</style>

</head>

<body>

<div class="box">

<div class="arrow"></div>

</div>

</body>

</html>