I have a series of rows, each containing two columns, split 50/50 in width.
I'd like every other row to swap the left column (.image
) to the right right, but I need to maintain the sequence in the HTML as it's displayed as one column on smaller screens.
CSS:
ul {
list-style: none;
padding-left: 0;
}
.row {
text-align: center;
}
@media (min-width: 768px) {
.image,
.text {
display: inline-block;
width: 50%;
vertical-align: middle;
}
/* alternate */
.row:nth-child(odd) .image {
float: right;
}
/* clearfix */
.row:before,
.row:after {
content: " ";
display: table;
}
.row:after {
clear: both;
}
}
HTML:
<ul>
<li class="row">
<div class="image">
<img src="http://placehold.it/350x150">
</div><div class="text">
<p>Lorem ipsum</p>
</div>
</li>
<li class="row">
<div class="image">
<img src="http://placehold.it/350x150">
</div><div class="text">
<p>Lorem ipsum</p>
</div>
</li>
<li class="row">
<div class="image">
<img src="http://placehold.it/350x150">
</div><div class="text">
<p>Lorem ipsum</p>
</div>
</li>
</ul>
I know I can do this with flexbox, which I can't use in this project. And using float: left
doesn't allow me to vertically align the elements. What other options do I have?
NB: I'm using display: inline-block
for these columns, as they vary in height and I'd like them to be aligned vertically to one another.
Edit: I've made a CodePen with the example using floats as shown above.
you may use
display:table
(optionnal),:nth-child(even)
anddirection
to swap div position : codepenTry this:
Here is a working plunker
I don't really understand what you mean by "aligned vertically to one another". How exactly aligned?
But maybe you want to align them vertically using
vertical-align: middle
and then, yes,float: left
is not your friend.You say that your columns split 50/50 in width. Then you can use
position: relative
withleft: 50%;
andright: 50%;
rules.Like this:
The background color is just for demonstration.
This way each even row will swap columns and you can still use
vertical-align
rule. See the plunker.