Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I vertically align a <table> in the middle of a fixed height <div>?

Why does the code below not cause the <table> to be vertically-aligned in the middle of the <div>?

<div style="width: 850px; height: 470px;vertical-align: middle;" align="center">
        <table style="padding-left: 20px; width: 700px; border: 10px groove #0033CC; background-color: #F9F9F9;">
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
        </table>
    </div>

I want the <table> in the middle of the <div>, but it is at the top! How can I fix this?

Thanks for your future advice.

like image 908
LostLord Avatar asked Oct 31 '10 19:10

LostLord


People also ask

How do I make div content vertical-align middle?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do you vertically align a table?

To place an item at the top or bottom of its cell, insert the "VALIGN=" attribute within the code for that cell. To vertically align an entire row (e.g., placing all data in that row at the tops of the cells), insert the "VALIGN=" attribute within the code for that row.

How do I center a table in a div?

If you want to center the table by using text-align on the outward <div>, you can do it by using text-align. You can also style the cells of the table with a text-align or vertical-align value whenever you want to position the inline text in a specific way.


2 Answers

Outside of table cells, vertical-align sets the vertical alignment of text within a line, rather than the vertical alignment of entire elements like your table.

However, if you set display: table-cell; on your <div>, that seems to achieve the effect you want.

I’m not sure how many browsers support this though. I’ve checked in Chrome 6, Firefox 2 and Opera 10.5, and they’re fine with it. Internet Explorer could be a different matter.

like image 144
Paul D. Waite Avatar answered Oct 05 '22 08:10

Paul D. Waite


Have you tried "display:flex;"?

div{
    width: 850px;
    height: 470px;
    
    display: flex;
    /* WIDTH and HEIGHT are required */
    justify-content: center;
    align-items: center;
}
td, table{
    border-collapse: collapse;
    border: 1px solid black;
}
<div>
    <table>
        <tr>
          <td>Lorem</td>
          <td>Lorem</td>
          <td>Lorem</td>
        </tr>
        <tr>
          <td>2019</td>
          <td>2018</td>
          <td>2017</td>
        </tr>
    </table>
</div>
like image 30
diogo-b013 Avatar answered Oct 05 '22 10:10

diogo-b013