When I was redesigning a few sites, I wanted to put a fixed width sidebar to place ads. But since I was working on to make a responsive design for the site, a sidebar based on grid layouts which are based on percent-width wouldn’t have solved my purpose. In this post, I will share the CSS which I used to create a fixed width sidebar for this site, while keeping the entire site layout responsive and suitable for mobile views.
You can see the final demo by tapping on the preview image below. Try resizing your browser window to see the fixed width sidebar inside a responsive layout.
Find below the HTML needed to create the layout of the page. We are using a div of class
wrapper to wrap our content as well as sidebar. This wrapper provides a maximum width to our page layout which I have chosen to be 1160px in the CSS. You can increase this width as per your own need. This wrapper also has left and right margin set as
auto in CSS which helps it to stay at the center of the page.
We would need an additional wrapper for our content called
content-wrapper which we are going to make of width
100% and with a float property of
left. This allows our sidebar to stay on top of the content wrapper instead of flowing down.
CSS for wrappers
This is how our final CSS looks for the page and content wrapper divs. You can ignore the min-height attribute as it has been added only to give a height to the demo without placing any content.
CSS for content and sidebar
Before writing the CSS for content and sidebar, we need to determine the size of our fixed width sidebar. I have chosen a width of 320px for our sidebar. If you choose any other size then do make sure to change the appropriate values in the CSS shown below.
Depending on the fixed width of our sidebar, we have applied a margin of same width to our content. Since I am placing my fixed width sidebar on the right, I have applied a right margin. We will then give a fixed width of 320px to our sidebar with a negative margin to make it appear next to our content inside our content-wrapper.
We are pretty much done for larger screens and will have the page content have a responsive width while the sidebar maintaining its fixed width for different screen sizes. However there is still some work left to optimize it for small screen devices such as smart-phones, as this fixed width sidebar can actually take up the entire width (320px) on the small screens leaving no space for the content.
Responsive layout on mobile
For the mobile screens, I am going to hide the sidebar altogether and just stick with the main page content. This is an ideal way to display content on small screen devices and generally you should not be placing any important content in the sidebar. Here is the CSS to hide the fixed width sidebar when viewed on mobile devices. Note that I am using a media query breakpoint of 768px, below which the sidebar will get hidden. If you want to hide the sidebar for tablets too, you may opt for a bigger size for this breakpoint.
Demo of fixed width sidebar
We are now done with creating our fixed width sidebar in a responsive web design layout. You can view the demo as well as the full code on CodePen using one of the options below:
Fixed width sidebar on left
In case you want the sidebar on the left, instead of right, then some minor tweaks in CSS margins would get us the desired result without the need of changing the HTML. Find below the code pen demo with fixed width sidebar on left.
We are using the same logic to create a fixed width sidebar on this blog. You can see the sidebar on this site too as an example of the fixed width sidebar in responsive layout. Hope you found this Tutorial useful! Let me know if anything doesn’t work for you.