Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How does CSS draw a five-pointed star?


May 29, 2021 Article blog



A few days ago, the w3cschool compilation introduced several common graphic painting methods. So today we're going to show you how CSS draws five-pointed stars?

The application of five-pointed stars in web pages is still very common, such as our common favorite function, by drawing a pentix star, when the user taps the five-pointed star can light the five-pointed star, to achieve the collection effect.

Let's start with the implementation:

 How does CSS draw a five-pointed star?1

Idea: In fact, the idea of realizing a pentrophogram is to draw three triangles first, and then combine the three triangles, you can get a five-pointed star.

Specific code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>CSS绘制五角星 - 编程狮(w3cschool.cn)</title>

<style type="text/css">

#star-five {

   margin100px 0;

   position: relative;

   display: block;

   color: red;

   width0px; /*设置元素宽高为0*/

   height0px;

   border-right:  100px solid transparent; /*设置右部分为透明*/

   border-bottom70px  solid #ff0000; /*设置底部为红色*/

   border-left:   100px solid transparent; /*设置左部分为透明*/

 /*旋转角度为顺时针35度*/

   -moz-transform:    rotate(35deg); /*Firefox*/

   -webkit-transformrotate(35deg); /*Safari 和 Chrome*/

   -ms-transform:     rotate(35deg); /*IE 9*/

   -o-transform:      rotate(35deg); /*Opera*/

}

#star-five:before {

   border-bottom80px solid #ff0000;

   border-left30px solid transparent;

   border-right30px solid transparent;

   position: absolute;

   height0;

   width0;

   top: -45px;

   left: -65px;

   display: block;

   content'';

   -webkit-transformrotate(-35deg);

   -moz-transform:    rotate(-35deg);

   -ms-transform:     rotate(-35deg);

   -o-transform:      rotate(-35deg);

}

#star-five:after {

   position: absolute;

   display: block;

   color: red;

   top3px;

   left: -105px;

   width0px;

   height0px;

   border-right100px solid transparent;

   border-bottom70px solid #ff0000;

   border-left100px solid transparent;

   -webkit-transformrotate(-70deg);

   -moz-transform:    rotate(-70deg);

   -ms-transform:     rotate(-70deg);

   -o-transform:      rotate(-70deg);

   content'';

}

</style>

</head>

<body>

<div id="star-five"></div>

</body>

</html>

These are the full answers to how CSS draws five-pointed stars. For more CSS graphics, follow w3cschool.com.

Read more: How does CSS achieve a mouse-slip text effect? /b10> How does CSS achieve photo blur?