Controlling the width and height of elements
The first task in this chapter is to define the width and height of elements. We will learn about explicit sizes, relative sizes and even learn how to control the range of sizes an element can be as the html page scales. By the end of this lesson, you will know everything about sizes of HTML elements and how to control them using CSS.
Video Transcript
00:00In the last chapter, our main focus was elements
00:04manipulating them, but really we were
00:06manipulating their exterior, if it was their border,
00:09if it was their outline, if it was their transparency,
00:12if they were visible or not visible as a whole.
00:15In this chapter, we dive deeper into the element itself
00:18and start controlling its properties.
00:20We're gonna start off with looking at the width and height
00:23and how to create a width and height that is dynamic
00:25so we could create dynamic layouts.
00:27By the end of this lesson,
00:28you will have the core tools to create elements
00:31that will resize and shape and change based on
00:34whatever the screen resolution that the user has
00:37at any given time.
00:39Let's jump right into it and learn it and do it in action.
00:45For the sake of this chapter, we split out
00:47a new little example that is really based
00:50off of our last sample.
00:52So just go ahead into your samples and just move into,
00:55and let me just push out the videos,
00:57move into site number two, all right?
01:00Now, in site number two, you're gonna find a few things.
01:02One is you're gonna find your index file,
01:05where in our index file, we have your our,
01:08just that article with a headline
01:10and a couple of paragraphs that are inside of that article.
01:15Without further ado, let's jump right in
01:17and what I wanna do is I wanna basically control
01:19the width and height of things in general.
01:23For example, if I wanted to configure the width
01:26and height of my footer and not have it be
01:30the default as it is right now,
01:32let's go ahead and change that and it's really simple.
01:34We've seen it in action previously more or less.
01:38At least we've seen parts of that logic.
01:41And I'm gonna go ahead here and just break lines here.
01:45So we have a width, this is simple enough.
01:47I could set it to be 100 pixels
01:49and I could go ahead to my height
01:51and set it to be 100 pixels as well.
01:54And maybe in a more realistic scenario,
01:56we might wanna change it to be something else,
01:59but I'm gonna go ahead and save it.
02:01I'm gonna refresh and now we have our footer.
02:05Now I could, if I'm not happy with this width,
02:07which I kinda am, maybe I'm not happy with the height,
02:10which is probably too many pixels,
02:11maybe I'll change the height to 40 pixels,
02:14but really in a real world scenario,
02:16a lot of times if you're doing
02:17and creating your design dynamically,
02:20you might go ahead to that element,
02:21inspect that element and then just go into that height
02:24and play around with the value until you're happy
02:27with the actual value.
02:28So let's say I'm happy with 20 pixels.
02:31So I'm gonna go ahead there
02:32and just configure it to be 20 pixels and that's it.
02:35And then I created my element, I'm happy with it.
02:38I have a width, I have a height.
02:39So far so good.
02:41Now let's take another look
02:42and something very common is text,
02:44if it doesn't have anything that's bounding it,
02:47will just go wild and it's really hard to read copy
02:50that is laid out so widely.
02:52It's just not natural for our eye.
02:54In general, people tend to prefer reading things
02:57that have up to 10 words per line.
03:00So in this case, I have your one, two, three,
03:02four, five, six, seven, eight, nine, 10.
03:04Really, this is where ideally I would cut off.
03:08I would not want all this extra stuff.
03:09So let's say I would cut it off
03:11maybe in this range, in this area,
03:13or really in if it was gonna take
03:14a measurement tool of my Apple here.
03:17So we're really talking about around
03:19that 460 pixels for my case and my font size
03:23would be a good size, a good legible size.
03:27Now to do that, all I really have to do
03:29is I'm gonna go ahead and add another rule
03:32beyond my article rule.
03:34I wanna add and I'm just gonna get rid of that semicolon
03:36because there's nothing after it.
03:38I'll do that also in this one
03:39because we're saving a little space.
03:41And I'm gonna go inside and say,
03:42hey, anything inside of the article,
03:44every paragraph inside of the article,
03:47what do we wanna do there?
03:48We wanna limit their width to be 500 or 450 pixels.
03:53That's all I had to do.
03:54I'm not gonna also change the height
03:56because text is fluid, it changes in size.
03:59So I probably would never control its height
04:01in that capacity.
04:03But if I go ahead there and I click on refresh,
04:05we'll see that now our copy is gonna be sitting nicely
04:08and flushly in a way that is a lot more legible
04:11than it was before.
04:12Now, there's a few more problems
04:14when it comes down to width and height.
04:17And that comes down to the fact
04:18that if I just go ahead here
04:19and I just get rid of this width by just commenting it out,
04:23one of the problems that I'm gonna have here
04:25is not really a problem,
04:27but one of the things in the natural layout
04:29is if I go ahead and I resize,
04:31notice how the copy is resizing dynamically,
04:34which is wonderful and something that many times
04:36I would want that to happen.
04:38Because if the user is resizing,
04:40I want the content to resize with them.
04:42Talking about the future of the web
04:43is designs that are not specific to a specific resolution,
04:47but designs that are built for the user,
04:50built based on whatever their resolution is.
04:53For me to be able to adhere to this,
04:55if I would go ahead and just uncomment that width again,
04:58notice what would happen by defining absolute pathing,
05:02by saying, hey, you're 450 pixels.
05:05If I resize my elements smaller than the size of the text,
05:08my text is not resizing,
05:10forcing my user to need to scroll
05:12to be able to read the content.
05:14So that's definitely not something good.
05:16A way to solve this,
05:17and there's a couple of things that we could do,
05:19is to define instead of a width that is absolute,
05:23to define basically a minimum width or a maximum width.
05:27And in this case, I wanna set a maximum width to my element,
05:31saying, hey, your content will be,
05:33the max size you will have will be 450 pixels.
05:37Now in this case, if I set it to be a maximum width,
05:40and I go ahead and I try to resize my element,
05:43when I resize my element and it reaches a position
05:46where the text will be out of bounds,
05:48the text starts resizing.
05:50Now again, under the same kind of concept,
05:53what if a copy this size is just not legible,
05:57because it's just too small,
05:58but really this is the smallest we want our copy to get.
06:02In that case, I could go ahead and just literally
06:04just figure out what is that width,
06:06in this case, it's about 400 or 370.
06:12And if it's 370 pixels, I could go ahead and say,
06:14hey, by the way, not only do I wanna limit
06:17the width of my item based on its maximum size,
06:21I could also set a minimum width to my item,
06:24setting that minimum width to be 360 pixels,
06:28making sure that if I go ahead here and I click on refresh,
06:32that when I start resizing, my copy will resize.
06:36But once I reach the limit of resize,
06:38the copy will not continue and resize.
06:41So I could that way create a gap,
06:44a range of acceptable values
06:47instead of defining an absolute value.
06:50Now by default, the value of my width
06:53is gonna be 100% of the space that I'm in.
06:56So literally the default value right here is 100%.
07:01I could have, if I wanted, defined it also relatively now,
07:04set it to be 90% if I wanted to.
07:06By setting it to 90%, I'm still adhering
07:09to the rules of max width and min width.
07:12Remember that as long as I'm defining my width
07:15in a relative, aka in percentages,
07:18then it's gonna continue working.
07:21So if I go ahead here and click on refresh,
07:23we'll see that now our copy by default
07:25is defined with a maximum because it's now in a position
07:29that is 450 pixels are a lot less than 90%.
07:34So we were basically making sure that 90%
07:37would never happen in the screen resolution.
07:39But as our screen becomes smaller,
07:42we're gonna see that we have a gap here
07:44that is being created based on that rule
07:46that we applied to that 90%.
07:48So notice in this game of combination things,
07:50but if you put inside a width that is absolute,
07:53then obviously it breaks down a lot of the logic here
07:56and then something is gonna override something.
07:59So you could play with the width, max width,
08:02min width and max width, and on top of that,
08:04you could also play just by the way.
08:05I'm gonna put here as a reference.
08:07We're not gonna get into it
08:08because it's the exact same thing.
08:09The same rules apply when it comes down to height.
08:13So you have yourself your height,
08:15you have yourself your max height, min height.
08:17So you have your min height.
08:20Let me just put that right there.
08:22Min height, max height.
08:25And obviously height.
08:27So there you go.
08:28So that is the basics of defining and shaping
08:31the size of shapes in CSS.
