user3713915
user3713915

Reputation: 53

td:before and vertical-align:middle

I use this construction to get table with square cells:

table {
    width: 100%;
    table-layout: fixed;
}
td {
    text-align: center;
    vertical-align: middle;
}
td:before {
    content: '';
    padding-top: 100%;
    float: left;
}

But vertical-align doesn't work for cell's content. How can I fix that?

html is:

<table>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>4</td>
        <td>5</td>
        <td>6</td>
    </tr>
    <tr>
        <td>7</td>
        <td>8</td>
        <td>9</td>
    </tr>
</table>

http://jsfiddle.net/FU84y/1/

Upvotes: 5

Views: 1447

Answers (3)

Alex Wilson
Alex Wilson

Reputation: 2419

you need remove padding-top: 100%; to td:before DEMO

HTML

<table>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>4</td>
        <td>5</td>
        <td>6</td>
    </tr>
    <tr>
        <td>7</td>
        <td>8</td>
        <td>9</td>
    </tr>
</table>

CSS

table {
    width: 100%;
    table-layout: fixed;
}
td {
    text-align: center;
  border: 1px solid #ececec;
  height: 50px;
}
td:before {
    content: '';
    float: left;
}

Upvotes: 1

Alohci
Alohci

Reputation: 82986

Replace

float: left;

with

display: inline-block;
vertical-align: middle;

See http://jsfiddle.net/8jPT5/2/

Upvotes: 4

punkologist
punkologist

Reputation: 761

You dont need to remove the padding, You need to give the table cell a height.

table {
width: 100%;
table-layout: fixed;
}
td {
text-align: center;
vertical-align: middle;
height: 40px;

}
td:before {
content: '';
float: left;
padding:100%;
}

see the fiddle example

Upvotes: 1

Related Questions