怎么用css写出三角形?

html-css013

怎么用css写出三角形?,第1张

我们的思路是使用border边框来实现三角形的样式,因为border的边框是由四个三角形组成的。

请点击输入图片描述

首先我们创建一个带边框的div:

具体代码实现如下:

width: 40px

height: 40px

border-width: 40px

border-style: solid

border-color: red green blue brown

请点击输入图片描述

然后我们将内部DIV的宽高设置为0:

width: 20px

height: 20px

border-width: 10px

border-style: solid

border-color: red green blue brown

请点击输入图片描述

将其他的三个边框给取消点:

width: 0

height: 0

border-width: 40px

border-style: solid

border-color: red transparent transparent transparent

请点击输入图片描述

利用更改border的边框,我们可以随意控制写出我们想要的三角形,通过控制边框的大小来实现三角形的大小,通过控制边框的位置来改变三角形的位置。

请点击输入图片描述

6

使用上面的方式实现三角形有一个问题就是,三角形的方位不太好控制,但是使用其他的方式依然会面临这样的问题。

请点击输入图片描述

1.可以用css3的border-radius属性来实现,支持ie9+

<div class="dm">

</div>

<div class="dm1">

</div>

<div class="dm2">

</div>

<div class="dm3">

</div>

<div class="dm4">

</div>

<div class="dm5">

</div>

<div class="dm6">

</div>

<div class="dm7">

</div>

<style>

.dm {

    width: 0

    height: 0

    border-left: 50px solid transparent

    border-right: 50px solid transparent

    border-bottom: 100px solid #00897B

}

.dm1{

  width: 0

    height: 0

    border-left: 50px solid transparent

    border-right: 50px solid transparent

    border-top: 100px solid #00897Bmargin-top: 20px

}

.dm2{

width: 0

    height: 0

    border-top: 50px solid transparent

    border-right: 100px solid #00897B

    border-bottom: 50px solid transparentmargin-top: 20px

}

.dm3{

  width: 0

    height: 0

    border-top: 50px solid transparent

    border-left: 100px solid #00897B

    border-bottom: 50px solid transparentmargin-top: 20px

}

.dm4{

   width: 0

    height: 0

    border-top: 100px solid #00897B

    border-right: 100px solid transparentmargin-top: 20px

}

.dm5{

 width: 0

    height: 0

    border-top: 100px solid #00897B

    border-left: 100px solid transparent margin-top: 20px

}

.dm6{

  width: 0

    height: 0

    border-bottom: 100px solid #00897B

    border-right: 100px solid transparent

}

.dm7{

  width: 0

    height: 0

    border-bottom: 100px solid #00897B

    border-left: 100px solid transparent

}

</style>