Table of Contents
Ixagonal triangle
Square
Regular pentagon
Regular hexagon
Regular heptagon
Regular octagon
Home Web Front-end CSS Tutorial How to perform regular polygon transformation of a single div with CSS

How to perform regular polygon transformation of a single div with CSS

Apr 29, 2019 am 10:13 AM
css css3 html

This article purely uses CSS to transform a "single" div from a regular triangle to a regular octagon (a single div can only be a regular octagon at most), and finally uses the effect of animation to transform it into a regular polygon. Animation, and because regular polygons require a lot of trigonometric function calculations, for the sake of convenience, the sides of regular polygons are set to 100px.

Ixagonal triangle

The equilateral triangle does not need to use pseudo elements. It only needs to set the border width of p itself to generate it. First, let’s take a look at the side length and center line of the equilateral triangle. , if the side length is 100px, the center line is rounded to 87px (100 x sin (60) = 87).

How to perform regular polygon transformation of a single div with CSS

So we need to set the length and width of p to 0, then set the width of the bottom border to 87px, and the left and right borders Set the width to 50px (and the color to transparent) to make a beautiful triangle.

1

2

3

4

5

width:0;

height:0;

border-width:0 50px 87px ;

border-style:solid;

border-color:transparent transparent #095;

Copy after login

How to perform regular polygon transformation of a single div with CSS

Square

The square should be the simplest, just set the length and width to the same value. But there are actually two other methods. The first one is to set the length and width to 0, and set the top, bottom, left and right borders to 50px. The second one is to set the height to 0 and the width to 100px, and then a certain The side width is also set to 100, which is fine.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

.a{

width:100px;

height:100px;

background:#c00;

}

.b{

width:0;

height:0;

border-width:50px;

border-style:solid;

border-color:#095;

}

.c{

width:100px;

height:0;

border-width:0 0 100px;

border-style:solid;

border-color:#069;

}

Copy after login

How to perform regular polygon transformation of a single div with CSS

Regular pentagon

Regular pentagon needs to enter the field of basic trigonometric functions. Let us first Decompose the regular pentagon, use the original p as the upper triangle, and then use a pseudo element to create the lower trapezoid. Because the angle between each side of the regular pentagon is 108 degrees, you can calculate the upper triangle through trigonometric functions. The height is 59px (100 x cos(54)), the width is 192px (100x sin(54) x 2), the height of the lower trapezoid is 95px (100 x sin(72)), and the width of the long side is the same as the triangle above It's 192px.

How to perform regular polygon transformation of a single div with CSS

After understanding the principle, you can use pseudo elements to match and create!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

.a{

      position:relative;

  width:0;

  height:0;

  border-width:0 81px 59px;

      border-style:solid;

  border-color:transparent transparent #069;

}

.a:before{

  position:absolute;

  content:"";

  top:59px;

  left:-81px;

  width:100px;

  height:0;

  background:none;

  border-width:95px 31px 0;

  border-style:solid;   

  border-color:#069 transparent transparent;

    }

Copy after login

How to perform regular polygon transformation of a single div with CSS

Regular hexagon

Each angle of a regular hexagon is 120 degrees, if viewed in the direction of pure CSS If so, just change the triangle on the regular pentagon to make a regular hexagon, which is just a combination of the upper and lower trapezoids. The long side of the trapezoid is 200px (100 x cos (60) x 2 100 ), the height of the trapezoid is 87px (100 x sin(60)).

How to perform regular polygon transformation of a single div with CSS

So you can make a regular hexagon by slightly modifying the CSS of the regular pentagon.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

.a{

      position:relative;

    width:100px;

    height:0;

    border-width:0 50px 87px;

      border-style:solid;

    border-color:transparent transparent #f80;

}

.a:before{

  position:absolute;

  content:"";

    top:87px;

    left:-50px;

    width:100px;

    height:0;

  background:none;

    border-width:87px 50px 0;

  border-style:solid;   

    border-color:#f80 transparent transparent;

    }

Copy after login

How to perform regular polygon transformation of a single div with CSS

Regular heptagon

The after pseudo-element must be used at the beginning of the regular heptagon, because the regular heptagon must It needs to be disassembled into three memory blocks, using the original p as the upper triangle, a pseudo element as the middle trapezoid, and then another pseudo element as the bottom trapezoid. The angles of the regular heptagons are not integers. It is 128 and 4/7 degrees, and the second decimal place is approximately 128.57, so the calculated result is as shown in the figure below. The key point is that you must clearly know what the length and width are.

How to perform regular polygon transformation of a single div with CSS

After you have the length and width, start writing with CSS!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

.a{

      position:relative;

    width:0;

    height:0;

    border-width:0 90px 43px;

      border-style:solid;

    border-color:transparent transparent #09c;

}

.a:before{

  position:absolute;

  content:"";

    top:140px;

    left:-112px;

    width:100px;

    height:0;

    border-width:78px 62px 0;

  border-style:solid;   

    border-color:#09c transparent transparent;

    }

  .a:after{

    position:absolute;

    content:"";

    top:43px;

    left:-112px;

    width:180px;

    height:0;

    border-width:0 22px 97px;

    background:none;

    border-style:solid;

    border-color:transparent transparent #09c;

  }

Copy after login

How to perform regular polygon transformation of a single div with CSS

Regular octagon

The regular octagon actually turns the triangle on the regular heptagon into a trapezoid. Then the trapezoid in the middle becomes a rectangle. The angle between the regular octagon is 135 degrees. The calculated length and width of each area are as follows.

How to perform regular polygon transformation of a single div with CSS

If you understand the same principle, it will be much easier to do it with CSS!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

.a{

      position:relative;

    width:100px;

      height:0;

      border-width:0 71px 71px;

      border-style:solid;

    border-color:transparent transparent  #f69;

}

.a:before{

  position:absolute;

  content:"";

    top:171px;

      left:-71px;

      width:100px;

      height:0;

      border-width:71px 71px 0;

  border-style:solid;   

      border-color: #f69 transparent transparent;

    }

  .a:after{

      position:absolute;

      content:"";

      top:71px;

      left:-71px;

      width:242px;

      height:0;

      border-width:0 0 100px;

      background:none;

      border-style:solid;

      border-color:transparent transparent #f69;

  }

Copy after login

1How to perform regular polygon transformation of a single div with CSS

小结

以上就是纯粹利用CSS做出来的单一div的正多边形变换,如果熟练的话,其实加上动画效果,就可以做出像下面示例这个样子的变换动画啰!

How to perform regular polygon transformation of a single div with CSS

相关教程:css视频教程

The above is the detailed content of How to perform regular polygon transformation of a single div with CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles