Home >Web Front-end >CSS Tutorial >How to Create a CSS Hexagon with Fully Curved Edges?

How to Create a CSS Hexagon with Fully Curved Edges?

Susan Sarandon
Susan SarandonOriginal
2024-12-02 01:44:15878browse

How to Create a CSS Hexagon with Fully Curved Edges?

CSS Curved Edge Hexagon

Using CSS, you can create a hexagon with curved edges. However, you may encounter an issue where the top and bottom edges remain straight. Here's a solution to make all edges curved:

Original CSS:

#hexagon-circle {
    width: 100px;
    height: 55px;
    background: red;
    position: relative;
    border-radius: 10px;
}
#hexagon-circle:before {
    content: "";
    position: absolute;
    top: -25px;
    left: 0;
    width: 0;
    height: 0;
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    border-bottom: 29px solid red;
    border-radius: 10px;
}
#hexagon-circle:after {
    content: "";
    position: absolute;
    bottom: -25px;
    left: 0;
    width: 0;
    height: 0;
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    border-top: 29px solid red;
    border-radius: 10px;
}

Solution:

Replace the original CSS with the following:

.hex {
  position: relative;
  margin: 1em auto;
  width: 10em;
  height: 17.32em;
  border-radius: 1em/.5em;
  background: orange;
  transition: opacity .5s;
}

.hex:before,
.hex:after {
  position: absolute;
  width: inherit;
  height: inherit;
  border-radius: inherit;
  background: inherit;
  content: '';
}

.hex:before {
  -webkit-transform: rotate(60deg);
  transform: rotate(60deg);
}

.hex:after {
  -webkit-transform: rotate(-60deg);
  transform: rotate(-60deg);
}

HTML:

<div>

This approach creates a hexagon with curved edges using border-radius and transformations.

The above is the detailed content of How to Create a CSS Hexagon with Fully Curved Edges?. 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