img with {display: table-cell} — is it a bug?

前端 未结 2 1867
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 06:21

I want to use the display: table-* CSS properties to format a list of photos. I believe that below is a \"correct\" implementation of it, in that there\'s nothi

相关标签:
2条回答
  • 2020-12-21 07:04

    The problem seems to be mostly due to the border-collapse. If you remove that, the alignment problem goes away. I can't seem to find any other discussion of this problem online but I have noticed bugs in the border-collapse: collapse algorithm many times in Firefox and Safari (lines that disappear/reappear as you scroll, etc). This appears to be just another bug in that algorithm.

    You're are right however, that it is specific to the image, if you wrap the images in divs, the problem goes away:

    <html>
    <head>
        <style>
            .photos {display: table; border-collapse: collapse;}
            .photos > div {display: table-row; border-collapse: collapse;}
            .photos > div > * {
                border-collapse: collapse;
                display: table-cell;
                vertical-align: top;
                border: 1px solid #000;
                padding: 10px;
            }
        </style>
    </head>
    <body>
        <div class="photos">
            <div>
                <p>Hello World</p>
                <div><img src="http://www.studentsoftheworld.info/sites/music/img/23448_shopping_bags1.gif" /></div>
                <p>Hello World</p>
            </div>
            <div>
                <p>Hello World</p>
                <div><img src="http://www.studentsoftheworld.info/sites/music/img/23448_shopping_bags1.gif" /></div>
                <p>Hello World</p>
            </div>
        </div>
    </body>
    </html>
    

    I've tested this in Firefox 3.1 in Ubuntu and XP, Firefox 3.5 in XP, Safari 4 in Wine and XP, and Chrome 3 in XP and they ALL exhibit errors in rendering the border-collapse. Only Firefox shows the image table-cell as one pixel low.

    Opera 9.52 in XP oddly does not display the image at all. Opera 10.10 in XP behaves like the rest.

    Perhaps there's something about the spec that causes so many browsers to interpret this way.

    0 讨论(0)
  • 2020-12-21 07:11

    I see what you meant when I render the codes in FF. My initial thought was the doctype but it doesn't help with loose, transitional or strict.

    Going through a W3C reference at http://www.w3.org/TR/CSS2/tables.html#table-display
    Here's an excerpt:

    For example, an image that is set to 'display: table-cell' will fill the available cell space, and its dimensions might contribute towards the table sizing algorithms, as with an ordinary cell.

    Seems to me that non-container elements will just fill the 'cell space' but not acting like a true cells. Which explains the border 'bug'.

    I am hesistant to think the FF/Webkit-based browsers are rendering it wrongly but IE is correct. Perhaps someone can prove otherwise. :P

    My 2 cents.

    0 讨论(0)
提交回复
热议问题