4. Controlling the width of images - Responsive CSS Tutorial

Scrimba · Beginner ·🌐 Frontend Engineering ·6y ago

Key Takeaways

This video tutorial demonstrates how to control the width of images in responsive web design using CSS, covering topics such as setting widths, using percentages, and avoiding image distortion. Tools and techniques used include CSS selectors, width properties, and percentage-based sizing.

Full Transcript

okay so we have our containers that are set at fifty percent of our total screen size but we have this really awkward problem that's going on with our image our image just does not look very good when it's sticking out the side like that so how can we fix it can you think of a way that we could solve that problem is there something that we've looked at so far that would solve it because we have all the tools we need to be able to fix that so see if you can think about it coming to code try playing around with something and see if you can solve the width of the image so hopefully what you thought of is you can actually set a width on your image so if we come and take a look here images are a little bit weird and what you probably don't remember is when we first were looking at block and inline I mentioned that images aren't actually an inline element they can flow with text if I put an image in a paragraph the text on that line will will actually flow with it put that image in the paragraph itself they're a bit of a special case there's things that are a little bit different with them but they are technically an inline element so they're not defaulting to 100% with their defaulting to the size of themselves what that size of the image is so what we can actually do is we can come in and I like grouping it sort of here with all the other things that I'm doing in the general area instead of the more specific areas room styling things and I can select my images so IMG I'm not putting a dot I'm not putting anything else because I'm selecting my image itself so I'm gonna select my image I'm gonna give this a width now this width for now I'm gonna give this a width of 250 pixels it's a set unit we're not gonna leave it there but just to show you what that does so look at that my my image size has shrunk down the one thing you have to be really careful with if you set a width on an image is not to also set a height to it because if I set a height on this image which you can do it will actually end up stretching the image and distorting it and making it look really really ugly whereas if you only put one or the other so I could say a height or I could say a width either one is going to work and if I if I only have one of them it's automatically going to handle the the second property so it's going to keep it in proportion instead of stretching it so you know now that we can set a width on an image how do you think we can get that image to match the size of the container so hopefully you said use a percentage and what I want you to do is set it and see if you picked the right percentage try and set an actual size on this using percentage and see if it's fitting the way you think it would all right so there's two answers people usually think of one of them is to give it a width of a hundred percent and the other one is a width of 50 percent and the reason a lot of people think of 50 percent is because on my container we gave that a width of 50 percent here so if this has a width of 50% it makes sense to give the image a width of 50% but if you did that you might have noticed it didn't work it's pretty small but if you shrink this you'll see it's it is growing and shrinking with my page so the percentage is working it's just not what we thought it was so we just have to remember what I said before about how percentages work a percentage is always based on the width of the parent so if we come and we look in our index here when we set the width of the container this was 50% of the width of section 1 which automatically was the full size of my screen so it ends up being 50% of my screen width this image on the other hand its parent is the container itself so this image is the whatever percentage I set for a width on my image will be a percentage of this container width so if we come into my styles now my image I actually wanted to be 100% so if we go and look again we can see that it has been fixed and now whatever I do here my image is going to grow and shrink with the size of that container and match everything that's around it which just makes the layout work much better so if I come down and get rid of that magenta border and we go and look at it again we can see that it looks nice and fancy and it fits well with the overall look of the document the only thing is a 50% width is a little bit small so if I come down to this everything is getting a little bit smushed I'd rather that it's sticking closer to the sides and I'm not getting like this massive it's almost acting like padding you know look how small that images I can barely see it so it's really common to not have a width of 80 50 percent but to do like a 90 percent or something like that because on small screens that's gonna look a lot better that's sort of that that cell phone type look that you're probably used to having and seeing but as this gets bigger we do at one point now we're getting two big lines of text going from one side all the way to the other is really really awkward so we want to try and avoid having text that stretches from one side of the other like what's happening right now and the next video we're going to look at how we can do that

Original Description

🎓 View our courses: https://scrimba.com/links/all-courses This tutorial is a part of "The Responsive Web Design Bootcamp" on Scrimba. Explore the full course here: https://rebrand.ly/responsivebootcamp_yt We can fix our image by giving it a width, and to make it match the size of our container, we can give it's width a value using percentage so that matches the size of the container at all times!
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Scrimba · Scrimba · 45 of 60

