Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As long as your page content is limited to a content that automatically reflows, like text, you'll be fine. However, once you start putting images and other fixed-width resources like video on your page, you are going to quickly find that your fluid layout has some limitations. So to demonstrate that, I have the fluid.htm file opened here from the 06_04 folder. And if I just open this in a browser, you can see we've added an image to our layout. And right now everything looks just fine. It's fitting in there fine. The text is reflowing around it okay.
However, if I begin to resize this image, watch what happens. Whereas the content and the columns are flexing, this image has a very fixed width. So at the end of the day, you just end up with this text running right over the top of it. It's colliding and it's tearing up your layout, and that's going to happen wherever you use fixed-width images within your fluid or flexible layouts. There are some things that we can do to help us with this. So I am going to go back into my code and I am going to scroll down, and let's take a look at the image, which is about on line 107 or so here.
If I take this image and I change its width from 448 pixels to 100%, let's see what happens then. So I am using a percentage value rather than a fixed value for the image width. I am going to save this, go back to my browser, and refresh. You'll notice that it did a couple of things. It distorted the image, so it sort of stretched it, if you will, and it stretched it to fill the entire column. So that tells us a couple of things here. Number one, we don't want to leave the height at a fixed value. The other thing is that the width, just like when you're calculating the width of elements and padding and margin, it is relative to its parent element. And you're not saying 100% of what the normal width of this element would be; you are actually saying give me 100% of its parent element, which would be the entire width of the column.
So we need to find what the appropriate percentage is to display this image in its natural state. And I've got to tell you something. That is not easy to do. You can't just say, "Okay, what's my target resolution? All right! Let me go ahead and factor that in," because it's not always quite that simple. Let me show you, though, a very easy way I've found to sort of work through this issue. So what I am going to do is I am going to revert back to the image the way it was before, and then I'm going to copy this image, and then I'm going to paste it right in there.
Now, if I save this, if I preview this, this gives me two of these images stacked right on top of each other. And when I am looking at these images, one of the things I'm thinking about right now is, okay, what percentage of this column width is this image. What percentage is that taking up? If you had to guess, what would you say, around 70, 75, 80%, somewhere around in that range? That's typically where I start out. I sort of look at that and say, okay, what percentage is that? So if I flip over here and I take the second image and I get rid of its height, I am going to strip the height out, and I take its width value and I change that, so let's say I changed it to 70%, and if I save this, and then preview it, you can see the flexible image now is a little bit smaller. So it's close there, but it's not quite on the money.
So if I go back in, I can start manipulating this value a little bit. If I go up to 78% and save it and preview it, I can see that it's a little bit larger. Now, one thing that you really want to try to avoid is you want to try to avoid scaling the image up. That's going to damage the image quality, and it's going to cause some issues. So if you can find a happy medium, then that's fine. And if I go back here, let's try 74%, save that, preview it, and that's really close. I mean, you are talking about a couple of pixels off. And since this image is going to be fluid anyway, that's not a bad place to start from.
I am going to go back over and get rid of my fixed image and just sort of leave this one where the other one used to be. So I stripped the height value out. I gave it a width of 74%. If I save this now and preview it, I am left with my flexible image. And now you'll notice if I resize the browser, the image resizes with it, which is really cool. There are some downsides to this. Performance in mobile devices can be negatively impacted by the browser having to constantly scale images.
You also have to think about image quality itself. The image quality can suffer. It's not suffering quite as much if you scale down instead of up. And notice if I had a really larger monitor than this and I kept expanding, I would actually be scaling up. So a good rule of thumb is to use maybe a slightly larger image and make sure that you're always sort of scaling it down a little bit, so that you're not losing a lot of image quality. There are other ways to make images or elements responsive as well. Now, the same technique that we just did here would also work on video, and you definitely want to make sure not to leave the height value in there for those video objects either, because you definitely want to make sure the aspect ratio remains the same on those.
But you can do that for video. You can do it for canvas. You can do it for all sorts of assets that you might be placing here on the page if they need to be fluid as well. But there is another method that we can use that specifically targets images, and that's working with background graphics. So if I go back into our code and I scroll down, I can see that on 110 I have an empty div tag here with the class of banner. Now, typically I wouldn't use an empty div tag. I mean that's non-semantic markup, but if I'm wanting an image that's just purely decorative in nature and I want the image to be able to flex with the layout, then sometimes using this technique is actually a pretty nice solution.
So I am going to scroll up to my CSS and we are going to see how we are going to use this. We have an empty banner selector already up there. So what I'm going to do is I am going to give it a height of 250 pixels. Now, how do I know the height? Well, the image that I am going to be using here within this banner, I know it's 250 pixels tall, so I kind of have a little leg up on this. Speaking of that image, I am going to go ahead and do a background image here, and I am going to give it a url of _images/philly_banner.jpg. And then I am going to go ahead and just tell that no- repeat, so that it doesn't tile, in case the container stretches out wider or taller than the actual image itself.
The next thing I am going to do is set some margins on this. I am going to do a margin of 2% for top and bottom-- that will keep the text away from the top and bottom of the image--and then 0 from side to side. So if I save this and then preview this in the browser--let me just refresh this page-- if I scroll down, now, here's my image coming in. Now, remember, I didn't give this a width at all. Knowing that the behavior of a div tag, for example, is to stretch to fill its parent container, I knew that by default that div tag is just going to expand to fill the column, so that's fine.
I now have an image that stretches the entire width of the column. The height is determined by the image itself. And again, because we didn't specify a width, you'll notice that as we resize the browser, the div tag resizes as well. However, the image itself doesn't resize. Essentially what we're doing is we are almost cropping off a portion of that image as we resize this; it's sort of sliding. So when you're doing something like this, number one, it has to be a decorative image, and number two, you have to realize that at certain resolutions you are going to be losing part of that image. That way if I was creating an image where something had to be seen, I would make sure that that subject matter was on the left-hand side here rather than on the right-hand side, because I know that that has a chance of being hidden based upon the size of the screen.
If you're going to be integrating images into your fluid layouts, you really have a lot of factors that you need to sit down and think through as you are planning your layout. You need to consider how scaling images might affect image quality, what type of performance hit you are going to take within the browser, especially for mobile devices, whether or not you can effectively control content reflow. So if you're making it flexible and the content is reflowing around it, what happens if a headline above this suddenly comes down below this image? Do I need to clear those within the float, that sort of thing.
So in our next movie we are going to explore how we can use minimum- and maximum- width properties to give us greater control over not just image assets, but all parts of our fluid layouts.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 61320 Viewers
61 Video lessons · 93523 Viewers
82 Video lessons · 104382 Viewers
56 Video lessons · 107830 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.