You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用CSS实现居中带内圈及角连线、周边环绕文本的HTML六边形

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.main-section {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}
.outer {
  position: relative;
  width: 300px;
  height: 300px;
}
/* 六边形样式 */
.hexagon {
  position: absolute;
  width: 100%;
  height: 100%;
  border: 2px solid #222;
  clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
  background: #fff;
}
/* 中心圆形样式 */
.circle {
  position: absolute;
  width: 60px;
  height: 60px;
  border: 2px solid #222;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  z-index: 2;
}
/* 连线容器通用样式 */
.outer span {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  transform-origin: center;
  font-size: 16px;
  font-weight: 500;
}
/* 中心到顶点的连线 */
.outer span::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
  width: 2px;
  height: 50%;
  background: #222;
}
/* 逐个设置旋转角度、文本位置,反向旋转保证文字正向显示 */
.outer span:nth-of-type(1) {
  transform: rotate(0deg);
}
.outer span:nth-of-type(1)::after {
  content: "one";
  position: absolute;
  top: -25px;
  left: 50%;
  transform: translateX(-50%) rotate(0deg);
}

.outer span:nth-of-type(2) {
  transform: rotate(60deg);
}
.outer span:nth-of-type(2)::after {
  content: "two";
  position: absolute;
  top: 45px;
  right: -25px;
  transform: rotate(-60deg);
}

.outer span:nth-of-type(3) {
  transform: rotate(120deg);
}
.outer span:nth-of-type(3)::after {
  content: "three";
  position: absolute;
  bottom: 45px;
  right: -40px;
  transform: rotate(-120deg);
}

.outer span:nth-of-type(4) {
  transform: rotate(180deg);
}
.outer span:nth-of-type(4)::after {
  content: "four";
  position: absolute;
  bottom: -25px;
  left: 50%;
  transform: translateX(-50%) rotate(-180deg);
}

.outer span:nth-of-type(5) {
  transform: rotate(240deg);
}
.outer span:nth-of-type(5)::after {
  content: "five";
  position: absolute;
  bottom: 45px;
  left: -40px;
  transform: rotate(-240deg);
}

.outer span:nth-of-type(6) {
  transform: rotate(300deg);
}
.outer span:nth-of-type(6)::after {
  content: "six";
  position: absolute;
  top: 45px;
  left: -25px;
  transform: rotate(-300deg);
}

调整说明

  • 尺寸可按需修改:调整.outer的宽高、.circle的宽高、边框颜色和粗细即可适配不同设计需求
  • 文本偏移量可根据实际字体大小微调,保证文本在六边形外侧均匀分布
  • 使用clip-path实现的六边形自适应外层尺寸,无需手动计算六边形各边参数
  • 连线通过span伪元素生成,跟随span旋转自动对齐六边形六个顶点,无需额外定位

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 10:36:07