Hi there. Today’s fun post is delving a bit into the smartest way to make your website responsive for devices of all sizes – media queries!
A media query is the code you add to your website’s stylesheet to help align the design with the size of the screen the visitor is using.
This is my About page on a 1220px wide desktop:
This is my About page when I re-size my browser window to about 515px wide:
See what happened? My website responded to the size of the screen so that I, as a visitor, could still read everything without having to zoom in. This, my friends, is what keeps people on your website when they’re not viewing on a desktop.
Hot tip: within the next two years, mobile browsing will likely surpass desktop browsing. So here’s the best way to go about making sure your site is lookin’ good.
Don’t design with specific phone sizes in mind. Media queries will often be written based on the size of an iPhone, an Android, a tablet, etc. We know those screen sizes, so if we cater specifically to them, we know it’ll look great. But hold up. Say two years ago I set up media queries that looked great on the iPhone 5 portrait and landscape mode. But now the iPhone 6 and 6+ are out, and they’re different sizes, so does that mean a whole new media query? There’s no way to know how devices will change over time.
Start with your browser at it’s widest width, then slowly decrease the width until it looks like crap, and then write a media query for that size window. I just practiced this with my site, and realized that, hey, at around the 1140px mark, my menu overlaps into two lines, and cuts off my page title. So, let’s fix it! This is real time blogging, people.
What it looks like when I re-size my browser to 1140px:
See how the upper menu drops into a second line, and covers up the “About” page title? No bueno. Let’s get to cracking. First, I check out Inspect Element to see what’s going on with my code. My header is comprised of two pieces – the title area, which is where my logo is in the left, and the header-widget-area, which is where my social media icons and menu are. Here’s what I see:
The title area is 360px wide.
The widget area changes depending on the size of the screen. It’s 560px when the screen is 1140px or less (blue arrow), and 740px when the screen is larger than 1140px (red arrow).
The 560px widget area is what’s causing the menu to display on a second line – that’s not enough space for the menu to display on one line.
Which brings me to an important point: when possible, code using percentages, not pixels. If I know about how much space I want the menu to take up, I can give it a percentage number, rather than a pixel size, and it makes the whole thing much more fluid and responsive. So, looking at my header, I can say I want my title area to take up 25%, and my widget area to take up 75%. Here’s what that looks like in the stylesheet:
I sometimes will comment out the original code when entering in new code, just in case anything goes wrong. That way I still have the original stuff in there to revert back to easily. But, everything worked, and now my site looks like this at 1140px:
WINNING! Have any questions? Let me know in that big ol’ comment box below!
CSS for Web Designers
Learn more about my upcoming CSS for Web Designers course, and get useful tools + tutorials to make you a better web designer.