0

I'm trying to build a simple table using box-sizing:border-box;float:left elements.

Warning 1. Maybe my approach is bad, even maybe there's no need to use "float" and "box-sizing" properties to complete this task, then what would you do in this situation? Warning 2. Of course, you should remember that you must use a modern CSS3-capable browser to view this markup :)

<!DOCTYPE html>
    <html>
        <head>
            <title></title>
            <meta charset="utf-8">
            <style>
                div {
                    vertical-align:top;
                    margin:0px;
                    padding:0px;
                }
                div.table {
                    border:solid 2px green;
                    width:90%;
                    background-color:red;
                }
                div.table > div:not(.clear) 
                {
                    -moz-box-sizing:border-box;
                    box-sizing:border-box;
                    float:left;
                    max-height:8em;
                    overflow:auto;
                    border:solid thin black;
                    background-color:white;
                }
                div.table > div:nth-child(3n+1):not(.clear) 
                {
                    clear:left;
                    width:40%;
                }
                div.table > div:nth-child(3n+2):not(.clear),div.table > div:nth-child(3n+3):not(.clear) {width:30%;}
                div.clear {clear:both;height:0px;border-style:none;}
            </style>
        </head>
        <body>
            <div class="table">
                <div>
                    Left first<br />Left first<br />Left first<br />Left first<br />Left first<br />Left first<br />Left first<br />Left first<br />Left first<br />Left first
                </div>
                <div>Middle first</div>
                <div>Right first</div>
                <div>Left second</div>
                <div>Middle second</div>
                <div>Right second</div>
                <div class="clear"></div>
            </div>
        </body>
    </html>

You see that red zone, it's showing that "Middle first" and "Right first" divs heights don't stretch to fit the element with the greatest height in the row. How to force them to automatically stretch its heights? I would prefer a pure CSS solution, but accept javascript (jquery) solution if it can deal with very, very large tables...

EDIT: Of course, using the floating elements is not an approach for my task, forget it. I don't like the implementation, but this may give a better understanding of what I want:

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<style>
div {vertical-align:top;margin:0px;padding:0px;}
div.table {display:inline-block;border:solid 2px green;background-color:red;}
div.table > div{display:inline-block;
max-height:8em;overflow:auto;
border:solid thin black;background-color:white;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script>
    $(document).ready(function () {
        $('<br />').insertAfter($("div.table > div:nth-child(3n+3)").not(':last'));
        $("div.table > div:nth-child(4n+1)").each(function () {
            $(this).css('width', '15em');
            if ($(this).height() < $(this).next('div').height() || $(this).height() < $(this).next('div').next('div').height()) {
                $(this).height(Math.max($(this).next('div').height(), $(this).next('div').next('div').height()));
            }
        });
        $("div.table > div:nth-child(4n+2)").each(function () {
            $(this).css('width', '10em');
            if ($(this).height() < $(this).prev('div').height() || $(this).height() < $(this).next('div').height()) {
                $(this).height(Math.max($(this).prev('div').height(), $(this).next('div').height()));
            }
        });
        $("div.table > div:nth-child(4n+3)").each(function () {
            $(this).css('width', '10em');
            if ($(this).height() < $(this).prev('div').height() || $(this).height() < $(this).prev('div').prev('div').height()) {
                $(this).height(Math.max($(this).prev('div').height(), $(this).prev('div').prev('div').height()));
            }
        });
    });
</script>
</head>
<body>
<div class="table">
<div>Left first<br />Left first<br />Left first<br />Left first<br />Left first<br />Left first<br />Left first<br />Left first<br />Left first<br />Left first</div><div>Middle first<br />Middle first<br />Middle first<br />Middle first<br />Middle first<br />Middle first<br />Middle first<br />Middle first<br />Middle first<br />longlonglonglonglonglonglonglonglonglong</div><div>Right first</div>
<div>Left second</div><div>Middle second Middle second Middle second<br />longlonglonglonglonglonglonglonglonglong</div><div>Right second</div>
</div>
</body>
</html>

Disadvantages: 1. Requires a bunch of ugly JS. 2. There are red zones which appear when scrollbars are present, or when you zoom the page. They differ in different browsers. Does anybody know where do they come from??? Is it possible to get rid of them? If no, I'll try to make use of something like equalHeights jquery plugin
EDIT 2:
I found a script equalizing heights of all the elements in a row, but I won't use it because now I realize that there is no script that may be applied to large table-like structure. CSS Flexible Box Layout Module is a solution, but it's currently not supported by the major browsers, and is being rendered much slower than normal tables.

lyrically wicked
  • 1,185
  • 12
  • 26
  • 8
    Why are you trying to re-invent the wheel? – boz Oct 23 '12 at 09:39
  • 1
    If you require a table use table… BW: I would say you will need an additional wrapper for each row, or at least some identification of the first item in each row. – feeela Oct 23 '12 at 09:42
  • table markup can't afford to add content in the source code of a page easily – lyrically wicked Oct 23 '12 at 11:01
  • What are you talking about, what wheel? If this "wheel" is already invented, then just show it – lyrically wicked Oct 24 '12 at 06:15
  • This wheel: `
    Content
    `
    – Jan Oct 25 '12 at 03:20
  • Yeah, now I must admit that tables is my faith, but I was just talking about the absence of scrolling mechanism, as said [here](http://stackoverflow.com/questions/5662357/css-a-table-cell-with-intern-scroll), [here](http://stackoverflow.com/questions/5602346/overflowscroll-in-a-table-cell-works-in-chrome-doesnt-work-anywhere-else), [here](http://www.htmlforums.com/archive/index.php/t-12557.html)... – lyrically wicked Oct 25 '12 at 03:41

1 Answers1

3

This is just a suggestion but not recommendation

Use CSS display: table, display: table-row and display: table-cell

Demo Page

Dipak
  • 11,930
  • 1
  • 30
  • 31
  • table-cells have no `overflow:auto` property. What should I do, if I need a cell in a row to be no more than, say, 8em? Placing a `max-height:8em;overflow:auto` div inside a cell? Is this the best? :( – lyrically wicked Oct 23 '12 at 10:51
  • @lyricallywicked I guess yes! Div inside a cell would be better – Dipak Oct 23 '12 at 11:57