Short Story
Let's say my HTML is already set in stone:
<div id="blockA">Block A</div>
<div id="blockB">Block B</div>
<div id="blockC">Block C</div>
It will look like this:
------------
| Block A |
------------
| Block B |
------------
| Block C |
------------
Now I want to switch the order of the blocks. How can I do that with only CSS?
------------
| Block C |
------------
| Block A |
------------
| Block B |
------------
I'm aware there's hacky solutions such as using position:absolute
, but this doesn't preserve the effective use of the display:block
property. That is, blocks push other blocks downward when they grow in size.
Long Story
When user uses a computer to view my webpage, the blocks are displayed in this order:
- General info.
- Event schedule.
- iPhone app advertisement
The iPhone app advertisement is placed last because it's not terribly important to computer users. A small percentage of computer users will whip out their phone and install the app.
If a mobile user comes to this site, the iPhone app advertisement should be the most important thing on the page. Therefore, it should be moved to the top:
- iPhone app advertisement
- General info.
- Event schedule.
I would like iPhone and computer users to share the same HTML, but have a CSS media query switch the order of the blocks.
@media only screen and (max-device-width: 480px) {
#blockC {
/* magic order switching */
}
}
position: absolute
doesn't changedisplay: block
. – Mediusposition:absolute
doesn't preserve the stacking and pushing nature ofdisplay:block
, which is the whole point of having a block element. – Titiandiv { display: flex; align-items: center; justify-content: center; flex-direction: row-reverse; //revert horizontally //flex-direction: column-reverse; revert vertically }
– Zachariah