Why CSS tables won't help in this case
Assuming two options to position the elements margin
and position: relative
, neither works on table cell elements. (margin is not applicable to table cells and also the effect of relative positioning is undefined for them).
8.3 Margin properties 'margin'
Applies to: all elements except elements with table display types
other than table-caption, table and inline-table
9.3.1 Choosing a positioning scheme: 'position' property
The effect of position:relative
on table-row-group
,
table-header-group
, table-footer-group
, table-row
,
table-column-group
, table-column
, table-cell
, and
table-caption
elements is undefined.
Therefore you probably need to use another layout scheme.
Choosing a layout method
There are two other options to create the columns, CSS floats and inline-blocks (Actually there are three. The third one, CSS Flexible box layout is only supported in modern web browsers at the time of writing; Hence I ignored that).
Since the contents should be aligned vertically at the middle, I'd go with using inline-block
columns.
Important Note: You should remove the white-space between inline-block elements (the columns in this case) in order to prevent the layout from being broken (by two 50% columns).
Column ordering
In order to push and/or pull columns to a side, you could either use a positive/negative margin
value or use relative positioning and specify left
/right
properties.
For instance, by using margin
you'll have:
EXAMPLE HERE
.col {
width: 50%;
display: inline-block;
padding: 0 10px;
vertical-align: middle;
-webkit-box-sizing: border-box; /* to calculate width of the box including */
-moz-box-sizing: border-box; /* borders and padding (10px left/right) */
box-sizing: border-box;
}
.col.push-right { margin-left: 50%; }
.col.push-left { margin-left: -100%; }
@media only screen and (max-width : 640px) {
.col {
display: block;
width: auto;
text-align: center;
padding: 10px;
}
.col.push-right, .col.push-left {
margin-left: auto;
}
}
Or by using relative positioning you'll end up with:
EXAMPLE HERE
.col {
/* other declarations as above... */
width: 50%;
position: relative;
}
.col.push-right { left: 50%; }
.col.push-left { right: 50%; }
@media only screen and (max-width : 640px) {
.col {
/* other declarations... */
display: block;
width: auto;
}
.col.push-right { left: auto; }
.col.push-left { right: auto; }
}
Regardless of inline-block
columns, this approach is widely used by most CSS frameworks such as Twitter Bootstrap and Zurb Foundation.
Additionally in both of the above approaches, your HTML markup should be changed like so:
<div class="container">
<div class="col push-right">
<p>Lorem ipsum....</p>
</div><!-- comment out spaces/new lines/tabs between
these two inline-block columns in order to remove the gap.
--><div class="col push-left">
<div id="box"></div>
</div>
</div>