new course
CSS grid landing page tutorial (36min)

Lesson info

Lesson info

In web design, you can declare an element's width and/or height using either fixed values or relative values. This video covers the most commonly used dimensions (outside of ems):

  1. Auto-sizing (auto)
  2. ‍Pixels (px)
  3. ‍Percentage (%)
  4. ‍Viewport height (vh)
  5. ‍Viewport width (vw)
  6. ‍Minimum (min) and maximum (max)

You can adjust an element's width and height in the Styles Panel, under the Layout section. We’ll be covering how each of these units work in the context of setting element dimensions.

Clone this projectDownload project assetsDownload lesson assets

Transcript

An element's Dimensions can often be defined by values related to width and height. In many cases, the Dimensions of an element can be set by content inside of that element. But with block elements, we can set our width and height manually.

And to cover this, we want to touch on five different types of values: Pixels, Percentages, VH (viewport height), VW (a German automaker), and finally Minimums and Maximums.

Let's start with Pixels. Here's a div block with some content inside. And sometimes we might want to set one or both of our Dimensions to a set number of Pixels. Let's say 800 Pixels. Type it in, hit Enter. And it's exactly as we want it. 800 Pixels. But what happens when we go down and look at this on smaller device widths? It's still set to 800 Pixels, but 800 Pixels can completely go off the screen on some smaller devices. So there are times when using Pixel values — especially on larger elements — can be rigid.

How does this differ from percentages? Let's go back and select the same div block, this time, let's set a percentage. We'll do 90%. This means the div block is taking up 90% of its parent. How does that translate on other device widths? It's working fine. Because it's taking up 90% of its parent. So as the width of its parent changes, so too does the div block we've set.

And what about VH? This is a common way to set an element's vertical Dimension relative to the height of the viewport. In other words, 90vh? That's 90% of the viewport. 100vh? That's 100% of the viewport. 120vh? That's 120% of the viewport.

And if VH if viewport height, VW is viewport width. And here’s a cool way to demonstrate this:

Two sections. They both have images inside. We're going to take our first image (the top section) and set it to 50% width. And in our bottom section, we'll set the image to 50vw.

And if you just glance at this, they look the same. But if you look really closely, they still look the same. That's because the width is calculated to be the same thing.

Let's grab our top section and set the width to 1000 Pixels. Remember: we set the first image to 50% width. So naturally, it's taking up 50% of that 1000-Pixel value (500 Pixels).

But let's do the same thing on the bottom section. If you look really closely this time, you'll see it doesn't matter. The image doesn't change, because our viewport width didn't change.

If we go in and adjust our viewport width, we can see that the image set to 50vh (the bottom one) respects the width of the viewport.

That's VW.

Minimums and Maximums let us add a floor or ceiling to a value. Maybe we want our section to be 100vh, the full height of the viewport. But, what if the viewport isn’t tall enough to fit the content? Now, our content is crushed or completely cut off. Instead we can clear the 100vh height, and set it to 100vh minimum. A minimum height. Now the section will never have a height less than the viewport, but it will still respect the content inside. Because we are only setting the minimum.

Same with maximum. Let’s go back to our first example from a couple minutes ago, when we set the width to 800 pixels, it overflowed when the viewport got too narrow. But if we set a maximum of 800 pixels, it will never get wider than 800 and it’ll still respect the other values. It’s actually even more powerful than this. Minimums and maximums can be used in conjunction with other values for our dimensions, too. So we can go back and add 90% to the width, meaning this div block is always going to have a width of 90%, except in cases where 90% is greater than 800 pixels.

That’s minimums and maximums.

So, we have Pixels for set or fixed values, we have percentages as an alternative when we need flexibility, we have viewport height which lets us set heights relative to the browser's viewport, we have VW which makes the Jetta, the Golf, and owns Porsche, and we have minimums and maximums to put a floor or ceiling on Dimensions when we don't want things to go out of control.