tags:

views:

11

answers:

2

If I have a div that expands to its contents, how can I ensure that its height is always a multiple of a given value, say, 50 pixels? Is there any way to do this with CSS, or would I need to resort to JavaScript?

+2  A: 

Since CSS is not aware of the actual computed element dimensions, it cannot achieve what you ask. JavaScript can do this easily.

mingos
+1  A: 

You'll need JavaScript. In case the content doesn't change inline (with ajax or jquery or anything) you can use an onload event to start a script that checks the height and fixes it:

<body onload="javascript:fixHeight()">
  <div id="thediv"> 
    content
  </div>
</body>

script:

function fixHeight() {
    var thediv = document.getElementById('thediv');
    thediv.style.height = ceil(thediv.offsetHeight / 50) * 50 + 'px';
}  

(not tested)

Litso