Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Making images flexible

From: CSS: Page Layouts

Video: Making images flexible

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.

Making images flexible

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.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 42753 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 20s
    1. Welcome
      54s
    2. How to use the exercise files
      3m 26s
  2. 1h 39m
    1. Box model review
      8m 47s
    2. Calculating element dimensions
      11m 11s
    3. Understanding margin collapse
      7m 59s
    4. Calculating em values
      7m 41s
    5. Calculating percentage values
      7m 51s
    6. Normal document flow
      13m 3s
    7. Controlling element display
      8m 53s
    8. Using CSS Resets
      7m 11s
    9. Fixed, fluid, and responsive layouts
      9m 9s
    10. CSS debugging tools
      6m 46s
    11. Using the Firebug Inspector and the WebKit Web Inspector
      11m 5s
  3. 53m 15s
    1. Page design workflow
      3m 6s
    2. Page design tools
      4m 56s
    3. Determining page structure
      7m 18s
    4. Creating image assets
      8m 58s
    5. Creating initial page structure
      7m 3s
    6. Adding meaning with classes and IDs
      5m 23s
    7. Structuring content with HTML5
      6m 6s
    8. Building internal structure
      10m 25s
  4. 1h 36m
    1. Floating elements
      7m 50s
    2. Clearing floats
      7m 28s
    3. Containing floats
      7m 50s
    4. Clearfix technique
      10m 38s
    5. Floating inline elements
      14m 34s
    6. Two-column floated layouts
      8m 17s
    7. Three-column floated layouts
      11m 30s
    8. Column height considerations
      7m 3s
    9. Creating equal-height columns
      10m 42s
    10. Floats: Lab
      5m 25s
    11. Floats: Solution
      5m 21s
  5. 51m 42s
    1. Relative positioning
      7m 59s
    2. Absolute positioning
      8m 59s
    3. Fixed positioning
      4m 23s
    4. Controlling stacking order
      8m 31s
    5. Clipping content
      8m 21s
    6. Controlling content overflow
      5m 38s
    7. Positioning elements: Lab
      3m 59s
    8. Positioning elements: Solution
      3m 52s
  6. 48m 46s
    1. Design considerations for fixed layouts
      3m 28s
    2. Establishing the layout grid
      7m 57s
    3. Defining column spacing
      9m 30s
    4. Applying the grid through CSS
      8m 56s
    5. Creating grid-based assets
      8m 26s
    6. Grid design resources
      6m 22s
    7. Building fixed layouts: Lab
      4m 7s
  7. 44m 35s
    1. Designing for flexible layouts
      2m 30s
    2. Calculating percentage values
      8m 45s
    3. Setting flexible width values
      6m 6s
    4. Making images flexible
      8m 10s
    5. Setting minimum and maximum widths
      7m 24s
    6. Building flexible layouts: Lab
      4m 53s
    7. Building flexible layouts: Solution
      6m 47s
  8. 49m 36s
    1. Responsive layout overview
      3m 49s
    2. Using media queries
      7m 16s
    3. Organizing styles
      8m 39s
    4. Making content responsive
      8m 33s
    5. Mobile design considerations
      7m 32s
    6. Building responsive layouts: Lab
      4m 23s
    7. Building responsive layouts: Solution
      9m 24s
  9. 1h 22m
    1. Creating multi-column text
      6m 36s
    2. Using borders to enhance design
      13m 59s
    3. Rounding corners
      6m 56s
    4. Adding drop shadows
      10m 35s
    5. Working with opacity
      6m 8s
    6. Utilizing the background property
      15m 5s
    7. Working with CSS sprites
      7m 58s
    8. Enhancing page design: Lab
      6m 22s
    9. Enhancing page design: Solution
      8m 38s
  10. 6m 25s
    1. Additional resources
      6m 25s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Page Layouts.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.