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

如何通过纯CSS实现表单行的完全圆角左侧边框?

问题解决:仅用CSS实现左侧药丸形标记

问题背景

我正在重做应用中的部分UI,希望将当前样式替换为目标样式。现有HTML和CSS代码如下,添加div并设置为药丸形状的方案需要修改大量代码,因此更希望通过CSS实现。我了解到可使用::after等伪元素,但不确定能否仅通过CSS达成目标。

当前HTML代码

<div class="row required"><label for="NumericFieldStartKm-numericfield">Kilometer at start</label><input id="NumericFieldStartKm-numericfield" value="" class="required numbercontrol mbsc-comp" min="1" max="" readonly=""></div>

当前CSS代码

.row.required { border-left: 7px solid #C30000; padding-left: 7px; border-top-left-radius: 6px; border-bottom-left-radius: 6px; margin-left: 4px !important; }

解决方案

可以通过伪元素::before实现需求,无需修改HTML结构,具体代码如下:

圆形标记实现

.row.required {
  position: relative;
  padding-left: 24px; /* 为左侧标记预留空间 */
  margin-left: 4px !important;
  /* 移除原有左侧边框样式 */
  border-left: none;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}

.row.required::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%); /* 垂直居中 */
  width: 12px;
  height: 12px;
  background-color: #C30000;
  border-radius: 50%; /* 圆形效果 */
}

药丸形(椭圆)标记实现

如果需要椭圆药丸效果,调整伪元素的宽高和圆角即可:

.row.required::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 18px;
  height: 8px;
  background-color: #C30000;
  border-radius: 4px; /* 椭圆圆角 */
}

原理说明

  • 给.row.required设置position: relative,让伪元素可以基于容器定位
  • 用::before生成空元素,通过绝对定位放在容器左侧并垂直居中
  • 调整容器内边距避免内容和标记重叠,同时移除原有边框样式消除冲突

内容的提问来源于stack exchange,提问作者Koen van der Borght

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 01:40:38