Table of Contents
Drawing Togo's Flag with CSS
Alvaro Montoro ・ Nov 19
Home Web Front-end CSS Tutorial Drawing Taiwan&#s Flag with CSS

Drawing Taiwan&#s Flag with CSS

Nov 24, 2024 am 11:24 AM

I was impressed very much by @alvaromontoro's work

Drawing Taiwan

Drawing Togo's Flag with CSS

Alvaro Montoro ・ Nov 19

#css #html #webdev
of drawing a Togo flag with a single div and bunch of magic CSS. It's truly mind-blowing for me. Therefore, I want to do the same thing. Here is how I've done it.

HTML

I add one

with some aria attributes. This will be the single

to render the flag.
<div role="img" aria-label="Flag of Taiwan" class="flag taiwan"></div>
Copy after login
Copy after login

CSS

I used the same method as @alvaromontoro to create a basic background of the Taiwan flag: red background color with a blue rectangle on the top-left.

.flag.taiwan {
  aspect-ratio: 3 / 2;
  height: 500px;
  position: relative;
  background: linear-gradient(rgb(19, 53, 129) 0 0) 0 0 / 50% 50% no-repeat,
    rgb(205, 44, 36);
}
Copy after login
Copy after login

Drawing Taiwan

The Sun

Ok, the easiest part is finished. Now it is time for the real deal. The sun on the flag.
Drawing Taiwan
It looks pretty complicated, with 12 beams and a circle at the center. How is it possible to use the pseudo elements, ::before and ::after to draw them? It feels like only the clip-path: path() is the only way to do that because path() can draw any shape we want. However, clip-path: path() has a fatal disadvantage: it's not responsive! That means the flag can have only one size if I choose that approach.

I started googling many SVG files of Taiwan flag. And I noticed that they only use 2 elements to represent the sun.

  1. a white 12-beam star
  2. the white circle with a blue border

Drawing Taiwan

Drawing Taiwan
When the circle is put down on the center of the star. It looks like there are 12 beams surrounding the circle with a desired gap. So smart!. It seems like designers have already figured out this clever way to draw that sun. By taking this approach, I can use ::before as the circle and ::after as the star.

12-beam star

It is very easy to find SVG files of Taiwan flag. Unfortunately, all the stars are drawn by path(). It's because path() is responsive when it is in a real so they do not have this problem. It is only not responsive in clip-path. The polygon is responsive but I did not find a way to convert path to polygon.

It is a really difficult problem. My final solution is really to calculate all the positions of all the points of the 12-beam star? I utilized this fantastic online SVG Path Editorhttps://yqnn.github.io/svg-path-editor/ to visualize all points in the path.

Drawing Taiwan
And I asked my dear brother who is very good at math what are the positions of the rest of the points. He used mathematica to solve 12 linear equations and got all the points! ?

Drawing Taiwan

Then, I backed to the path editor to draw the outline of the star as the path and scaled it so it will be in a 100*100 scope.

Drawing Taiwan

I then convert all positions to percentages since it's already in 100*100 scope. As a result, we can display the star on the flag

<div role="img" aria-label="Flag of Taiwan" class="flag taiwan"></div>
Copy after login
Copy after login

Drawing Taiwan

The circle

The circle is relatively easier. However, my first attempt was using the border. It failed because the width of the border can only be px. I changed to use radial-gradient. The tricky part is that the percentage in radial-gradient needs to be the diagonal of the element so it also needs some math but it's not that hard.

.flag.taiwan {
  aspect-ratio: 3 / 2;
  height: 500px;
  position: relative;
  background: linear-gradient(rgb(19, 53, 129) 0 0) 0 0 / 50% 50% no-repeat,
    rgb(205, 44, 36);
}
Copy after login
Copy after login

Result

The full CSS is

&::after {
    content: '';
    position: absolute;
    top: 6.25%;
    left: 12.5%;
    width: 25%;
    height: calc(3 / 8 * 100%);
    background: white;
    clip-path: polygon(50% 0%,56.6987% 25%,75% 6.6987%,68.3013% 31.6987%,93.3013% 25%,75% 43.3013%,100% 50%,75% 56.6987%,93.3013% 75%,68.3013% 68.3013%,75% 93.3013%,56.6987% 75%,50% 100%,43.3013% 75%,25% 93.3013%,31.6987% 68.3013%,6.6983% 75%,25% 56.6987%,0% 50%,25% 43.3013%,6.6983% 25%,31.6987% 31.6987%,25% 6.6983%,43.3013% 25%);;
  }
Copy after login

You can also check out my work on codepen below

Hope you like it!

The above is the detailed content of Drawing Taiwan&#s Flag 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)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

See all articles