You need to enable JavaScript to run this app. 使用CSS Flexbox实现2×3响应式等比正方形及嵌套居中字母圆形
优惠活动
大模型
产品
解决方案
定价
更多

使用CSS Flexbox实现2×3响应式等比正方形及嵌套居中字母圆形

解决方案

完整可运行代码如下:

CSS代码

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.container {
  display: flex;
  width: 100%;
}
.blokrij {
  display: flex;
  flex-direction: column;
  width: 50%;
}
.vierkant {
  position: relative;
  flex: 1 0 auto;
  background-color: hsla(26, 100%, 50%, 1.00);
  border-radius: 10%;
  margin: 1px;
}
.vierkant::before {
  content: '';
  display: block;
  padding-top: 100%;
}
.rond {
  position: absolute;
  top: 10%;
  left: 10%;
  width: 80%;
  height: 80%;
  background-color: blue;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: clamp(1rem, 15vw, 8rem);
  color: #fff;
}

HTML代码

<div class="container">
  <div class="blokrij">
    <div class="vierkant">
      <div class="rond">L</div>
    </div>
    <div class="vierkant">
      <div class="rond">O</div>
    </div>
    <div class="vierkant">
      <div class="rond">I</div>
    </div>
  </div>
  <div class="blokrij">
    <div class="vierkant">
      <div class="rond">L</div>
    </div>
    <div class="vierkant">
      <div class="rond">O</div>
    </div>
    <div class="vierkant">
      <div class="rond">I</div>
    </div>
  </div>
</div>

核心调整说明

  • 全布局基于Flex实现,未使用Grid、表格相关属性,完全符合要求
  • 通过伪元素padding-top:100%实现正方形1:1固定宽高比,完全响应式,自动撑满外层容器
  • 调整了元素嵌套结构,将文字放入圆形容器内,通过Flex实现文字垂直水平居中
  • 字体使用clamp()相对单位,随容器尺寸自动缩放,不会出现溢出问题
  • 如需调整正方形和圆形的间距,修改.rond的top/left/width/height参数即可

内容的提问来源于stack exchange,提问作者Kopschot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 01:54:04