Place a Left Positioned Sidebar Below the Main Content on Mobile
Most responsive layouts have multiple columns at full-size, which then collapse on mobile devices to accommodate the smaller display size. Unless you have unusual coding practices, the collapsed columns will always be automatically stacked in the order they appear in the source code. This means that columns on the left side of the screen at full size, will be at the top of the stack on mobile and vice versa.
<div class="wrap"> <div id="main-content"><p>Main Content</p></div> <div id="sidebar-left"><p>Left Sidebar</p></div> <div id="sidebar-right"><p>Right Sidebar</p></div> </div>CSS:
/*Base Mobile Layout*/ .wrap { width: 90%; margin: 0 auto; } #main-content, #sidebar-left, #sidebar-right { width: 100%; float: left; } /*3 Column Layout*/ @media only screen and (min-width: 1024px) { .wrap { width: 1024px; margin: 0 auto; } #main-content { width: 50%; float: left; } #sidebar-left { width: 25%; float: left; } #sidebar-right { width: 25%; float: left; } } /*Wide Layout*/ @media only screen and (min-width: 1200px) { .wrap { width: 1140px; margin: 0 auto; } }Working from mobile up we have put the main content column first, then the left and right sidebars follow. This is how we want the design to display on mobile, and is also the correct hierarchy for search engine and accessibility purposes. The problem is that, our design dictates that the left sidebar should be on the left at full size, but with this code it will render in the center. And no matter what you do, CSS will always float elements in the order they appear in the HTML. So, we need to move that column above the main content somehow at full size.
/*Move Sidebar Position*/ @media only screen and (min-width: 1024px) { .wrap, #main-content, #sidebar-left, #sidebar-right { position: relative; } #main-content, #sidebar-left, #sidebar-right { top: 0; } #sidebar-right { right: 0; } #sidebar-left { left: -50%; /*Width of #main-content*/ } #main-content { left: 25%; /*Width of #left-sidebar*/ } }This code applies position: relative to the .wrap element and all 3 columns. The top position for all 3 columns is set to 0, and then we stick the right sidebar to the right, move the left sidebar left -50% (negative the width of #main-content) and move the main content left 25% (the width of #left-sidebar) to rearrange the columns.
/*Optional 2 Column Layout*/ @media only screen and (min-width: 768px) and (max-width: 1024px) { #sidebar-right { width: 50%; } #sidebar-left { width: 50%; } #main-content { position: relative; top: 0; left: 0; } }
Comments