1 CSS Grid Course: Learn the Basics in 3 Minutes
CSS Grid Course: Learn the Basics in 3 Minutes
Scrimba
2 CSS Grid Course: Positioning Items
CSS Grid Course: Positioning Items
Scrimba
3 CSS Grid Course: Why Learn It And How It Compares To Bootstrap
CSS Grid Course: Why Learn It And How It Compares To Bootstrap
Scrimba
4 CSS Grid Course: auto-fit & minmax
CSS Grid Course: auto-fit & minmax
Scrimba
5 CSS Grid Course: Implicit Rows
CSS Grid Course: Implicit Rows
Scrimba
6 CSS Grid Course: Fraction Units And Repeat
CSS Grid Course: Fraction Units And Repeat
Scrimba
7 CSS Grid Course: Justify Items and Align Items
CSS Grid Course: Justify Items and Align Items
Scrimba
8 CSS Grid Course: An Awesome Image Grid
CSS Grid Course: An Awesome Image Grid
Scrimba
9 CSS Grid Course: Named Lines
CSS Grid Course: Named Lines
Scrimba
10 CSS Grid Course: auto-fit vs auto-fill
CSS Grid Course: auto-fit vs auto-fill
Scrimba
11 CSS Grid Course: Justify Content and Align Content
CSS Grid Course: Justify Content and Align Content
Scrimba
12 CSS Grid Course: Template areas
CSS Grid Course: Template areas
Scrimba
13 27. Setting up the structure - Responsive CSS Tutorial
27. Setting up the structure - Responsive CSS Tutorial
Scrimba
14 25. Making the navigation responsive - Responsive CSS Tutorial
25. Making the navigation responsive - Responsive CSS Tutorial
Scrimba
15 36. Playing with the title's position and negative margins - Responsive CSS Tutorial
36. Playing with the title's position and negative margins - Responsive CSS Tutorial
Scrimba
16 31. Starting the CSS for our page - Responsive CSS Tutorial
31. Starting the CSS for our page - Responsive CSS Tutorial
Scrimba
17 26. Taking a look at the rest of the project - Responsive CSS Tutorial
26. Taking a look at the rest of the project - Responsive CSS Tutorial
Scrimba
18 15. Spacing out the columns - Responsive CSS Tutorial
15. Spacing out the columns - Responsive CSS Tutorial
Scrimba
19 33. Starting to think mobile first - Responsive CSS Tutorial
33. Starting to think mobile first - Responsive CSS Tutorial
Scrimba
20 22. Making our navigation look good - Responsive CSS Tutorial
22. Making our navigation look good - Responsive CSS Tutorial
Scrimba
21 37. Changing image size with object-fit - Responsive CSS Tutorial
37. Changing image size with object-fit - Responsive CSS Tutorial
Scrimba
22 44. Module Wrap up - Responsive CSS Tutorial
44. Module Wrap up - Responsive CSS Tutorial
Scrimba
23 16. Controlling the vertical position of flex items - Responsive CSS Tutorial
16. Controlling the vertical position of flex items - Responsive CSS Tutorial
Scrimba
24 39. Setting up the widgets and talking breakpoints - Responsive CSS Tutorial
39. Setting up the widgets and talking breakpoints - Responsive CSS Tutorial
Scrimba
25 42. Setting up the About Me page - Responsive CSS Tutorial
42. Setting up the About Me page - Responsive CSS Tutorial
Scrimba
26 35. Changing the visual order with flexbox - Responsive CSS Tutorial
35. Changing the visual order with flexbox - Responsive CSS Tutorial
Scrimba
27 23. Adding the underline - Responsive CSS Tutorial
23. Adding the underline - Responsive CSS Tutorial
Scrimba
28 21. Using flexbox to start styling our navigation - Responsive CSS Tutorial
21. Using flexbox to start styling our navigation - Responsive CSS Tutorial
Scrimba
29 20. Creating a navigation - Responsive CSS Tutorial
20. Creating a navigation - Responsive CSS Tutorial
Scrimba
30 40. Using a new pseudo class to wrap up the homepage - Responsive CSS Tutorial
40. Using a new pseudo class to wrap up the homepage - Responsive CSS Tutorial
Scrimba
31 43. Fixing up some loose ends - Responsive CSS Tutorial
43. Fixing up some loose ends - Responsive CSS Tutorial
Scrimba
32 32. Starting the layout. Looking at the big picture - Responsive CSS Tutorial
32. Starting the layout. Looking at the big picture - Responsive CSS Tutorial
Scrimba
33 24. A more complicated navigation - Responsive CSS Tutorial
24. A more complicated navigation - Responsive CSS Tutorial
Scrimba
34 28. Feature article structure - Responsive CSS Tutorial
28. Feature article structure - Responsive CSS Tutorial
Scrimba
35 34. Styling the featured article - Responsive CSS Tutorial
34. Styling the featured article - Responsive CSS Tutorial
Scrimba
36 18. Making layout responsive with flex direction - Responsive CSS Tutorial
18. Making layout responsive with flex direction - Responsive CSS Tutorial
Scrimba
37 19. flex direction explained - Responsive CSS Tutorial
19. flex direction explained - Responsive CSS Tutorial
Scrimba
38 41. Creating the recent posts page - Responsive CSS Tutorial
41. Creating the recent posts page - Responsive CSS Tutorial
Scrimba
39 17. Media Query basics - Responsive CSS Tutorial
17. Media Query basics - Responsive CSS Tutorial
Scrimba
40 30. Home Page. HTML for the aside - Responsive CSS Tutorial
30. Home Page. HTML for the aside - Responsive CSS Tutorial
Scrimba
41 38. Styling recent articles for large screens - Responsive CSS Tutorial
38. Styling recent articles for large screens - Responsive CSS Tutorial
Scrimba
42 29. The home page.  HTML for the recent articles - Responsive CSS Tutorial
29. The home page. HTML for the recent articles - Responsive CSS Tutorial
Scrimba
43 10. ems and rems   an example - Responsive CSS Tutorial
10. ems and rems an example - Responsive CSS Tutorial
Scrimba
44 1. Starting to think responsively - Responsive CSS Tutorial
1. Starting to think responsively - Responsive CSS Tutorial
Scrimba
4. Controlling the width of images - Responsive CSS Tutorial
4. Controlling the width of images - Responsive CSS Tutorial
Scrimba
46 5. min width and max width - Responsive CSS Tutorial
5. min width and max width - Responsive CSS Tutorial
Scrimba
47 3  CSS Units.  Percentage - Responsive CSS Tutorial
3 CSS Units. Percentage - Responsive CSS Tutorial
Scrimba
48 11. Flexbox  refresher and setting up some HTML - Responsive CSS Tutorial
11. Flexbox refresher and setting up some HTML - Responsive CSS Tutorial
Scrimba
49 12. Basic Styles and setting up the columns - Responsive CSS Tutorial
12. Basic Styles and setting up the columns - Responsive CSS Tutorial
Scrimba
50 8. The Solution Rems - Responsive CSS Tutorial
8. The Solution Rems - Responsive CSS Tutorial
Scrimba
51 14. Setting the columns widths - Responsive CSS Tutorial
14. Setting the columns widths - Responsive CSS Tutorial
Scrimba
52 2  CSS Units - Responsive CSS Tutorial
2 CSS Units - Responsive CSS Tutorial
Scrimba
53 7. The problem with ems - Responsive CSS Tutorial
7. The problem with ems - Responsive CSS Tutorial
Scrimba
54 6. CSS Units. The em unit - Responsive CSS Tutorial
6. CSS Units. The em unit - Responsive CSS Tutorial
Scrimba
55 13. Adding the background color - Responsive CSS Tutorial
13. Adding the background color - Responsive CSS Tutorial
Scrimba
56 9. Picking which unit to use - Responsive CSS Tutorial
9. Picking which unit to use - Responsive CSS Tutorial
Scrimba
57 Tutorial to Learn Alpine JS - Full Course for Beginners
Tutorial to Learn Alpine JS - Full Course for Beginners
Scrimba
58 Guide To Algorithms in Javascript [Binary Search] - Full Course / Tutorial
Guide To Algorithms in Javascript [Binary Search] - Full Course / Tutorial
Scrimba
59 Learn UI Design [7 Fundamentals Tutorial] - Full Course for Beginners
Learn UI Design [7 Fundamentals Tutorial] - Full Course for Beginners
Scrimba
60 Javascript Tutorial for Beginners [From 0 to ES6+] - Full Course
Javascript Tutorial for Beginners [From 0 to ES6+] - Full Course
Scrimba

