简体   繁体   English

段落CSS和HTML中的编号行

[英]Numbering lines in a paragraph CSS & HTML

I'm attempting to make a code-like style for content that is responsive to different window sizes. 我正在尝试为响应不同窗口大小的内容制作类似代码的样式。 I want to number lines of a paragraph using purely CSS, but I haven't been able to figure out how. 我想使用纯CSS为段落编号,但是我还无法弄清楚该怎么做。 I'm certain there must be some way given all of the cool and amazing things I've seen with pure CSS lately. 我敢肯定,一定有某种方式可以提供我最近使用纯CSS所见过的所有奇妙的东西。

Here is what I have so far, but it obviously only numbers the paragraphs. 这是我到目前为止的内容,但是显然它仅对段落编号。

p:before {
    counter-increment: para;
    content: counter(para);
    margin-right: 7px;
}

Fiddle for forking: https://jsfiddle.net/joshuarlowry/tsbza4j6/ 小提琴分叉: https//jsfiddle.net/joshuarlowry/tsbza4j6/

Ideas... 想法...

  • Maybe it is possible to fake it by creating a ::before that is the same height of the paragraph (somehow) and counts up until the bottom. 也许可以通过创建::before来伪造它,该::before与段落的高度相同(以某种方式)并计数到底部。
  • Maybe it is possible to somehow turn paragraphs into individual <span> for each line dynamically based on the area and count normally off of those. 也许可以以某种方式根据面积动态地将每一行的段落转换成单独的<span> ,并通常从这些计数中扣除。
  • Maybe we can style room to the left of the paragraph and draw the count there some other way. 也许我们可以在段落左侧设置空间样式,然后以其他方式绘制计数。

Here is some sample content for reference. 这是一些示例内容供参考。 I do not control the formatting. 我不控制格式。

<p>
Cupcake ipsum dolor sit amet pie tart. Brownie pudding lemon drops fruitcake pastry jelly-o cake macaroon cookie. Dessert wafer biscuit cookie gummies sweet carrot cake. Dragée fruitcake I love marzipan gingerbread sugar plum. I love brownie oat cake I love toffee chocolate jelly marzipan. Jelly beans powder gummies chocolate powder caramels powder sweet. Chocolate cake carrot cake I love. Sugar plum gummi bears chocolate bar dragée carrot cake bear claw apple pie chocolate cake topping. I love candy canes bonbon sweet tart jelly beans caramels dragée. Gingerbread I love dragée muffin chupa chups I love toffee sweet I love. Jelly-o toffee danish powder gingerbread I love I love bear claw. I love dragée jujubes I love oat cake. Pie chocolate bar cotton candy. Macaroon fruitcake jujubes danish candy canes lemon drops I love sesame snaps.
</p>
<p>
Pastry cotton candy tiramisu bonbon powder gummi bears cotton candy. Biscuit jelly powder I love cheesecake pudding topping. Cheesecake lollipop dessert pastry marshmallow icing cotton candy halvah chocolate. Donut brownie lollipop jelly-o jujubes jelly-o. Liquorice pie cake toffee cake. Icing pastry pie gingerbread cupcake chocolate cake. I love cookie cake croissant marzipan jelly beans cupcake cheesecake marshmallow. Chocolate cake apple pie chupa chups chocolate cake candy canes biscuit I love cookie sweet. Cotton candy caramels topping sweet roll muffin. Gingerbread cake icing pastry pudding fruitcake lollipop halvah I love. Tiramisu tootsie roll macaroon liquorice. Toffee marzipan macaroon toffee jelly-o cupcake pastry gummies ice cream. Jelly candy canes sweet roll lollipop.
</p>

Text from Cupcake Ipsum: http://www.cupcakeipsum.com/ 来自Cupcake Ipsum的文字: http ://www.cupcakeipsum.com/

A text editor assigns a unique line number to each line as denoted by a carriage return. 文本编辑器为每行分配一个唯一的行号,如回车符所示。 If the text simply wraps onto multiple lines, it will still only be assigned a single line number. 如果文本仅换行,则仍将仅分配一个行号。

With that in mind, and given that you're working with HTML, the carriage return would instead be represented by a <br> element. 考虑到这一点,考虑到您正在使用HTML,回车符将由<br>元素表示。

Thus, the following code will find all <p> elements, split their content on <br> elements, and wrap the resulting lines in <span> elements. 因此,以下代码将找到所有<p>元素,将它们的内容拆分到<br>元素上,并将结果行包装在<span>元素中。

 Array.prototype.slice.call(document.querySelectorAll('p')) .forEach((p) => { p.innerHTML = p.innerHTML.split('<br>') .map((l) => `<span>${l.trim()}</span>`) .join(''); }); 
 body { counter-reset: p; } p { margin: 0; padding-left: 40px; } p > span { display: block; margin-bottom: 5px; position: relative; } p > span::before { counter-increment: p; content: counter(p) "."; left: -30px; position: absolute; } 
 <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit<br> Dolores, nobis repellendus, molestias aperiam aspernatur praesentium, perferendis inventore unde ducimus ab excepturi<br> Sunt illum magni necessitatibus ad similique est tenetur. Facere. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis voluptatum assumenda dolorem vero soluta commodi maiores temporibus. </p> 

You need to wrap p tags in a wrapper and using counter-icreament , you can achieve this 您需要将p标签wrapperwrapper并使用counter-icreament ,您可以实现此目的

 .wrapper { counter-reset: my-awesome-counter; } p { counter-increment: my-awesome-counter; } p:before { content: counter(my-awesome-counter); margin-right: 7px; } 
  <div class="wrapper"> <p> Cupcake ipsum dolor sit amet pie tart. Brownie pudding lemon drops fruitcake pastry jelly-o cake macaroon cookie. Dessert wafer biscuit cookie gummies sweet carrot cake. Dragée fruitcake I love marzipan gingerbread sugar plum. I love brownie oat cake I love toffee chocolate jelly marzipan. Jelly beans powder gummies chocolate powder caramels powder sweet. Chocolate cake carrot cake I love. Sugar plum gummi bears chocolate bar dragée carrot cake bear claw apple pie chocolate cake topping. I love candy canes bonbon sweet tart jelly beans caramels dragée. Gingerbread I love dragée muffin chupa chups I love toffee sweet I love. Jelly-o toffee danish powder gingerbread I love I love bear claw. I love dragée jujubes I love oat cake. Pie chocolate bar cotton candy. Macaroon fruitcake jujubes danish candy canes lemon drops I love sesame snaps. </p> <p> Pastry cotton candy tiramisu bonbon powder gummi bears cotton candy. Biscuit jelly powder I love cheesecake pudding topping. Cheesecake lollipop dessert pastry marshmallow icing cotton candy halvah chocolate. Donut brownie lollipop jelly-o jujubes jelly-o. Liquorice pie cake toffee cake. Icing pastry pie gingerbread cupcake chocolate cake. I love cookie cake croissant marzipan jelly beans cupcake cheesecake marshmallow. Chocolate cake apple pie chupa chups chocolate cake candy canes biscuit I love cookie sweet. Cotton candy caramels topping sweet roll muffin. Gingerbread cake icing pastry pudding fruitcake lollipop halvah I love. Tiramisu tootsie roll macaroon liquorice. Toffee marzipan macaroon toffee jelly-o cupcake pastry gummies ice cream. Jelly candy canes sweet roll lollipop. </p> </div> 

Working fiddle here 在这里工作

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

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