Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we've discussed media queries and how to properly organize responsive styles, let's turn our attention to some of the ways that we can take advantage of media queries to make our content a little bit more responsive. So I have the content.htm file from the 07_04 folder opened up, and I want to preview the finished version of this for you in the browser first, so I'm going to switch over to my browser here. Okay, so here's our finished page. This is sort of where we're going, and I just want to talk about the different parts of this. Over here on the right-hand side, we have a menu of our archived galleries.
Beside that we have a branding image. And you'll notice that as I begin to resize this that this layout is flexible. So as I begin to make the page larger or smaller, obviously the different parts of it are responding to that. Now if I hit a specific breakpoint and that breakpoint is for our mobile devices, you'll notice that the layout changes and when the layout changes, the menu stays vertical, but now the branding image is stacked on top of it and it's a much smaller branding image designed more for the tablet-based experience. And if I keep scrolling, you'll notice that this one is flexible as well. And if I go down far enough to hit my breakpoint, here I reach the mobile version of that.
Here we have a totally different branding image and then instead of that long menu that we saw before, because on the mobile screen space is at the premium, we now have a dropdown menu or a little pulldown menu that we can use instead of the actual menu itself. Now page structure doesn't change at all. The only thing that changes here are styles, so let's go into our HTML and CSS and see how we can make this content a little bit more responsive. So here I am, back in the content.htm file, and the first thing I want to do here is sort of scroll down through so you can see the structure of the page, because there are few things here that are a little tricky, and I'm going to go into the HTML to show you what I'm talking about.
In the HTML you can see that once we get down a little bit, we've got a section for our banner so that's that branding graphic that you were seeing, and inside that we have a heading 2 that says Fine urban photography. After that we have our menu and it's just an ordered list with a bunch of links. However, directly after that we have our pulldown menu or our select. So essentially what's happening is at one specific size we're deciding to show one of these elements, and of course that means we're going to be hiding the other element. Now that's not really an ideal way of doing this, because obviously both the menu and the pulldown menu, the select element, still remain in our HTML code, and that means that other user agents or accessibility devices are going to see both of them.
Now again, always be aware of the fact that that is the closing curly brace of your media query. You don't want to write any styles outside of that. I'm just going to write .menu select, so I'm targeting that pulldown menu inside the menu region. And for my tablet styles I don't want that to display, so I'm going to change the display property to none. I'm going to use the exact same rule of my desktop, so I'm just going to copy that, go down to the very bottom of my desktop styles, again right here, right before the closing curly brace for my desktop styles, and I'm going to paste that and save it.
So that's basically going to turn off the visibility of that pulldown menu for my desktop styles and for my tablet styles. Now for my mobile styles, on the other hand, I do want that to display. So I'm going to go up into my mobile styles--and this is one of the reasons why I love commenting out these regions, because otherwise it would be very difficult for you to know where one region started and another one stopped. So I know the styles right above this are my mobile styles, and here what I'm going to do is I want the menu select to display, but I don't want the unordered list above it to display.
So here I'm going to type in .menu ul to target the unordered list, and I'm going to tell that not to display. I also need to control the visuals of my select, so underneath that, I'm going to do .menu select and I'm going to give that a width of 200 pixels. I'm going to display it as a block-level element, and that's going to allow me to center it using margins. I'm going to do a margin of 1em top and bottom. It's going to give me some space in between my headlines and the menu. And then I'm going to do auto for left and right, which is going to center it.
Now if I save this and if I preview this page, you see we haven't put our image in there yet, but now there is our menu on the desktop version. There is our menu on the tablet version, and you can see the dropdown menu isn't there. And once I go up to the mobile version of that, the unordered list is not showing up, but the Select menu is. Perfect! The only thing we need to do next is take care of our banner image. So I'm going to go back into my styles, and the first thing that I want to do is I'm going to go into my desktop styles. So I'm just going to scroll down into my desktop styles. I'm going to find the banner selector which is right here on line 155, and just below that I'm going to type in a height of 450.
Now this is being driven by the image that I'm using, so I'm making this element the size that my image is. And I am going to give it a background, and I'll just do a URL to point to the image, and I'm going to go _images. It's in the _images directory, and then I am going to do stairs.jpg. And then right after that I'm going to do a no-repeat so that it does not tile. All right! So I'm going to go ahead and save that. And I just need to keep doing this for each one of these. So that was desktop. I am going to scroll up into tablet and I am going to find the banner for tablet, and I'm going to do the exact same thing.
I'm just going to use different values obviously. So for height, for my tablets, that's going to be 150 pixels. And I'm going to do background: url, and then in the url this one is going to be _images/sub.jpg. And once again no-repeat so that doesn't tile. So I'm going to go ahead and save that. And then the last one that we're going to be doing is mobile obviously so I'm going to go up into mobile and I'm going to find banner up in mobile. And I'm going to do pretty much the same thing. I'll give it a height of 200 pixels, and then for background I am going to give a url (_images/glass.jpg) and there again we're going to say, no-repeat.
Now one of the reasons why obviously I chose a background image rather than an image tag, there are a couple of reasons for that. Number one, it's a purely decorative image. But number two, it is a lot easier to swap out background images for media queries than it is an actual image tag. For an image tag we would have to do something very similar to what we were doing with the dropdown menu, which is have multiple images on the page, then hide and show one for one media query, hide and show it another way for another media query. That is really not efficient. You're talking about still having to download all of those background images, and then you have the overhead of turning them on and off.
In this case we're simply requesting the images that we need. So you know, I'm setting here resizing the browser when I test this, but people that use this on different devices, they won't be resizing their browser. If it's viewed on the smartphone, the only image that's going to be requested is glass, if it's viewed on a tablet, the only image that's going to be requested is sub, so forth and so on. So if you're limiting the number of requests it's making to the server, you're making life a little bit easier for the people that are viewing your site because you've lowered the overhead. So I'm going to save that, go back out to my browser, and refresh. There is my image. If you're wondering, the headline is already styled obviously. And then as I scroll down, I get my first breakpoint, and it requests the sub graphic, and as I hit my third breakpoint I now get the glass graphic, and they're all sitting within exactly the same element.
Now even though it's a little bit more complex, it's still a fairly simple example. If you are really focused on making your content more responsive or using responsive layouts, you really have to put a lot of thought into how you're going to structure and style your pages. You know, you have to think not only about what the layout is going to look like, but also about how people are going to be using it, you know things like how content is typically consumed and used on mobile devices versus desktop or how orientation could affect content needs. Now stylistically we can do anything we want now based on a number of different factors, including orientation and screen size. Now my point-- and frankly the main goal of this exercise--is to let you know that you need to start thinking in creative ways about how you can change layout and presentation that is device-specific and focused on how people are likely to use your site.
Get unlimited access to all courses for just $25/month.Become a member