user2385986
user2385986

Reputation: 31

Restricting text in two lines using css only

I want to limit the text to two lines inside a <div>

If the text is coming in more than two lines, then it should hide rest of the text after displaying two lines

For example:

Long text continues

down the road

into a lane.

***this should come as:

Long text continues

down the road

I have to do it using css only..

Please suggest?

Upvotes: 2

Views: 23484

Answers (2)

Nitesh
Nitesh

Reputation: 15739

Here is the Solution.

The HTML:

<div>The quick brown fox jumps over the lazy dog.</div>

The CSS:

div{background:gray; width:100px; height:40px; overflow:hidden;}

Hope this Helps.

Upvotes: 0

Richard Ev
Richard Ev

Reputation: 54087

How about fixing the height of your div to be exactly two lines high, and using overflow:hidden;?

For example:

<style type="text/css">
  .twolines
  {
     font-size: 20px;
     height: 48px;
     overflow: hidden;
  }
</style>

You could also use em values and line-height:

<style type="text/css">
  .twolines
  {
     font-size: 1em;
     line-height: 1em;
     height: 2em;
     overflow: hidden;
  }
</style>

Here's a complete, working example:

<!DOCTYPE html>

<html>
<head>
  <style type="text/css">
     .twolines
     {
        font-size: 1em;
        background-color: silver;
        line-height: 1em;
        height: 2em;
        overflow: hidden;
     }
     .twolines p
     {
        margin: 0;
     }
  </style>
</head>
<body>
  <div class="twolines">
     <p>Long text continues</p>
     <p>down the road</p>
     <p>into a lane.</p>
  </div>
</body>
</html>

Upvotes: 24

Related Questions