简体   繁体   English

CSS:如何在段落前添加一行?

[英]CSS: how to add a line before a paragraph?

I try to put a line before the first line of my text like this:我尝试在文本的第一行之前放置一行,如下所示:

在此处输入图片说明

I tried with a :before, but it doesn't work :我试过:之前,但它不起作用:

 p:before { content: ''; display: block; width:30px; top: 10px; bottom:30px; border-bottom: 4px solid #FBDBD3; }
 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent purus massa, dapibus eget ipsum ac, tincidunt feugiat justo. Duis ac dui bibendum, laoreet nunc eget, laoreet enim. Aenean ac congue ex. Sed id egestas diam, molestie interdum tellus.</p>

Make the p a flex container and you can easily adjust the line using margin:使p成为一个 flex 容器,您可以使用边距轻松调整行:

 p { display:flex; align-items:flex-start; } p:before { content: ''; width:50px; margin-top: 10px; margin-right:10px; border-bottom: 4px solid #FBDBD3; }
 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent purus massa, dapibus eget ipsum ac, tincidunt feugiat justo. Duis ac dui bibendum, laoreet nunc eget, laoreet enim. Aenean ac congue ex. Sed id egestas diam, molestie interdum tellus.</p>

Or use display:table :或使用display:table

 p { display:table; border-spacing:10px; } p:before { content: ''; width:50px; position:relative; top:10px; display:table-cell; border-top: 4px solid #FBDBD3; }
 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent purus massa, dapibus eget ipsum ac, tincidunt feugiat justo. Duis ac dui bibendum, laoreet nunc eget, laoreet enim. Aenean ac congue ex. Sed id egestas diam, molestie interdum tellus.</p>

Or rely on some padding and background to create the line and no need extra markup, pseudo-element and a lot of CSS:或者依靠一些填充和背景来创建线条,不需要额外的标记、伪元素和大量的 CSS:

 p { padding:0 0 0 100px; background:linear-gradient(to right,#FBDBD3,#FBDBD3) 0 5px/80px 4px no-repeat; }
 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent purus massa, dapibus eget ipsum ac, tincidunt feugiat justo. Duis ac dui bibendum, laoreet nunc eget, laoreet enim. Aenean ac congue ex. Sed id egestas diam, molestie interdum tellus.</p>

You can use display: flex on p element and then use margin on pseudo-element for space between line and text.您可以在p元素上使用display: flex ,然后在伪元素上使用边距作为行和文本之间的空间。

 p { display: flex; align-items: flex-start; } p:before { content: ''; width:30px; border-bottom: 4px solid #FBDBD3; margin-right: 15px; margin-top: 7px; }
 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent purus massa, dapibus eget ipsum ac, tincidunt feugiat justo. Duis ac dui bibendum, laoreet nunc eget, laoreet enim. Aenean ac congue ex. Sed id egestas diam, molestie interdum tellus.</p>

you can change some css for :before and p tag, make p tag relative so that you can play with :before in absolute position.您可以为 :before 和 p 标签更改一些 css,使 p 标签相对,以便您可以在绝对位置使用 :before 。

p {
    padding-left: 40px;
    position: relative;
}
p:before {
    content: '';
    display: block;
    width: 35px;
    border-bottom: 4px solid #FBDBD3;
    position: absolute;
    left: 0;
    top: 6px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM