Home > Web Front-end > CSS Tutorial > How Can I Create a Seamless Half-Circle Cutout Using Only CSS?

How Can I Create a Seamless Half-Circle Cutout Using Only CSS?

Linda Hamilton
Release: 2024-12-21 16:45:11
Original
225 people have browsed it

How Can I Create a Seamless Half-Circle Cutout Using Only CSS?

Creating Seamless Transparent Cutouts with CSS

Achieving a transparent cut out shape in a div using only CSS3 can be a challenge, especially when the shape is not a simple geometric figure. This article presents a solution that employs the ::after pseudo property to create a seamless half circle cutout, ensuring that all elements remain either black or transparent.

The approach involves creating a black rectangle with a transparent circle placed over it. However, to achieve the desired cutout effect, the circle requires a transparent radius extending beyond its boundaries. This is achieved by using the ::after pseudo element with a large border to create the half circle shape.

body {
  background: green;
}

.rect {
  height: 100px;
  width: 100px;
  background: rgba(0, 0, 0, 0.5);
  position: relative;
  margin-top: 100px;
  margin-left: 100px;
}

.circle {
  display: block;
  width: 100px;
  height: 50px;
  top: -50px;
  left: 0;
  overflow: hidden;
  position: absolute;
}

.circle::after {
  content: '';
  width: 100px;
  height: 100px;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  border-radius: 100px;
  background: rgba(0, 0, 0, 0);
  position: absolute;
  top: -100px;
  left: -40px;
  border: 40px solid rgba(0, 0, 0, 0.5);
}
Copy after login

This code creates a black rectangle with a transparent half circle cutout on the top half. The ::after pseudo element positions a transparent circle above the rectangle and applies a wide black border to create the rounded shape. The transparent background of the ::after element allows the rectangle's color and surrounding background to show through, resulting in the seamless cutout effect.

The above is the detailed content of How Can I Create a Seamless Half-Circle Cutout Using Only CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template