以 div 为圆心
Center a div in a circle
我有一个圆圈,我无法将 div 与文本居中。
我试过 flexbox 但它似乎不起作用或者我做错了。
这是代码
.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 class="circle">
<div class="words"></div>
<div class="words1"></div>
<div class="words2"></div>
</div>
您可以使用 display:flex
、align-items:center
和 justify-content:center
.circle{
width: 100px;
height: 100px;
background-color: #14b1e7;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
<div class="circle">
<div>A</div>
<div>B</div>
<div>C</div>
</div>
问题来自您的 .circle
上的 padding-top: 100%;
。您正在使用它来获得 .circle
的高度,但它实际上是将所有嵌套的 div 的内容推到底部。相反,在 html, body
上设置 height
并在 .circle
上使用 height: 100%;
。然后你可以设置 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 class="circle">
<div class="words">foo</div>
<div class="words1">foo</div>
<div class="words2">foo</div>
</div>
我有一个圆圈,我无法将 div 与文本居中。 我试过 flexbox 但它似乎不起作用或者我做错了。
这是代码
.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 class="circle">
<div class="words"></div>
<div class="words1"></div>
<div class="words2"></div>
</div>
您可以使用 display:flex
、align-items:center
和 justify-content:center
.circle{
width: 100px;
height: 100px;
background-color: #14b1e7;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
<div class="circle">
<div>A</div>
<div>B</div>
<div>C</div>
</div>
问题来自您的 .circle
上的 padding-top: 100%;
。您正在使用它来获得 .circle
的高度,但它实际上是将所有嵌套的 div 的内容推到底部。相反,在 html, body
上设置 height
并在 .circle
上使用 height: 100%;
。然后你可以设置 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 class="circle">
<div class="words">foo</div>
<div class="words1">foo</div>
<div class="words2">foo</div>
</div>