How to Create Circles in CSS?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

Creating circles in CSS is simple and versatile, adding an appealing design element to web pages. To craft circles, use the border-radius property, setting it to 50% of the container's dimensions. For instance, if you have a square container, applying border-radius: 50%; will render a perfect circle. To create circles with different sizes, use percentage values based on your preferences. Additionally, CSS pseudo-elements like ::before or ::after can be employed to generate circles within containers. Circles can serve as decorative elements, buttons, or avatars, enhancing the visual appeal and user experience of your web interface. By mastering this technique, you can easily incorporate circles into your CSS-driven designs, making them more dynamic and engaging.

Pre-requisites

Before you can create circles in CSS, you should have a basic understanding of the following prerequisites:

  • HTML Structure:
    Familiarity with HTML is crucial, as you'll need to create the elements (e.g., divs, buttons, images) in which you want to display circles.
  • CSS Fundamentals:
    You should have a grasp of CSS basics, including selectors, properties, and values, to style and format your HTML elements.
  • Box Model:
    Understanding how the CSS box model works is essential, as it affects an element's dimensions and spacing, which can impact the appearance of circles.
  • Width and Height Properties:
    Knowledge of how to set the width and height properties for HTML elements, whether in pixels or other units, is necessary to define the size of your circles.
  • Border Properties:
    You'll need to know how to use CSS border properties, such as "border-radius", to create rounded shapes like circles.
  • Units of Measurement:
    Familiarity with units like pixels (%), ems, and rems is helpful for specifying dimensions and positioning elements accurately.
  • Basic Text and Content Styling:
    Understanding how to style text and content within your circles, such as colors, fonts, and alignment, can enhance the overall design.
  • Browser Developer Tools:
    Proficiency in using browser developer tools for debugging and inspecting your CSS code is beneficial for fine-tuning circle designs and troubleshooting issues.
  • Once you have these prerequisites in place, you'll be well-prepared to create circles and other shapes in CSS to enhance your web designs.

How to Create Circles in CSS?

Create HTML

To create a circle in CSS, you'll first need an HTML element to apply your CSS styles. Typically, you can use a <div> element for this purpose. Here's the HTML part with an explanation:

  • Basic Circle using CSS border-radius Property:
    This creates a basic circle using CSS border-radius. The circle-basic class is used for styling.
  • Circle with Text using CSS border-radius Property:
    This creates a circle with text inside it using CSS border-radius. The circle-text class is used for styling.
  • Circle with Text using <span> Element:
    This is another way to create a circle with text inside by using a <span> element. The circle-with-text class is used for styling.
  • Responsive Circle using Percentage Units:
    This demonstrates a responsive circle using percentage units for width and padding. The circle-responsive class is used for styling.
  • SVG Circle using SVG <circle> Element:
    An SVG circle is created using the <circle> element inside an <svg> element. It's styled directly in the HTML.
  • Canvas Circle using HTML <canvas> Element:
    A canvas circle is drawn using JavaScript inside the <canvas> element. The <script> section provides the JavaScript code for drawing the circle.

Add CSS

Using CSS border-radius Property:

  • width and height define the dimensions of the circle.
  • background-color sets the circle's background color.
  • border-radius: 50%; creates a circular shape by making the border-radius equal to half of the width and height.
  • text-align and line-height center the text vertically and horizontally.
  • color sets the text color.
  • font-size determines the font size.
  • margin-bottom adds space between circles if you have multiple.

Using <div> Elements (Circle with Text inside):

  • This CSS is similar to the basic circle, but with adjusted dimensions to accommodate text inside.
  • width and height are increased for a larger circle.
  • background-color sets the circle's background color.
  • border-radius: 50%; creates a circular shape.
  • text-align and line-height center the text vertically and horizontally.
  • color sets the text color.
  • font-size determines the font size.

Creating a Responsive Circle (Using Percentage Units):

  • width: 50%; and padding-top: 50%; create a responsive square container for the circle.
  • background-color sets the circle's background color.
  • border-radius: 50%; creates a circular shape.
  • text-align and line-height center the text vertically and horizontally.
  • color sets the text color.
  • font size determines the font size.

Using SVG <circle> Element (SVG Circle)

  • cx and cy:
    Define the center coordinates of the circle.
  • r:
    Specifies the radius of the circle.
  • fill:
    Sets the fill color of the circle.
  • stroke:
    Determines the color of the circle's outline.
  • stroke-width:
    Sets the width of the outline.
  • opacity:
    Adjusts the transparency of the circle.

Using HTML <canvas> Element (Canvas Circle):

The CSS code provides an optional border around the <canvas> element, adding a visual distinction to the canvas on the webpage. This border is useful for styling purposes but doesn't directly affect the drawing of the circle.

Conclusion

  • Using the border-radius property to set div elements as circles.
  • Styling div elements to customize circle appearance.
  • Combining <div> and <span> elements for text inside circles.
  • Creating responsive circles with percentage-based dimensions.
  • Employing SVG <circle> elements with attributes for circle design.
  • Drawing circles on HTML <canvas> elements using JavaScript.
  • Adapting circle properties like radius, fill, and border color.
  • Tailoring circles to diverse design needs and responsive layouts.