CSS Tutorial For Beginners 42 - Width & Height
6K views
Feb 7, 2024
CSS Tutorial For Beginners 42 - Width & Height
View Video Transcript
0:00
Hey guys, what's going on? You're watching CSS for Beginners Lesson 42, and in this video
0:11
we're going to talk about the width and height properties in CSS. Alright, so so far in this course we've talked about the box model quite a lot, and we've
0:20
said that the box model controls the spatial properties of block level elements, and we've
0:25
also talked about what block level elements are. So we've discussed the margin, we've
0:29
discussed padding, and we've talked about borders as well. But we've not really talked
0:34
about the width and height properties in any great detail, so that's what we're going to
0:38
take a look at today. Okay, so I'm back here in the code now, and as you can see I've added these four div tags
0:45
right here, and the first one has got a class of static width, the second one has a class
0:49
of percentage width, and the bottom two here have classes of inline block. I've also added
0:55
a selector up here which is going to target all of these divs, it's going after all of
0:59
the divs within the main content, remember the main content is here, and it's going after
1:03
all of these divs within that. And it's giving those a background colour of a light grey
1:07
and it's giving them a margin bottom of 20 pixels. Remember the way this margin shorthand
1:11
works is top, right, bottom, left. Okay so let's take a look at what this looks like in a browser before doing anything else
1:24
Okay then, so there they are, and we've got our four divs within this parent element
1:28
and they're all taking up 100% width of the parent element, minus this area here, which
1:34
is the padding on the parent element. So remember, all block level elements automatically take
1:39
up 100% of the parent element's width, so that's why they're doing that
1:44
So what we're going to do now is take a look at the width and height properties in the code, and mess around with those a little bit
1:52
And the first thing I'm going to do is go after this static width div here. So to do
1:56
that I'll do my class selector, start it with a period, and then say static width, and then
2:02
we'll give it some values. Now the width property is just width, and by static width I mean
2:08
we're going to give it a pixel value, and we'll just say 300 pixels. And that's static
2:13
because no matter how big your browser is, or how big the parent element is, we're saying
2:17
to this one here, it's always going to be 300 pixels. And we're going to do the same
2:22
for the height, and we'll just give that a height of 100 pixels. So let's view this in
2:29
a browser now, and see what it looks like. Alright, cool, so there it is, it's a width
2:36
of 300 pixels and a height of 100 pixels. So now we're going to take a look at percentage
2:43
width. And to do that we'll go after our percentage width class here, and we'll give
2:51
it a width property, and this time I'm going to say 70%. So what that there is saying is
2:58
whatever width the parent element is, I want this div to be 70% of that width. And then
3:06
we'll just give it a height of 50 pixels. So the height is always going to be static
3:10
but the width is going to be dynamic depending on how wide the parent element is. So let
3:15
me show you that in a browser. Okay, so here now you can see this grey box is about 70
3:27
the width of this white box, which is good because if we were designing for a mobile
3:32
screen then maybe the browser and the white box would be smaller. And now you can see
3:38
here as I increase and make this window smaller, that this white box is getting smaller, and
3:48
subsequently this grey box is getting smaller because this grey box is always 70% of this
3:54
white box. So this is really good for mobile devices. If you're designing a website that's
4:01
going to scale down to mobiles, then it's really good to work with percentages because
4:06
whereas this 300 pixel block you can see now is creeping over the edge, this 70% width
4:13
one here is always 70% the width of this parent element. So it's never going to do this. So
4:20
that's really cool. So now let's look at these two here, which I've given a class of inline
4:26
block to, so we'll tag it both of those. And this time what I'm going to do is say give
4:31
them a width of 40%. And we know that block level elements stack on top of each other
4:39
Now in this case I want these to stack side by side, and we know to do that we have to
4:43
give it those inline properties because inline elements stack side by side. However, if we
4:49
were to say display inline, we know that that will take away the box model properties. We
4:56
won't have full control over those because the box model only controls block level elements
5:02
So what we need to do is say inline block to get the best of both worlds, remember
5:07
So we're getting that side to side property we get with inline elements, but we're also
5:11
getting the block level element properties, which is going to let us control the box model
5:15
properties, things like margin and padding. So let's save that now and view this in a
5:22
browser. Cool, and there we go. Now we have a 40% width here for this one, and right next
5:33
to it another one which is 40% in width too. And same again, if we inspect the elements
5:41
and scroll this in and out, or rather zoom it left and right, we can see them getting
5:46
smaller and getting larger, dependent on how large this parent element is
5:52
So that about covers everything we need to talk about when it comes to width and height
5:58
If you have any questions whatsoever, feel free to throw a comment down below. I'll answer
6:02
all of those as soon as possible. Otherwise, if you enjoy these videos, please like, subscribe
6:07
or share, and I'll see you guys in the next one