简体   繁体   English

如何在段落中添加行号?

[英]How can I add line numbering to my paragraph?

I'm trying to add line numbers in my paragraph, like shown on the screenshot, but I don't know how. 我正试图在我的段落中添加行号,如屏幕截图所示,但我不知道如何。 What should I use? 我该怎么用? CSS or Javascript? CSS还是Javascript? Thank you! 谢谢!

这是一个截图

You can create custom counter and use counter-increment: custom-counter+5 so it will increment by 5 but it will start from 5 not from 1, but you can add number 1 for first p with :before and exclude it from counter. 您可以创建自定义计数器并使用counter-increment: custom-counter+5因此它将增加5但它将从5开始而不是从1开始,但您可以为第一个p添加数字1 :before并将其从计数器中排除。

 .content { width: 200px; counter-reset: custom-counter; } p { display: table; } p:not(:first-child):before { counter-increment: custom-counter+5; content: counter(custom-counter)". "; display: table-cell; color: #aaa; } p:first-child:before { display: table-cell; color: #aaa; content: '1. ' } 
 <div class="content"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Praesentium, blanditiis.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Non, eligendi.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis, minus.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque, repellendus?</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Exercitationem, laboriosam.</p> </div> 

You can use CSS3 counters to achieve it. 您可以使用CSS3计数器来实现它。 Assuming your sections are paragraphs: 假设你的部分是段落:

body {
    counter-reset: section;
}

p::before {
    counter-increment: section;
    content: counter(section);
}

See the reference . 参见参考文献

I am assuming you do not want to pre-separate the lines, ie you let the browser automatically add the line-breaks. 我假设你不想预先分隔线,即你让浏览器自动添加换行符。

You could add a second column / div next to your paragraph and fill it with line numbers. 您可以在段落旁边添加第二列/ div,并用行号填充它。 Be sure to use the same line-height so it does not get misaligned. 请务必使用相同的行高,以免它错位。

On the other hand you could base on @LGSon's solution and the lining.js library to access single lines. 另一方面,您可以基于@LGSon的解决方案和lining.js库来访问单行。

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

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