3 CSS Units. Percentage - Responsive CSS Tutorial

Scrimba · Beginner ·🛠️ AI Tools & Apps ·6y ago
Skills: HTML & CSS70%

Key Takeaways

Explains how to use percentages in CSS for responsive web design, as part of Scrimba's Responsive Web Design Bootcamp

Full Transcript

so we're gonna start with percentages instead of em in RAM just because it's much easier to understand how they work so if we look at a layout like this and we will be making this layout in the next module of the course once we've learned how to think responsibly cuz we're going to make it responsive we're gonna make this work on small screens up to big screens and that's gonna be our project in the next one but for now just looking at this and thinking about it with what we know at the moment I would have a set width on the total size of this so say I say the entire document here is 960 pixels so I set a width I put a container on the whole thing or something and they say width 960 pixels so that's fine and this is a common size we used to use in the really really old days and then I have the two parts here so I need to define an individual width for the left and the right side so I might say well I want that left side to be about 70% of that so I need to calculate what that is so I do 960 times point seven and that would give me six hundred and seventy-two pixels so now I know the width and it's at the width of six hundred and seventy two pixels for my left side but then there's also the other side and I have to find out the fixed pixel value for that side and well then I also have padding in there so then I'd have to start subtracting padding and doing all this oh it's annoying right you don't want to have to start thinking about all of these things and this is for a fixed width imagine now you're trying to bring all those numbers into it for you're trying to like adjust it for different screen sizes so instead of complicating matters when we think responsively we want to simplify matters a big part of thinking responsively and I sorry if I ever say respond responsibly they're so similar so if we when we started to think responsively it's all about trying to simplify and out complicate matters so you remember this layout we just finished it but let's say we wanted to turn this into a more responsive layout instead of the fixed layout like we have now we can do that so let's go and take a look at how we can do it um and you can probably guess the one thing that we really need to change is this fix container with because right now it has a width of 620 pixels so we have a set width on that container I'm also going to turn at this border back on just so we can really visualize what's happening while we're playing with this so I'm gonna save that for the moment we're going to come and take a look at it and you can see when I play with the screen size were set at 620 pixels so it's working at big screens but if we go smaller all of a sudden I run into this problem where I have side scrolling so I get a scroll bar to go left and right because my screen is too small for the content that's in there my content is wider than my viewport width and that causes the side scrolling issue so if I was gonna open this page on a cell phone that would it just wouldn't work right I would get this issue I wouldn't be able to see it I get this weird problem there that I'm not even going to worry about right now um it just does not look good so how can we fix it as I mentioned we want to change this from a set width to a percentage so what I want you to do right now is change this over to a percentage it has to be 100% or less and I would go with less than 100% it would defeat the purpose of the container if not try different sizes see what they look like play around with the screen size in the player here and then once you're happy and you sort of see what's going on I'm gonna dive in and we'll look into it in a little bit more detail great so I'm you know it's pretty evident let's say I do 50% on this I think it's obvious what's gonna happen is those containers are now 50 percent of the total screen size you might have got a little surprise with your image sticking off the side and we'll get to that in a little bit but we can see with the container now as I grow or shrink my screen that container is adjusting itself automatically which is pretty cool I think because we know the containers width is set to 50 percent I know I have 25 percent on the left there and 25 percent on the right left over so that's pretty cool that I can sort of make this container that adapts to my screen size now the problem is my image so before we see how we can actually fix that thing that's going on with the image and understanding that what I really want you to understand is what is this 50% of so if my container is 50% it's always going to be 50% of its parent so we have a few different containers here as we can see this container here is 50% of section 1 this one is 50% of my header and this one is 50% of section 2 now my header my section 1 and my section 2 don't have widths on them and block level elements by default have a width of 100% so section 1 section 2 and my header are all 100% of my screen size so that just means that my container ends up being 50% of my screen size now what would happen if I came and set a width of 500 pixels on my header can you take a guess before you come and look at what's actually happening on the page what do you think has happened to the container that was inside my header so let's go and take a look at it and you can see that I've limited my header to 500 pixels and this container is 50% of that 500 so I get 25% there 50% here and then another 25% there so this container is now smaller than this container on this side because each one is only looking at its parent and it's not looking at anything else now normally I'm not gonna have a set width on something because that makes that whole thing look pretty ugly so we're gonna get rid of that and go back and take a look at it and then we go we fix that so just remember always when you're using a percentage if that percentage is always relative to its parent but a lot of the time the parent is defaulting to a hundred percent we're gonna run into situations where maybe that's a little bit different even if you feel a little bit confused right now in the long run it does actually make our lives much much much easier I'm gonna leave this video for here so you can play around with it a little bit make sure you really understand how that's working if you're a little bit unclear try playing with the width of your header of your section one in your section two to really see how this affects it and then maybe even play around with this width as a percentage just to really make sure it's clear and then once you've figured that out in the next video we're gonna look at how we can fix that problem that's going on with the image

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 Percentages, when set on the width (which is where we'll be using them the most), are always calculated as a percentage of their parent's width. This might seem to complicate matters, but once you get used to it, it's much easier to set up our layouts and, when dealing with responsive layouts, it's an essential unit to understand.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Scrimba · Scrimba · 47 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
45 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
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

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →