Draw a circle using pure CSS (code example)

青灯夜游
Release: 2021-03-26 09:59:59
forward
3986 people have browsed it

This article introduces how to draw a circle using pure CSS through code examples. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Draw a circle using pure CSS (code example)

The idea of ​​drawing a circle is very simple: first draw two circles and set different background colors; then make the centers of the two circles coincide.

Difficulty factor

##☆☆

HTML

<div class="container">
    <span class="ring-style"></span>
</div>
Copy after login

Analysis:

    There is a parent container here

CSS

.container {
    position: relative;
    top: 0;
    left: 0;
    width: 300px;
    height: 300px;
    background-color: lightgrey;
}
.ring-style {
    display: inline-block;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: blue;
    width: 260px;
    height: 260px;
    border-radius: 260px;
}
.ring-style::before {
    content: &#39; &#39;;
    display: inline-block;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: white;
    width: 200px;
    height: 200px;
    border-radius: 200px;
}
Copy after login

Analysis:

    Set the width, height and rounded corners of the element The effect is to draw a circle
  • Create two circles through the ::before pseudo-element and the body element
  • By setting the top, left, and translate attributes based on the absolute positioning of the parent container, Center the element horizontally and vertically based on the parent container, so that the centers of the two circles coincide
[Recommended tutorial:

CSS video tutorial]

Rendering

Draw a circle using pure CSS (code example)

##Knowledge points

border-radius
  • :: before && ::after
  • Elements are centered horizontally and vertically
  • For more programming-related knowledge, please visit:
Programming Video

! !

The above is the detailed content of Draw a circle using pure CSS (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template