最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to limit a <td> to only three lines? - Stack Overflow

programmeradmin0浏览0评论

I would like to achieve a unified look and feel across all my table rows. When you look at my example below you can see that the note in the middle goes over 4 lines and thats not so pretty.

I was hoping to limit all <td> to 3 lines.

If there is more to be shown than three lines, then it should cut the content with ... [click for more] and put the content inside a collapseable element, so that when clicked on it it would show the whole content.

The latter shouldn't be a problem, but how do I limit the content to only three lines? Shall I count the characters to make the decision upon that? Is there a better strategy? I am using Django by the way,but I am happy to use javascript, jquery or any css magic instead to solve this.

Update:

The accepted answer is very good. However it es with a caveat, which isn't easy to solve. if you have a neighbouring td that already goes over three lines, while the current td is only two lines we will get an infinite while loop.

while($(this).innerHeight() / $(this).css('line-height').slice(0,-2) >= 3){ .. }

Since $(this).innerHeight() can't decrease because of the neighbouring cell holding the height up high. I think if it was possible to get the css of the current td and copy it across the content pletely in a separate field, where neighbouring tds can't interfere, we would get the optimal solution.

Update 2:

As Assad mentioned, the solution is to put a div wrapper around the content of td and set the class on the div inside the td rather than on the td itself. It works flawlessly.

I would like to achieve a unified look and feel across all my table rows. When you look at my example below you can see that the note in the middle goes over 4 lines and thats not so pretty.

I was hoping to limit all <td> to 3 lines.

If there is more to be shown than three lines, then it should cut the content with ... [click for more] and put the content inside a collapseable element, so that when clicked on it it would show the whole content.

The latter shouldn't be a problem, but how do I limit the content to only three lines? Shall I count the characters to make the decision upon that? Is there a better strategy? I am using Django by the way,but I am happy to use javascript, jquery or any css magic instead to solve this.

Update:

The accepted answer is very good. However it es with a caveat, which isn't easy to solve. if you have a neighbouring td that already goes over three lines, while the current td is only two lines we will get an infinite while loop.

while($(this).innerHeight() / $(this).css('line-height').slice(0,-2) >= 3){ .. }

Since $(this).innerHeight() can't decrease because of the neighbouring cell holding the height up high. I think if it was possible to get the css of the current td and copy it across the content pletely in a separate field, where neighbouring tds can't interfere, we would get the optimal solution.

Update 2:

As Assad mentioned, the solution is to put a div wrapper around the content of td and set the class on the div inside the td rather than on the td itself. It works flawlessly.

Share Improve this question edited Dec 3, 2012 at 16:05 Houman asked Dec 3, 2012 at 10:45 HoumanHouman 66.4k97 gold badges294 silver badges481 bronze badges 4
  • 1 See stackoverflow./questions/3922739/… – Simone Commented Dec 3, 2012 at 10:48
  • Counting the characters wont work since you are not using a fixed with font. The "i" is much thinner then the "W". Less characters might easily go over your 3 line requirement. – Damien Overeem Commented Dec 3, 2012 at 10:49
  • stackoverflow./questions/261368/… Take a look. – Harsha Venkataramu Commented Dec 3, 2012 at 10:49
  • Even if you were using a monospace font, the breakpoints (spaces/hyphens) would ultimately determine the the number of characters that fit in each line. – user1726343 Commented Dec 3, 2012 at 10:51
Add a ment  | 

2 Answers 2

Reset to default 5

Assuming you are using jQuery, you could find all td elements that exceed a certain number of lines using:

$('td').filter(function(){
    return $(this).innerHeight() / $(this).css('line-height').slice(0,-2) > 3; //more than 3 lines
});

You could then apply collapsible elements to these td elements.

Here is a demonstration (using paragraphs instead of tds): http://jsfiddle/jM4ZY/1/

Here is an example of cutting off content to fit 3 lines, then adding a more button: http://jsfiddle/jM4ZY/2/

As far as the edit is concerned, this is easily resolved by using an inner wrapper for your content; possibly a div element. You can then measure the height of this element, which is independent of the height of neighboring cells.

Another jQuery solution is described here

It is described how to change the Text by counting the number of letters in the displayed text. If you are unsure about the number of letters, or want to make it dependent of the text-length you can calculate it by using this snipped

$.fn.textWidth = function(){
  var html_org = $(this).html();
  var html_calc = '<span>' + html_org + '</span>';
  $(this).html(html_calc);
  var width = $(this).find('span:first').width();
  $(this).html(html_org);
  return width;
};

which I took from Calculating text width

发布评论

评论列表(0)

  1. 暂无评论