I am trying to create an image like the following using a combination of HTML and CSS, but I can’t seem to get the ‘petals’ to rotate around the middle.
Here is my code:
.flower{
height: 600px;
width: 600px;
margin: 20px;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.mid{
width: 200px;
height: 200px;
border-radius: 50%;
background: #35414d;
z-index: 4;
}
.petal{
position: absolute;
left: 40px;
width: 180px;
height: 180px;
border-radius: 50%;
background: #b5a9d4;
}
.petal.p1 {
transform: rotate(22.5deg);
z-index: 5;
}
.petal.p2 {
transform: rotate(50deg);
z-index: 5;
}
.petal.p3 {
transform: rotate(100deg);
z-index: 5;
}
<div class="flower" >
<div class="mid"></div>
<div class="petal p1"></div>
<div class="petal p2"></div>
<div class="petal p3"></div>
<div class="petal p4"></div>
<div class="petal p5"></div>
<div class="petal p6"></div>
<div class="petal p7"></div>
<div class="petal p8"></div>
</div>
What transform values would work best to get 8 petals to surround the center circle?
3
Answers
You need to set the
transform-origin
of each petal to the centre of the flower. The transform origin is the point around which each petal will be rotated. So if you place all your petals on the flower’s edge, and set the transform origin to the centre, when you applytransform: rotate()
to each petal, they will be rotated around the edges of the flower.Make 8 copies of a
.mid
and a.petal
within it. Then position the.mid
s on top of each other withz-index
1 to 8. Next rotate each.mid
45deg
progressively (ex. 0, 45, 90,… 315).