Display:Block not working in Chrome or Safari
Asked Answered
C

4

23

I have a simple need to display the cells of a table row vertically. This works just fine in FF, but not in Chrome or Safari on the Ipad.

The example below renders as expected in FF, with each row cell under each other, but in Chrome, it seems to ignore the display:block altogether.

What is the issue - or is there a better way to do this.

(The reason for wanting this is that im using @media in the CSS to render the table differently for a small screen)

for a more visual example:

A normal table might be

DATA1 | DATA2 | DATA3

but with display:block, it should be

DATA1
DATA2
DATA3

Many Thanks

<html>
<head>
<style>
    table,
    thead,
    tr,
    td
    {
        display:block;
    }
    table,tr
    {
        border : 1px dotted red;
    }
    td
    {
        border:1px dashed blue;
    }
    thead
    {
        display:none;
    }
</style>
</head>
<body>

<table>
<thead>
<tr>
    <td>Heading 1</td>
    <td>Heading 2</td>
    <td>Heading 3</td>
</tr>
</thead>
<tbody>
<tr>
    <td>Data 1</td>
    <td>Data 2</td>
    <td>Data 3</td>
</tr>
<tr>
    <td>Data 1</td>
    <td>Data 2</td>
    <td>Data 3</td>
</tr>
<tr>
    <td>Data 1</td>
    <td>Data 2</td>
    <td>Data 3</td>
</tr>

</tbody>
</table>

</body>
</html>
Crowbar answered 11/5, 2012 at 17:55 Comment(1)
Why are you making tables more complicated than they are? If you want to display a single column table, then just make a single column table; there's no need to define them with display: block;.Estrada
Q
72

I think I have worked out your problem. Webkit overrides display: block; and computes it to be display: table-cell; in a td when there is no <!DOCTYPE> declared for your html.

To fix this I recommend you set <!DOCTYPE html> before <html> at the top of your html.

The reason the jsfiddle will work is because the site has a <!DOCTYPE> already declared.

Quinacrine answered 11/5, 2012 at 18:13 Comment(2)
I am having this problem even though the website does have <!DOCTYPE html> before the <html> tag. This problem is only in Safari.Caveman
It is important to note that <!DOCTYPE html> must be the very first content on your page, not just above your <html>. I had an issue like this where my PHP was injecting some javascript above my doctype tag and caused a similar problem to this.Glutathione
R
1

I've did a trick with using th's instead of td. And a css hack wich only applies on safari (not webkit general).

HTML:

<table>
    <tr>
        <th>Cell 1</th> 
        <th>Cell 2</th>
    </tr>
</table>

CSS:

table {
    width: 100%;
    text-align: left;
}

/* Safari 7.1+ */
_::-webkit-full-page-media, _:future, :root .safari_only {
    .safari-fix table tr {
        display: block;
        width: 100%;
    }
}
/* Safari 10.1+ */
@media not all and (min-resolution:.001dpcm) { 
    @media {
        .safari-fix table tr {
            display: block;
            width: 100%;
        }
    }
}

table th {
    width: 100%;
    display: block;
}

Here is my jsfiddle

Roundhead answered 11/1, 2018 at 11:32 Comment(0)
C
1

Same issue, but fixed with display:grid, instead of display:block;

table, thead, tbody, th, td, tr { 
    display: grid; 
    width: 100%;
}
Carbonization answered 7/8, 2022 at 7:54 Comment(0)
G
0

Table broken in Chrome or Safari

Many case occurs in tabless.

display:block;  
display:"";

or

display:block;
display:table-row; 
*display:block;
Groff answered 4/3, 2013 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.