Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

I am trying to avoid using JavaScript to form a solution.

like image 928
Alex Avatar asked Feb 10 '12 12:02

Alex


People also ask

How do you select the last line in CSS?

The text-align-last property in CSS is used to set the last line of the paragraph just before the line break. The line break may be due to the natural ending of a paragraph, or it may be due to the use of <br> tag.

How do you align the last line?

The text-align-last property specifies how to align the last line of a text. Notice that the text-align-last property sets the alignment for all last lines within the selected element. So, if you have a <div> with three paragraphs in it, text-align-last will apply to the last line of EACH of the paragraphs.

What is the last line in an HTML document?

At the end of your document you should close with the </html> tag.

What is text-align-last?

The text-align-last CSS property sets how the last line of a block or a line, right before a forced line break, is aligned.


2 Answers

Unfortunately there is no CSS selector that would apply.

You can however select the first line using the pseudo element :first-line. Unfortunately at the moment there is no equivalent for the last line.

like image 165
MMM Avatar answered Sep 28 '22 10:09

MMM


IIRC, the only css control over the last line is 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. Below is the JS for reference.

var thetext = document.getElementById('some_element').innerHTML.split(/\r?\n/);
alert(thetext[thetext.length - 1]);
like image 24
rickyduck Avatar answered Sep 28 '22 09:09

rickyduck