tags:

views:

45

answers:

4

I have a DIV with a multi-line paragraph.

Is there any way (maybe using jQuery) to only show the first line of the paragraph and hide the others?

A: 

There is not an outright way to do this by specifying the first line. I would suggest changing the height of the DIV using CSS to only show the first line. It would seem to me to be the simplest solution. If you then want to change to show the whole line with javascript just use it to change the height of the DIV back to 100%.

EDIT: I stand corrected, I was not aware that there was a first-line pseudo class. However changing the height may still be the simplest way.

Ben
did not know that either.
RPM1984
Thanks Ben - it worked!
Victor P
@Victor P Glad to help!
Ben
+2  A: 

Here is a way (sort of) by making the paragraph white-on-white except the :first-line of it: http://jsbin.com/usora4/2/edit

Snippet of CSS:

p { color: white; }
p:first-line { color: black; }
Chris Coyier
yeah this would work, apart from spacing. the p would'nt auto-size, because the contents are still there. so it's hiding the text, but no the actual 'line' (where 'line' refers to a block of space in a paragraph). still, +1. :)
RPM1984
Good, simple idea. But it still uses the same space as the whole paragraph.
Victor P
A: 

Determine the height of your line and set the height of the div such that only one line is shown, and set the div's overflow attribute to hidden.

Chadwick
A: 

I haven't tried it, but yuou should be able to do a little better using visibility, eg

<div class="excerpt">
   This is my text.  It has many lines. This is my text.  
   It has many lines.This is my text.  It has many lines. 
   This is my text.  It has many lines. This is my text.  
   It has many lines.
</div>

and then setting up a style

div.excerpt { visibility: hidden; }
div.excert:first-line { visibility: visible; {
Charlie Martin
It doesn't work. I've tried both visibility and display and couldn't get it to work. Funnily enough, it worked for color. But again it only worked when I've changed from div to p, and introduced a <br> in between the lines...
Wagner Silveira