Jump to content

shortening a div to 1 line


Michdd

Recommended Posts

Yes, you need a set height of about 1em, then set the overflow to hidden. Be sure that the line-height is correct as well.

 

Use div:hover { height: auto; } to show the content again.

 

I.e

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

  <head>
    <title>My first Website</title>
    <style type="text/css">
      * { margin: 0;padding: 0; }
      body {
        padding: 1em;
      }
      div {
        height: 1em;
        overflow: hidden;
      }
      div:hover {
        height: auto;
      }
      p {
        padding: 0 0 1em;
        line-height: 1em;
      }
    </style>
  </head>

  <body>
    <div>
     <p>My first Website.</p>
     <p>My first Website.</p>
     <p>My first Website.</p>
     <p>My first Website.</p>
     <p>My first Website.</p>
    </div>
  </body>

</html>

 

See also: CSS Selectors

As a side point, you should rethink your logic a little on this. If the user has javascript turned off, they will never be able to see more than that first line. So what you should do is leave it as a normal div, then use javascript to change the CSS of the div to what blue boden suggested. Although to be even more efficient, you are best creating two different CSS classes - one for each state - and using javscript to toggle between the classes. It's more efficient this way, and your javascript will run smoother.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.