This tutorial teaches how to control image widths in responsive web design using CSS, including setting widths, using percentages, and avoiding image distortion. By following along, viewers can learn how to create responsive and visually appealing web pages.

Key Takeaways
  1. Set a width on an image using CSS
  2. Use percentage-based sizing to make images responsive
  3. Avoid setting both width and height on an image to prevent distortion
  4. Use CSS selectors to target specific images or elements
💡 When using percentage-based sizing, the percentage is based on the width of the parent element, not the screen width.

Related Reads

📰
Stop installing giant icon packages — write SVGs as source with a CLI + MCP
Learn to create SVG icons from scratch using a CLI and MCP, reducing dependency on giant icon packages
Dev.to · arialus
📰
Frontend Engineers Are Quietly Becoming the New Overhead.
Frontend engineers are being bypassed in the development process, with features being shipped without their involvement, highlighting a potential shift in their role
Medium · AI
📰
Building an Interactive Fashion Hero with HTML, CSS & GSAP
Learn to build an interactive fashion hero using HTML, CSS, and GSAP, and discover how to create immersive digital experiences
Dev.to AI
📰
Out-of-Order HTML Streaming Moves from JS Frameworks into the Browser
Out-of-order HTML streaming is moving from JS frameworks to browsers, enabling faster page loading and interaction
InfoQ AI/ML
Up next
Sports on the Go: Africa makes history at 2026 FIFA World Cup
CGTN Africa
Watch →