
本文详解如何通过纯 css 构建带动态宽度、圆角边缘的顶部凹口(notch)——即视觉上“内陷”的倒圆角区域,支持自适应文本长度,并保持背景透明与边界平滑。
本文详解如何通过纯 css 构建带动态宽度、圆角边缘的顶部凹口(notch)——即视觉上“内陷”的倒圆角区域,支持自适应文本长度,并保持背景透明与边界平滑。
在现代 UI 设计中,卡片顶部添加一个带有圆角的“凹口(notch)”已成为一种轻量级且富有表现力的视觉手法,常用于突出标题、状态标识或交互入口。但 CSS 原生不支持“反向 border-radius”,因此需通过布局组合模拟该效果:用左右两块带单侧圆角的色块 + 中间一块底部圆角的黑色(或背景色)凹口区域,共同构成视觉上的“倒圆角缺口”。
核心实现思路
- 整体容器(
.screen)为红色主卡面,设置常规border-radius(如10px); - 顶部凹口区域(
.notch-area)采用display: flex+justify-content: space-between,使左右色块自动撑满,中间凹口区域宽度由内容决定; -
.left和.right分别设置 外侧圆角 + 内侧直角(例如border-radius: 10px 5px 0 0),形成向内收束的过渡; -
.notch区域使用相反方向的圆角(border-radius: 0 0 10px 10px),配合与背景一致的颜色(如黑色),在视觉上“挖出”一个圆润凹口; - 文本置于
.notch .content内,通过white-space: nowrap防止换行,padding微调留白,字体大小可响应式缩放。
完整可运行代码示例
<style>
:root {
--notch-top-radius: 5px;
--notch-bottom-radius: 10px;
--screen-radius: 10px;
--screen-bg: #e53935;
--notch-bg: #000;
}
body {
margin: 0;
padding: 0;
background: var(--notch-bg);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
.screen {
background: var(--screen-bg);
height: calc(100vh - 2rem);
margin: 1rem;
width: calc(100vh / 1.61);
display: flex;
flex-direction: column;
border-radius: var(--screen-radius);
color: white;
padding: 1rem;
box-sizing: border-box;
}
.notch-area {
background: var(--notch-bg);
display: flex;
justify-content: space-between;
width: 100%;
height: 1rem;
}
.notch-wrapper {
background: var(--screen-bg);
height: 1rem;
display: flex;
align-items: center;
justify-content: center;
}
.notch {
width: fit-content; /* 关键:宽度随内容自适应 */
height: 1rem;
background: var(--notch-bg);
border-radius: 0 0 var(--notch-bottom-radius) var(--notch-bottom-radius);
padding: 0 0.5rem;
}
.notch .content {
font-size: 0.6rem;
color: white;
white-space: nowrap;
line-height: 1rem;
}
.notch-area .left,
.notch-area .right {
width: 100%;
height: 1rem;
background: var(--screen-bg);
}
.notch-area .left {
border-radius: var(--screen-radius) var(--notch-top-radius) 0 0;
}
.notch-area .right {
border-radius: var(--notch-top-radius) var(--screen-radius) 0 0;
}
</style><div class="screen">
<div class="notch-area">
<div class="left"></div>
<div class="notch-wrapper">
<div class="notch">
<div class="content">Hello World</div>
</div>
</div>
<div class="right"></div>
</div>
<p>Doloribus est architecto laboriosam minus a dolorem labore...</p>
</div>
<div class="screen">
<div class="notch-area">
<div class="left"></div>
<div class="notch-wrapper">
<div class="notch">
<div class="content">Status: Active</div>
</div>
</div>
<div class="right"></div>
</div>
<p>Quibusdam fuga et quo impedit excepturi a voluptates optio...</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13837" title="htmlq 命令行 HTML 内容提取器,支持 CSS 选择器"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20d3191a227.jpg" alt="htmlq 命令行 HTML 内容提取器,支持 CSS 选择器" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13837" title="htmlq 命令行 HTML 内容提取器,支持 CSS 选择器" class="overflowclass">htmlq 命令行 HTML 内容提取器,支持 CSS 选择器</a>
<p class="overflowclass">htmlq 是一个命令行工具,使用 CSS 选择器从 HTML 中提取内容,相当于 jq 的 HTML 版本。该工具使用 Rust 编写,允许你通过 CSS 选择器将 HTML 管道化,直接从终端提取文本、属性和结构化内容。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13837" title="htmlq 命令行 HTML 内容提取器,支持 CSS 选择器" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
</div>
注意事项与局限性
- ✅ 适用场景:适用于纯色背景或简单渐变;凹口区域下方不可放置交互内容(因
.notch是覆盖层); - ⚠️ 动态宽度原理:依赖
width: fit-content+ Flex 布局的space-between,确保凹口宽度严格匹配文字长度; - ⚠️ 圆角一致性:
.left/.right的var(--notch-top-radius)必须与.notch的var(--notch-bottom-radius)协调,否则衔接处会出现锯齿或缝隙; - ? 复杂背景限制:若需在凹口后显示图片或纹理,建议改用
clip-path或 SVG;例如:.screen { clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%); } .notch { clip-path: inset(-1rem 0 0 0 round 0 0 10px 10px); }(注:
clip-path: inset()的圆角支持需现代浏览器)
此方案零依赖、语义清晰、易于维护,是构建轻量级 UI 凹口的可靠 CSS 实践。
前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!










