要实现一个圆形div并在其中垂直对齐文本和副标题,我们需要掌握以下几个CSS概念:
border-radius
设置为50%来创建圆形<div class="circle-container">
<div class="circle">
<div class="content">
<h2>主标题</h2>
<p>副标题</p>
</div>
</div>
</div>
<style>
.circle-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.circle {
width: 200px;
height: 200px;
border-radius: 50%;
background-color: #3498db;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
color: white;
}
.content h2 {
margin: 0;
font-size: 24px;
}
.content p {
margin: 5px 0 0;
font-size: 14px;
}
</style>
<div class="circle">
<div class="content">
<h2>主标题</h2>
<p>副标题</p>
</div>
</div>
<style>
.circle {
width: 200px;
height: 200px;
border-radius: 50%;
background-color: #2ecc71;
display: grid;
place-items: center;
text-align: center;
color: white;
}
.content h2 {
margin: 0;
font-size: 24px;
}
.content p {
margin: 5px 0 0;
font-size: 14px;
}
</style>
原因:当文本内容过长时,可能会超出圆形div的范围 解决方案:
word-break: break-word
或overflow-wrap: break-word
原因:父容器没有设置高度或使用了错误的布局方式 解决方案:
原因:宽度和高度不相等 解决方案:
aspect-ratio: 1
保持宽高比.circle {
width: 20vw;
height: 20vw;
/* 其他样式 */
}
.content {
padding: 10px;
width: 80%;
}
.circle {
transition: all 0.3s ease;
}
.circle:hover {
transform: scale(1.05);
}
没有搜到相关的文章