Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set element height for a fixed number of lines of text

Tags:

css

Given some text that occupies about 10 rows, how can I resize its container to only show the first 3 rows and hide the others? Apparently this works, but I think it is not reliable:

.container {     height: 7.5ex; /* 2.5ex for each visible line */     overflow: hidden; } 

Can I rely on the fact that height of one row = 2.5ex or is that just a coincidence in the browsers I am using to test?

like image 351
Tom Avatar asked Sep 09 '10 10:09

Tom


People also ask

How do you set the height in CSS to be depending on the content?

Syntax: height: length|percentage|auto|initial|inherit; Property Values: height: auto; It is used to set height property to its default value.

How do I set auto height in HTML?

If height: auto; the element will automatically adjust its height to allow its content to be displayed correctly. If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow.

What does line height 1.5 mean?

line-height: 1.5 (without units) will mutiply the element's font size by 1.5 to compute the line height. line-height: 150% will take 150% of the element's computed font size to compute the line height, which is equivalent to multiply it by 1.5 .


1 Answers

If you are going to use this you should ensure the line-height is always 2.5ex

.container {   line-height: 2.5ex;   height: 7.5ex; /* 2.5ex for each visible line */   overflow: hidden; } 

Demo

like image 190
irishbuzz Avatar answered Sep 25 '22 00:09

irishbuzz