How to draw a circle in an HTML page?
Here is a quick recap on how to draw a circle using CSS. Create a div
element with equal width and height to get a square shape, then apply border-radius: 50%;
to turn it into a circle.
By altering the width
, height
, and background
the size, shape, and color of the circle can be tweaked.
Implementing text and tailoring circle style
Text inside the circle
Inserting text inside your circular div requires the implementation of CSS positioning:
This technique uses position: absolute;
in conjunction with transform: translate(-50%, -50%);
to center the text.
Circle style customization
Use CSS properties such as border
, padding
, and display
to add distinct styles:
Apply color
and font-size
to enhance text readability and accessibility:
Creating circle using alternative methods
Creating circles with SVG
For more complex objects or additional visual modifications, SVG comes to the rescue:
The <circle>
element within SVG is like the superhero of circles — created to serve its purpose.
Using the HTML5 canvas tag
If you're looking for dynamic circle rendering, the <canvas>
tag and its sidekick, JavaScript, have your back:
The arc()
function in JavaScript is like the unsung hero, quietly rendering circles within canvas
.
Extras
Unicode characters and custom fonts
Ever thought of using Unicode characters or fonts that offer glyphs appearing as circles? Combine them with your text and create awesome designs.
Cross-browser compatibility
The good news is that mox
and webkit
prefixes are almost obsolete thanks to CSS3. Yet, validating your HTML5 and CSS3 code, and checking the compatibility with your target browsers, never hurts.
Aligning items
Use display: inline-block;
or the mighty flexbox properties to align your divs, just like aligning ducks in a row!
Keeping text at center within the circle
position: absolute;
along with vertical-align
and calculated offsets or flexbox can help you place text within the circle. No more playing hide and seek!
Was this article helpful?