search
HomeWeb Front-endCSS TutorialHow to perform regular polygon transformation of a single div with CSS

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.

width:0;
height:0;
border-width:0 50px 87px ;
border-style:solid;
border-color:transparent transparent #095;

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.

.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;
}

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!

.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;
    }

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.

.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;
    }

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!

.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;
  }

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!

.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;
  }

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
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
So Many Color LinksSo Many Color LinksApr 13, 2025 am 11:36 AM

There's been a run of tools, articles, and resources about color lately. Please allow me to close a few tabs by rounding them up here for your enjoyment.

How Auto Margins Work in FlexboxHow Auto Margins Work in FlexboxApr 13, 2025 am 11:35 AM

Robin has covered this before, but I've heard some confusion about it in the past few weeks and saw another person take a stab at explaining it, and I wanted

Moving Rainbow UnderlinesMoving Rainbow UnderlinesApr 13, 2025 am 11:27 AM

I absolutely love the design of the Sandwich site. Among many beautiful features are these headlines with rainbow underlines that move as you scroll. It's not

New Year, New Job? Let's Make a Grid-Powered Resume!New Year, New Job? Let's Make a Grid-Powered Resume!Apr 13, 2025 am 11:26 AM

Many popular resume designs are making the most of the available page space by laying sections out in a grid shape. Let’s use CSS Grid to create a layout that

One Way to Break Users Out of the Habit of Reloading Too MuchOne Way to Break Users Out of the Habit of Reloading Too MuchApr 13, 2025 am 11:25 AM

Page reloads are a thing. Sometimes we refresh a page when we think it’s unresponsive, or believe that new content is available. Sometimes we’re just mad at

Domain-Driven Design With ReactDomain-Driven Design With ReactApr 13, 2025 am 11:22 AM

There is very little guidance on how to organize front-end applications in the world of React. (Just move files around until it “feels right,” lol). The truth

Detecting Inactive UsersDetecting Inactive UsersApr 13, 2025 am 11:08 AM

Most of the time you don’t really care about whether a user is actively engaged or temporarily inactive on your application. Inactive, meaning, perhaps they

Wufoo   ZapierWufoo ZapierApr 13, 2025 am 11:02 AM

Wufoo has always been great with integrations. They have integrations with specific apps, like Campaign Monitor, Mailchimp, and Typekit, but they also

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.