您可以使用伪元素::after并使用left:50%/transform:translateX(-50%)在中间对齐,无论width
h1 {
position: relative;
display:inline-block;
/* min-width:200px */
}
h1::after {
border-bottom: 1px solid red;
bottom: -10px;
content: "";
height: 1px;
position: absolute;
width: 20px;
left:50%;
transform: translateX(-50%);
}Run Code Online (Sandbox Code Playgroud)
<h1>This text can be longer than the border</h1>Run Code Online (Sandbox Code Playgroud)