tags:

views:

44

answers:

3

We have a fixed size div in which we are displaying some news items. We display 6 news items, and then a link that follows, which links them to archives. When the news article titles are all one line in length, everything fits. When multiple news items have titles that are two lines in height, the group of items takes up more space that we have.

Using jQuery, CSS, and HTML, how can we cut off or not display news items which cause the group to not fit in the div? Scroll bars is not an option. The height and width of the div is constant and known.

The structure of the news items is simply a list of list items.

+1  A: 

You can set CSS on your div to hide the extra data:

<div style="overflow: hidden;">

Or char limit your news item titles in your scripts. In PHP this would be:

<?php echo strlen($article['title']) > 60 ? substr($article['title'], 0, 57) . '...' : $article['title'] ?>

That would cut off any article titles longer than 60 characters at the 57th character and add '...' to show continuation.

Hope that helps.

Cryo
the first suggestion would not work because you would not want the last item to be chopped in half if part of it is within the div limit, and part is beyond the div limit. The second suggestion is good, however it presumes that the font, font size are constant as well. I'm hoping S.O. can come up with something slightly more robust (but still is an interesting idea)
MedicineMan
check out this question, you can detect the overflow condition and add the elipses: http://stackoverflow.com/questions/2059743/detect-elements-overflow-using-jquery
Chris McCall
@Chris McCall You added that as a comment beneath my answer, not the question. This notifies me but not the OP. I'll repost as a comment under the main question but you should post that as an answer instead of a comment. If you do post it let me know and I'll delete my comment.
Cryo
+2  A: 

I'd have to look up the exact code, but it is entirely possible with jQuery.

Something like this perhaps?:

var container = $("#container");
var list = $("#list");

var contHeight = container.height();
var listHeight = list.height();

while (ulHeight > contHeight) {
  ul.children().last().hide();
  var ulHeight = ul.height();
}

Haven't tested it, but that's the idea. Or you could step through each child adding the heights together and then hide all the ones over your limit.

Eric Meyer
I will have to come back and test this answer when I get a chance. This response looks very promising.
MedicineMan
A: 

First you would need to grab the fixed height of your div. Then loop over the items in the div and figure out when they start poking out of the div and hide them.

Example: http://jsfiddle.net/pqsEG/1/

PetersenDidIt