Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you make IE 9 (or earlier) lay out table elements as if they were regular display:block elements?

In WebKit, Firefox and Opera, you can set the various table elements to display: block to stop them displaying like tables:

  • http://jsfiddle.net/bHzsC/

This can be useful on smaller screens (e.g. iPhones) that don’t have room to display tables laid out traditionally.

IE 9, however, still lays out the table cells next to each other horizontally — it doesn’t seem to honour display: block on the table elements.

Is there any other code that will stop IE 9 (or earlier) from laying out tables as tables?

like image 926
Paul D. Waite Avatar asked Apr 03 '12 09:04

Paul D. Waite


People also ask

What does display block do in CSS?

display: block An element that has the display property set to block starts on a new line and takes up the available screen width. You can specify the width and height properties for such elements. Examples of elements that are at block-level by default are <div> , <section> , <p> , and lots more.

What is display block and display inline?

The display: inline-block Value Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.

What is display Define block inline inline-block and none?

As we can understand from its name, display: inline-block declaration shows both the characteristics of inline and block-level elements. In other words, we can think of an inline element, that width & height properties can be set, or we can think of a block-level element, that doesn't have to start with a new line.

What does display table do in CSS?

The display CSS property sets whether an element is treated as a block or inline element and the layout used for its children, such as flow layout, grid or flex. Formally, the display property sets an element's inner and outer display types.


1 Answers

Adding float:left;clear:left; will make IE 9 behave a bit better, but the width of each element will not be correct. If you add width:100% to the mix, it seems to behave the same as in Chrome and Firefox.

table,
thead,
tfoot,
tbody,
tr,
th,
td {
    display:block;
    width:100%;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    float:left;
    clear:left;
}

Edit: This has been asked before How can I make "display: block" work on a <td> in IE? and partially covered on How can I get inline-block to render consistently when applied to table cells? which quite rightly mention that any padding will cause the width:100% to create a horizontal scrollbar. However, this can be avoided with box-sizing:border-box;, or by using a suitably lower width or containing element with a fixed width.

like image 163
andyb Avatar answered Sep 30 '22 04:09

andyb