Home  >  Article  >  Web Front-end  >  How to Create a Half-Circle with CSS Using a Single Div?

How to Create a Half-Circle with CSS Using a Single Div?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-26 09:29:29193browse

How to Create a Half-Circle with CSS Using a Single Div?

Creating a Half Circle with CSS

Question:

How do you create a half-circle using only CSS, with a single div element and without relying on external tools like SVG or graphics APIs?

Answer:

Achieving a half-circle effect with CSS can be accomplished by leveraging border properties. To do this:

  1. Use border-top-left-radius and border-top-right-radius to round the corners of the box based on its height and any added borders. For example:
border-top-left-radius: 110px;  // Height + Border
border-top-right-radius: 110px; // Height + Border
  1. Add borders to the top, right, and left sides of the box. Set the border-bottom to zero to remove the bottom border.
border: 10px solid gray;
border-bottom: 0;

This combination of rounded corners and borders creates a half-circle shape.

Example:

<code class="css">.half-circle {
    width: 200px;
    height: 100px;
    border-top-left-radius: 110px;
    border-top-right-radius: 110px;
    border: 10px solid gray;
    border-bottom: 0;
}</code>

Alternative Method:

An alternative approach is to use box-sizing: border-box, which calculates the width and height of the box including borders and padding. This way, you can specify the height as exactly half of the width:

<code class="css">.half-circle {
    width: 200px;
    height: 100px;
    border-top-left-radius: 100px;
    border-top-right-radius: 100px;
    border: 10px solid gray;
    border-bottom: 0;

    box-sizing: border-box;
}</code>

Both methods can achieve the desired half-circle effect using purely CSS techniques.

The above is the detailed content of How to Create a Half-Circle with CSS Using a Single Div?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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