Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict <p> to one line and ellipsize when necessary?

Tags:

css

I have this:

<ul>
  <li>
    <p>Some long text here</p>
  </li>
</ul>

is there a way to get the p text to be restricted to a single line, and have it ellipsize if it's going to be wider than the parent container?

Thanks

like image 490
user246114 Avatar asked Sep 01 '10 12:09

user246114


People also ask

How do I limit text to one line in HTML?

element (white-space: nowrap; overflow: scroll;) Text parsed as HTML or XML is tokenized, which means all runs of whitespace are collapsed to a single space character. This means you will have a single line of text unless you specify something in addition to the overflow declaration that could cause your text to wrap.

How do you skip a line in P tag?

To add a line break to your HTML code, you use the <br> tag. The <br> tag does not have an end tag. You can also add additional lines between paragraphs by using the <br> tags. Each <br> tag you enter creates another blank line.


2 Answers

You can restrict the text to one line using CSS.

<p style="white-space: nowrap;"></p>

EDIT: According to quirksmode there's apparently a text-overflow: ellipsis. I've never used this and don't really know anything about it, but you should look into it.

like image 95
DLH Avatar answered Sep 27 '22 23:09

DLH


CSS has white-space: nowrap (gives you single line) and text-overflow: ellipsis which should do exactly what you want. Unfortunately, text-overflow: ellipsis isn't supported by all browsers (Firefox being the exception in this case).

Devon Govett wrote a ellipsis plugin for jQuery which solves that.

Original blog: Text-overflow: ellipsis for Firefox via jQuery via Wayback Machine since the original blog was deleted.

like image 29
Aaron Digulla Avatar answered Sep 28 '22 00:09

Aaron Digulla