如何在CSS中为文本内容添加水平分割线(即中间有一条横线)?
时间: 2025-01-18 10:04:18 浏览: 226
在CSS中,你可以通过伪元素`:before`和`:after`以及`content`属性来为文本内容添加水平分割线。这种方法通常用于无序列表、标题等需要分隔的地方。以下是一个基本的例子:
```css
.text-with-line {
position: relative; /* 或者 inline-block 如果是内联元素 */
}
.text-with-line::before {
content: "";
display: block;
height: 1px; /* 分割线高度 */
background-color: black; /* 设置颜色,可以自定义 */
width: 100%; /* 自适应宽度 */
}
/* 如果你想让分割线仅显示在行首或行尾 */
.text-with-line::after {
content: "";
display: block;
height: 1px;
background-color: black;
width: auto;
margin-left: auto; /* 如果是左对齐 */
margin-right: auto; /* 如果是右对齐 */
}
```
然后,在HTML中应用这个样式到需要加线的元素上:
```html
<p class="text-with-line">这是有分割线的文本</p>
```
阅读全文
相关推荐

















