Home > front end >  Center a div in a circle
Center a div in a circle

Time:05-25

I have a circle in which I can't center the div with text. I tried with flexbox but it doesn't seem to work or I did it wrong.

This is the code

.circle {
  border-radius: 50%;
  transition: all 2s;
  transition-delay: 0s;
  background-color: #14b1e7;
  width: 100%;
  padding-top: 100%;
  text-align: center;
}

.words {
  font-size: 40px;
  font-family: 'Montserrat', sans-serif;
  display: flex;
  animation-name: fade;
  animation-delay: 4s;
  animation-duration: 0.5s;
  animation-timing-function: ease-out;
  animation-iteration-count: forwards;
  animation-direction: absolute;
  font-weight: bold;
  justify-content: center;
  align-items: center;
}
<div >
  <div ></div>
  <div ></div>
  <div ></div>
</div>

CodePudding user response:

You can use display:flex, align-items:center and justify-content:center

.circle{
  width: 100px;
  height: 100px;
  background-color: #14b1e7;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div >
  <div>A</div>
  <div>B</div>
  <div>C</div>
</div>

CodePudding user response:

Problem comes from padding-top: 100%; on your .circle. You're using this to get a height on your .circle, but it is essentially pushing all of the nested div's content to the bottom. Instead, set a height on your html, body and use height: 100%; on your .circle. Then you can set display: flex; with align-items: center; and justify-content: center; on the parent.

html,
body {
  height: 100%;
}

.circle {
  height: 100%;
  border-radius: 50%;
  transition: all 2s;
  transition-delay: 0s;
  background-color: #14b1e7;
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.words {
  font-size: 40px;
  font-family: 'Montserrat', sans-serif;
  display: flex;
  animation-name: fade;
  animation-delay: 4s;
  animation-duration: 0.5s;
  animation-timing-function: ease-out;
  animation-iteration-count: forwards;
  animation-direction: absolute;
  font-weight: bold;
}
<div >
  <div >foo</div>
  <div >foo</div>
  <div >foo</div>
</div>

  • Related