简体   繁体   English

选择最后一行 <p> 元件

[英]Selecting the last line of a <p> element

I am aware of the :first-line selector in CSS, but I cannot find anything that will allow me to select the last line. 我知道CSS中的:first-line选择器,但是找不到任何可以选择最后一行的东西。

The reason I am trying to select the last line is because I want to be able to define the text-overflow and white-space attributes of the last line. 我尝试选择最后一行的原因是因为我希望能够定义最后一行的text-overflowwhite-space属性。

I am trying to avoid using JavaScript to form a solution. 我试图避免使用JavaScript形成解决方案。

Unfortunately there is no CSS selector that would apply. 不幸的是,没有适用的CSS选择器。

You can however select the first line using the pseudo element :first-line . 但是,您可以使用伪元素:first-line选择:first-line Unfortunately at the moment there is no equivalent for the last line. 不幸的是,目前没有最后一行的等效项。

I don't think this can be done with CSS alone. 我不认为仅凭CSS就能做到这一点。 jQuery or a PHP function might be able to get want you what jQuery或PHP函数可能会得到想要的东西

IIRC, the only css control over the last line is text-align: IIRC,最后一行的唯一CSS控制是text-align:

text-align-last: right;

I'm afraid javascript, or actually wrapping the last line in a span and styling that, is the only way. 恐怕javascript或实际上将最后一行包裹在span中并设置其样式是唯一的方法。 Below is the JS for reference. 下面是JS供参考。

var thetext = document.getElementById('some_element').innerHTML.split(/\r?\n/);
alert(thetext[thetext.length - 1]);

You can use only one line and give css : nth-child(3) 您只能使用一行并给css: nth-child(3)

Here (3) denotes the number of a last line 这里的(3)表示最后一行的编号

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

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