如何用HTML/CSS在圆形及其交叠区域放置文本并避免溢出?
解决方案:实现带文本的重叠圆形布局
核心思路
放弃绝对定位的不稳定布局,改用CSS 裁剪(clip-path) 或 遮罩(mask) 直接限制文本的显示范围,配合负边距/网格布局精准控制圆形重叠,确保交叠区域的文本完全贴合圆形边界,不会溢出。
实现方案一:Clip-path 裁剪文本
HTML 结构
简化冗余元素,让布局层级更清晰:
<main> <div class="circles-container"> <div class="circle left-circle"> <div class="text text-left">Text 1</div> </div> <div class="circle right-circle"> <div class="text text-right">Text 3</div> </div> <div class="intersection-text">Text 2</div> </div> </main>
CSS 样式
html { color: white; font-family: sans-serif; font-size: 20px; text-align: center; } .circles-container { position: relative; width: 450px; /* 左圆偏移后总宽度:300px + 300px*25% */ margin: 0 auto; } .circle { width: 300px; height: 300px; border-radius: 50%; display: flex; align-items: center; } .left-circle { background-color: #3a7487; } .right-circle { background-color: #9d4767; margin-left: -75px; /* 实现25%的重叠:300px*25% */ } .text { width: 150px; /* 非重叠区域宽度:圆形直径的一半 */ } .text-left { margin-left: 150px; /* 左圆非重叠区靠右侧 */ } .intersection-text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 150px; /* 交叠区域宽度等于重叠部分的直径 */ /* 用圆形裁剪文本,确保不会溢出圆形边界 */ clip-path: circle(75px at center); z-index: 1; }
实现方案二:Grid + Mask 遮罩(更简洁)
CSS 样式替换
用 Grid 布局实现圆形重叠,配合遮罩限制文本范围:
html { color: white; font-family: sans-serif; font-size: 20px; text-align: center; } .circles-container { display: grid; width: 450px; margin: 0 auto; position: relative; } .circle { grid-column: 1 / -1; width: 300px; height: 300px; border-radius: 50%; display: flex; align-items: center; } .left-circle { background-color: #3a7487; justify-content: flex-end; padding-right: 75px; /* 避开重叠区 */ } .right-circle { background-color: #9d4767; justify-self: end; justify-content: flex-start; padding-left: 75px; /* 避开重叠区 */ } .intersection-text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 150px; /* 遮罩效果,仅显示圆形范围内的文本 */ mask: radial-gradient(circle, black 100%, transparent 100%); -webkit-mask: radial-gradient(circle, black 100%, transparent 100%); z-index: 1; }
方案优势
- 文本无溢出:通过裁剪/遮罩直接锁定文本显示范围,完全贴合圆形边界
- 布局稳定:用负边距或Grid实现重叠,比绝对定位更易维护和调整
- 样式独立:三个文本区域各自可控,便于修改字体、颜色和对齐方式
内容的提问来源于stack exchange,提问作者Nellie
相关产品推荐
相关产品推荐

