Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a table-cell aligned to the right?

I have a table with two rows.
Looks something like this:

—————————————————
|   1   |   2   |
|———————————————|
|   3   |       |
—————————————————

The first row has two cells, but the second has only one.

How can I get the only table-cell in the second row, to align under the the second table-cell in the first row? (without adding a cell before the cell in the second row)
Like this:

—————————————————
|   1   |   2   |
|———————————————|
|       |   3   |
—————————————————

Here's my HTML:

<div class="table">
    <div class="row">
        <div class="cell one">
            1
        </div>
        <div class="cell two">
            2
        </div>
    </div>
    <div class="row">
        <div class="cell three">
            3
        </div>
    </div>
 </div>

And my CSS:

div.table {
    display:table;
    background:#EEE;
    padding:10px;
}
 div.row {
    display:table-row;
    background:#610;
    padding:10px;
    width:100%;
    text-align:right;
}
 div.cell {
    display:table-cell;
    background:#016;
    padding:10px;
    color:white;
    font-family:sans-serif;
}

jsFiddle.

I've tried margins & floats. I would like to avoid absolute and floats if possible though.

like image 337
henryaaron Avatar asked Nov 24 '13 01:11

henryaaron


People also ask

How do you right align a cell in a table?

To change the alignment, simply click inside that cell and click Align Right in the Paragraph group on the Home tab.

How will you align a table to the right or left?

To allow text to flow around the table, use ALIGN=LEFT to position the table at the left margin, with text flowing around its right handside, or use ALIGN=RIGHT to position the table at the right margin, with text flowing around its left handside.

How do you right align a table in Word?

Right-click anywhere inside the table and then choose the “Table Properties” command from the context menu that appears. In the Table Properties window that opens, you can choose left, center, or right alignment by clicking those options in the “Alignment” section.


1 Answers

If the only reason you don't want an extra cell because you don't have control of the HTML, you could do this:

.row + .row:before {
    content: '';
    display:table-cell;
    background-color:white;
}

http://jsfiddle.net/7H7rf/6/

like image 167
Alohci Avatar answered Sep 30 '22 18:09

Alohci