Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting a div height to two lines of text inside it?

Tags:

html

css

As the question says how to limit a div's height to one/two/n lines of the text inside it?

like image 627
Flood Gravemind Avatar asked Jun 21 '13 18:06

Flood Gravemind


People also ask

How do I make a div with two lines?

How to make a DIV span the 2 rows of the grid with the help of CSS. Approach 1: First get the height of the outer DIV of ID('outer'). We know the height of the outer element now design can be achieved using CSS Flexbox with flex-direction: column and flex-wrap: wrap.

How do I put two lines of text in HTML?

To create a multi-line text input, use the HTML <textarea> tag. You can set the size of a text area using the cols and rows attributes. It is used within a form, to allow users to input text over multiple rows.

How do you break a text with two lines?

We use the word–break property in CSS that is used to specify how a word should be broken or split when reaching the end of a line. The word–wrap property is used to split/break long words and wrap them into the next line.


2 Answers

div {     height: 1em; // that's one line, 2em for 2 lines, etc...     line-height: 1em; // the height of one text line     overflow: hidden; } 

This will display a div with a height the size of the current font size, and any overflow is clipped. As long as line-height and height are equal there will be one line of text. The proportion of height/line-height determines the number of lines displayed.

like image 131
Daniel Gimenez Avatar answered Sep 20 '22 14:09

Daniel Gimenez


The em unit adjusts by font-size, and line-height proportions are also based on that. So those are what you want to be using for "fixing" height.

You want either this:

div {     height: 2.2em;     line-height: 1.1;     overflow: auto; } 

Example Fiddle.

Or if you want it to potentially reduce to 1 line, then use this:

div {     max-height: 1.1em;     line-height: 1.1;     overflow: auto; } 

Example Fiddle.

like image 42
ScottS Avatar answered Sep 22 '22 14:09

ScottS