Consider the following bog-standard paragraphs-with-sidebar layout. The layout has two issues: first, the sidebar (which will be navigation stuff in my case) will be seen first by the browser, resulting in some accessibility issues with screen readers and text-only browsers. So the element should be after the article. But in that case I can't rely on the tried & true float:right
mechanism; all absolute positioning will lead to problems. The second issue relates to the first one; with narrow viewports I don't want it to float next to the text and want it placed after.
How can I achieve the same look but with the two elements #main
and #sidebar
swapped places?
#main {
width: 100%;
}
#sidebar {
width: 150px;
height: 200px;
background-color: #abc;
float: right;
}
<div id="sidebar"></div>
<div id="main">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
If you can use the FlexBox, you can use order
to swap it left and right.
I have done it for you:
.wrapper {
display: flex;
}
#main {
width: 85%;
}
#sidebar {
width: 150px;
background-color: #abc;
order: 1;
}
<div class="wrapper">
<div id="main">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content
here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
<div id="sidebar"></div>
</div>
Preview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With