As the question says how to limit a div's height to one/two/n lines of the text inside it?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With