Home >Web Front-end >CSS Tutorial >How to Create a Transparent Half-Circle Cutout in a Div using Only CSS3?

How to Create a Transparent Half-Circle Cutout in a Div using Only CSS3?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-03 03:20:39460browse

How to Create a Transparent Half-Circle Cutout in a Div using Only CSS3?

Creating a Transparent Half-Circle Cutout in a Div using CSS3

To construct a transparent half-circle cutout within a div using only CSS3, the background color of all elements that form the shape must remain either black or transparent. A technique that meets this requirement involves utilizing CSS's ::after pseudo property.

Here is an example of how to achieve the desired effect:

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);
}
<div class="rect"><span class="circle"></span></div>

This approach creates a rectangle with a black overlay and a semitransparent circle. The circle is positioned absolutely to create a transparent cutout shape.

The above is the detailed content of How to Create a Transparent Half-Circle Cutout in a Div using Only CSS3?. 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