A CSS Unit Deep Dive - Learn CSS Units & When To Use Them

Slaying The Dragon
18 Dec 202215:13

Summary

TLDRThe video script delves into the intricacies of CSS units, highlighting the use and misunderstandings surrounding the pixel unit. It explains that the pixel's size varies based on the device's expected viewing distance, making it a relative unit. The script contrasts this with relative units like 'em' and 'rem', which are tied to font size, and 'vw'/'vh', which relate to the viewport. It also touches on the 'ch' unit for setting max-width based on character width, emphasizing best practices for web design and accessibility.

Takeaways

  • ๐Ÿ“ There are over 40 units in CSS, but only a few are commonly used in web design.
  • ๐Ÿ”— CSS linked units are categorized into absolute (e.g., cm, mm, in) and relative (e.g., em, rem, vw) units.
  • ๐Ÿ“ฑ The pixel unit is often misunderstood; it's an absolute unit but its size varies based on the device's screen resolution and expected viewing distance.
  • ๐ŸŽฏ The reference pixel is the basis for the CSS pixel unit, defined as the visual angle of one pixel at a certain device pixel density and viewing distance.
  • ๐Ÿ“ Absolute CSS units like inches are not consistent with their real-world counterparts due to the anchor unit concept.
  • ๐Ÿ”„ The 'm' unit is relative to the font size of the element or its parent, making it flexible for sizing but potentially complex in nested structures.
  • ๐Ÿ”„ The 'rem' unit is relative to the root element's font size, simplifying the cascading effect of font sizes in nested elements.
  • ๐Ÿ“ Percentages are always relative to their parent element's dimension, making them useful for responsive design within a container.
  • ๐ŸŒ Viewport units (vw and vh) are relative to the viewport's width and height, respectively, allowing elements to scale with the size of the viewport.
  • ๐Ÿ”ข The 'ch' unit measures the width of the character '0', useful for setting max-width based on character count for readability.
  • ๐Ÿšซ Using pixels for font sizes can override user accessibility settings, so relative units like 'rem' or 'em' are preferred for better accessibility.

Q & A

  • How many different units are mentioned in the CSS specification?

    -There are at least 40 different units mentioned in the CSS specification.

  • What are the two types of linked units in CSS?

    -The two types of linked units in CSS are absolute and relative units.

  • Which unit among the absolute CSS link units should be used in web design and development?

    -The pixel unit should be used in web design and development among the absolute CSS link units.

  • What is the main difference between absolute and relative CSS length units?

    -Absolute CSS length units provide a precise and fixed size, while relative CSS length units are relative to either a font size or the size of the viewport.

  • What are some of the misunderstandings about the pixel unit in CSS?

    -Some misunderstandings about the pixel unit include thinking it's a non-linear angular measurement, relative to the pixel density of the device, or relative to the expected viewing distance of the device.

  • What is the anchor unit in CSS, and how does it affect the output of measurements?

    -The anchor unit in CSS is either a physical unit for print or a pixel for screen media. It affects the output of measurements by making them dependent on the expected viewing distance of the device or the resolution of print.

  • What is the problem with using pixel values for font size and how does it affect accessibility?

    -Using pixel values for font size can override the default browser settings and ignore the accessibility preferences of users who have modified the settings, potentially affecting a large user base.

  • How do the 'm' and 'rem' units solve the accessibility issue with pixel units?

    -The 'm' and 'rem' units are relative to a font size, which allows them to adjust according to the user's preferences and the parent element's font size, thus improving accessibility.

  • What is the main advantage of using the 'rem' unit over the 'm' unit for font sizes?

    -The main advantage of using the 'rem' unit over the 'm' unit for font sizes is that it solves the cascading problem of font sizes being dependent on a chain of parents, making it easier to manage and track the size of elements.

  • How do viewport units like 'vw' and 'vh' differ from percentages?

    -Viewport units like 'vw' (viewport width) and 'vh' (viewport height) are relative to the size of the viewport, whereas percentages are relative to the size of their parent element.

  • What is the CH unit in CSS and when is it useful?

    -The CH unit in CSS measures the width of the character '0' in the font. It is useful for setting a max-width on a paragraph to ensure readability and to adhere to the web design rule of thumb for line length.

Outlines

00:00

๐ŸŒŸ Understanding CSS Units and the Pixel Unit

This paragraph discusses the variety of units available in CSS, highlighting that while there are over 40 different units, many are either not used or rarely used in web design. It explains the difference between absolute and relative CSS length units, emphasizing that only a few are commonly used. The pixel unit, despite its common usage, is described as the most complex due to various misconceptions about its nature. The pixel unit's relationship with physical measurements and device pixel density is explored, clarifying that it is a relative unit tied to the expected viewing distance of the device. The paragraph concludes by explaining the anchor unit concept in CSS, which determines how absolute units are interpreted based on the output medium (print or screen).

05:02

๐Ÿ“ CSS Units: m, RAM, and their Application

This section delves into the m (em) and RAM (rem) units, which are relative to font size. It explains how setting the font size to 16 pixels in CSS does not change the default browser font size, which is also 16 pixels. The paragraph highlights the issue of accessibility when using pixel values for font size, as it overrides user preferences. It introduces the m and RAM units as solutions to this problem, with m being relative to the parent element's font size and RAM being relative to the root element's font size. The paragraph also discusses the challenges of using m units for padding, margins, and widths, and how RAM units solve the cascading issue of font sizes depending on a chain of parents.

10:03

๐ŸŽจ Applying CSS Units to Design Elements

This paragraph focuses on the practical application of CSS units, particularly percentages, VW (viewport width), VH (viewport height), and CH (character width). It explains how percentages are relative to the parent element and demonstrates this with an example of a container's width relative to its parent header's width. The paragraph then introduces viewport units, which are relative to the viewport's dimensions, allowing for responsive design. The CH unit is also discussed, showing how it can be used to set a maximum width for text based on the character '0', improving readability and aesthetics in web design.

15:05

๐Ÿ™ Conclusion and Call to Action

In conclusion, the paragraph emphasizes the importance of understanding the various CSS units and their appropriate use cases. It suggests that while some units are rare and can be learned on an as-needed basis, having a solid grasp of the main units is crucial. The video ends with a call to action, encouraging viewers to like and subscribe to the channel for more content.

Mindmap

Keywords

๐Ÿ’กCSS specification

The CSS specification refers to the set of rules and guidelines that define the syntax and behavior of Cascading Style Sheets (CSS), which is used for styling web pages. In the video, the speaker mentions that there are over 40 different units in the CSS specification, highlighting the complexity and variety of options available for web design and development.

๐Ÿ’กAbsolute CSS units

Absolute CSS units are units of measurement that provide a precise and fixed size for elements they are applied to, regardless of the user's device or settings. Examples include centimeters, millimeters, and pixels. The video emphasizes that among these, the pixel unit is the most commonly used in web design due to its consistency across devices.

๐Ÿ’กRelative CSS units

Relative CSS units are units of length that adjust based on either the font size or the size of the viewport. These include em, rem, and percentages. The video explains that these units are essential for creating scalable and responsive designs that adapt to different screen sizes and user preferences.

๐Ÿ’กPixel unit

The pixel unit in CSS is often misunderstood as it is defined as an absolute unit but behaves like a relative unit depending on the device's screen resolution and expected viewing distance. The video clarifies that the pixel unit is a measurement of length relative to the expected viewing distance, making it consistent across devices despite varying physical sizes.

๐Ÿ’กReference pixel

The reference pixel is a concept in CSS used to define how the pixel unit should be interpreted on different devices. It is based on the visual angle of a pixel on a device with a specific pixel density and viewing distance. The video explains that the reference pixel is what makes the pixel unit's size relative to the device's expected viewing distance.

๐Ÿ’กm unit

The m unit (em) in CSS is a relative unit that scales based on the font size of the element it is applied to or its parent if not directly assigned. It is useful for creating scalable designs but can lead to complexity if multiple elements depend on each other's font sizes. The video advises using the rem unit for font sizes instead of the m unit to avoid such issues.

๐Ÿ’กrem unit

The rem unit (root em) is a relative unit in CSS that scales based on the font size of the root element (html) rather than the immediate parent. This unit simplifies scaling and avoids cascading font size dependencies, making it preferable for font size declarations over the m unit.

๐Ÿ’กViewport units

Viewport units in CSS, such as VW (viewport width) and VH (viewport height), are relative units that scale based on the dimensions of the viewport. These units are useful for creating responsive designs that adapt to the size of the user's screen, ensuring that elements are sized appropriately regardless of device.

๐Ÿ’กPercentages

In CSS, percentages are used to define sizes relative to the parent element's corresponding property. They allow for flexible layouts that adjust based on the parent's dimensions. However, percentages can also be relative to the viewport when used for widths and heights, as explained in the video.

๐Ÿ’กCH unit

The CH unit in CSS measures the width of the character '0' in the used font. It is a useful unit for setting maximum widths for text elements to ensure readability and to adhere to best practices for line length in web design.

๐Ÿ’กAccessibility

Accessibility in web design refers to making content and interfaces usable for as many people as possible, including those with disabilities. The video emphasizes the importance of not using pixel values for font sizes to respect user preferences and accessibility settings, which can be altered for various reasons, including visual impairments.

Highlights

There are over 40 different units in the CSS specification, but many are rarely used or should not be used at all in web design.

CSS has two types of linked units: absolute and relative. Absolute units include centimeters, millimeters, quarter millimeters, inches, points, and pixels.

Among the absolute units, only the pixel unit should be used in web design and development.

Relative CSS length units are relative to either font size or the size of the viewport, and you only need to know about 10 of them.

The pixel unit in CSS is often misunderstood and is actually the most complicated unit.

The CSS pixel unit is relative to the expected viewing distance of the device being used, which explains why the same number of pixels can represent different physical sizes on different devices.

The reference pixel is a concept in CSS that relates to the visual angle of a pixel on a device with a specific device pixel density and viewing distance.

The 'm' unit in CSS is relative to the font size of the element it is assigned to, unless it is assigned to the font size property, in which case it is relative to the font size of the parent.

The 'rem' unit is relative to the font size of the root element, making it a more consistent unit for font sizes compared to 'm'.

The 'ch' unit measures the width of the character '0' in the font, which can be useful for setting max-width in a way that accommodates a typical character length.

Percentages in CSS are relative to their parent element's dimension.

Viewport units like 'vw' and 'vh' are relative to the viewport's width and height, respectively.

The pixel unit's complexity comes from its dependence on the reference pixel, which is based on the device's expected viewing distance.

Using 'rem' for font sizes avoids the cascading issue that 'm' introduces, where font sizes are dependent on a chain of parents.

The 'm' unit is particularly useful for paddings, margins, and widths when not assigned to the font size property, as it becomes relative to the font size of its own element.

When setting font sizes, it's generally recommended to use 'rem' units and avoid 'm' units for better accessibility and consistency.

The 'rem' unit solves the problem of font sizes depending on each other, which can make tracking the actual size of elements difficult.

For setting max-width, the 'ch' unit is preferred over 'm' or 'rem' because it aligns well with the recommended character length per line in web design.

Transcripts

play00:03

there's at least 40 plus different light

play00:05

units in the CSS specification and

play00:08

initially I was going to go over

play00:09

literally each one of them until I

play00:11

realized how silly of an idea that was

play00:13

the truth is many of these units should

play00:15

either not be used at all or are rarely

play00:17

used for web design for example in CSS

play00:20

there are two types of linked units

play00:21

absolute and relative the absolute CSS

play00:24

link unit is a unit of length that

play00:26

provides a precise and fixed size for

play00:29

the element they're applied to these

play00:30

units include centimeters millimeters

play00:33

quarter millimeters inches because

play00:36

points and pixels but out of all of them

play00:38

only the pixel unit should be used in

play00:40

web design and web development on the

play00:43

other hand a relative CSS length unit is

play00:45

a unit of length that is relative to

play00:47

either a font size or relative to the

play00:50

size of the viewport these units include

play00:52

the following but in most cases you're

play00:54

only ever going to need 10 of them and

play00:56

out of these 10 units only four of them

play00:58

are actually units you absolutely need

play01:00

to know this is not to say that the

play01:02

other units are useless because they're

play01:04

not but they're not frequently used in

play01:06

practice and can be learned in an

play01:08

eternal basis

play01:12

you might think that the pixel unit is

play01:14

the easiest to understand but actually

play01:15

it's the most complicated unit of all

play01:17

you see there's a lot of confusion about

play01:19

the pixel unit some people think it's a

play01:21

measurement that never changes some

play01:23

people think it's a distance measurement

play01:24

some people think it's a relative unit

play01:27

despite being defined as an absolute

play01:29

unit some people think it's somewhere in

play01:31

the middle and refer to it as a hybrid

play01:33

of both absolute and relative some

play01:35

people think it's a non-linear angular

play01:37

measurement or in other words uses

play01:39

mathematics under the hood and some

play01:41

people think it's relative to the pixel

play01:43

density of the device While others think

play01:46

it's relative to the expected viewing

play01:47

distance of the device well which one is

play01:50

it the truth is if you read one article

play01:52

it'll tell you one thing and present the

play01:54

information as being objectively true

play01:56

then you'll read another article that

play01:58

contradicts the previous article whilst

play01:59

simultaneously presenting the

play02:01

information as being objectively true

play02:03

however here's some non-subjective

play02:05

objective facts about the CSS pixel unit

play02:08

on my 27-inch 1080p monitor I have a box

play02:11

with a width of 96 pixels and a height

play02:14

of 96 pixels if I take a ruler and

play02:17

measure the width of my box I should get

play02:19

2.54 centimeters because 2.54

play02:22

centimeters equals one inch but as you

play02:25

can see on my screen it's actually

play02:26

almost 3 centimeters then on my phone

play02:29

which surprisingly has a 1440p

play02:31

resolution the size is 1.4 centimeters

play02:34

on my 27 inch 1440p monitor the size is

play02:38

2.2 centimeters and finally for the

play02:40

cherry on top of the cake we have my

play02:42

television with a size of well I don't

play02:45

even know what the size of my television

play02:46

is something above 50 inches I think but

play02:49

anyways the size of the box is 5.5

play02:51

centimeters so we've got a bunch of

play02:54

different sizes for the same box well

play02:56

why why is one CSS inch not actually one

play02:59

inch when I measure it and why am I

play03:00

getting different sizes for the same box

play03:02

on different devices well first of all

play03:04

it's important to understand that the

play03:06

CSS absolute units aren't necessarily

play03:08

the same as the real world counterparts

play03:10

for example one inch and and the real

play03:13

world equals 2.54 real world centimeters

play03:15

however in CSS although one CSS inch

play03:18

equals 2.54 CSS centimeters the output

play03:22

for these units are totally dependent on

play03:24

something called The Anchor unit if the

play03:27

output is for print or a similar

play03:29

resolution to print then a physical unit

play03:31

is the anchor unit in other words CSS

play03:34

will output measurements equal to those

play03:36

of the real world so one CSS inch but

play03:39

for print would be the same as one inch

play03:41

as measured with a ruler across your

play03:43

printed paper but if the output is for

play03:45

screen media including high resolution

play03:46

devices low resolution devices and

play03:49

devices with unusual viewing distances

play03:51

pixel is the anchor unit in this case

play03:54

where pixel is the anchor unit its

play03:56

measurement will be dependent on

play03:57

something called the reference pixel

play03:59

unfortunately the official explanation

play04:01

of the reference pixel on the w3c CSS

play04:04

specification is what leads to literally

play04:07

all of the confusion related to the

play04:09

pixel unit the official explanation is

play04:11

as follows and and I quote the reference

play04:14

pixel is the visual angle of one pixel

play04:16

on a device with a device pixel density

play04:18

of 96 DPI and a distance from the reader

play04:22

of an arm's length for a nominal arm's

play04:24

length of 28 inches the visual angle is

play04:27

therefore about

play04:29

0.0213 degrees for reading at arm's

play04:32

length one pixel does corresponds to

play04:33

about 0.26 millimeters or 196 of an inch

play04:37

this explanation is what led a lot of

play04:39

people to believe that the pixel unit is

play04:41

a non-linear angular measurement but we

play04:44

can see that this is not the case with

play04:45

the following paragraph and I quote the

play04:48

image below illustrates the effect of

play04:50

viewing distance on the size of a

play04:51

reference pixel a reading distance of 71

play04:54

centimeters or 28 inches results in a

play04:57

reference pixel of 0.26 millimeters

play04:59

while a reading distance of 3.5 meters

play05:02

or 12 feet results in a reference pixel

play05:04

of 1.3 millimeters notice how the

play05:07

reference pixel is referred to in

play05:09

millimeters rather than radians degrees

play05:11

or grads and how the reference pixel

play05:13

changes with viewing distance whereas

play05:15

the angle would be constant the

play05:17

reference pixel in more simpler terms is

play05:19

a pixel that looks exactly the same in

play05:21

all viewing situations relative to the

play05:23

expected viewing distance of a device

play05:25

this is why I had different measurements

play05:27

across the different devices I measured

play05:29

because they all had different expected

play05:31

viewing distances the expected viewing

play05:33

distance for my television substantially

play05:35

surpasses the expected viewing distance

play05:37

on my monitor and although the Box had a

play05:40

different physical measurement the Box

play05:41

actually looked exactly the same

play05:43

relative to the size difference of both

play05:45

the TV and the monitor this standard is

play05:47

beautiful because it creates consistency

play05:49

across all devices regardless of their

play05:51

size and pixels per inches so in summary

play05:54

the pixel unit is a measurement of

play05:56

length that is relative to the expected

play05:57

viewing distance of the device being

play05:59

used and if none of this made any sense

play06:01

that's okay like I said it's one of the

play06:03

most complicated unit of all but the

play06:05

beautiful thing about it is you don't

play06:07

need to understand it to use it when

play06:08

pixel will always be the smallest width

play06:10

to make a line visible on on the

play06:12

specific device and that's really all

play06:14

you need to know to use it

play06:18

now that we understand how the pixel

play06:20

unit works we can finally move on to the

play06:22

m and RAM unit but first we need to

play06:24

understand what problem they actually

play06:26

solve in the real world in my HTML all I

play06:28

have is a container with an unordered

play06:30

list of four list items then in my CSS I

play06:32

can select the unordered list and give

play06:34

it the font size property when I set the

play06:36

font size to 16 pixels we see nothing is

play06:39

happening and if I comment it out we see

play06:41

we still have the same font size the

play06:42

reason this is happening is because 16

play06:44

pixels is the default font size of all

play06:47

of the popular browsers including Chrome

play06:49

which happens to be the one that I'm

play06:50

using we can actually see that this is

play06:52

the case when I open my settings on my

play06:54

browser go on the appearance Tab and

play06:56

find the customize fonts button we see

play06:58

that the default font size is set to 16

play07:00

pixels and as it happens two percent of

play07:03

users change this value for

play07:05

accessibility reasons and when it comes

play07:07

to the internet three percent is

play07:08

probably a few million people the reason

play07:10

I'm showing you this is look at my

play07:12

unordered list changing sizes as I

play07:14

change the default font size on my

play07:16

browser you can see it grows and shrinks

play07:18

as I change the default font size on my

play07:20

browser however back in my CSS file if I

play07:23

were to explicitly set the font size on

play07:25

my unordered list the 16 pixels and try

play07:27

changing the default font size of the

play07:29

browser we see it's not working this is

play07:31

one of the pitfalls of using the pixel

play07:33

unit anywhere and on any property some

play07:35

people make the mistake of adding a

play07:37

pixel font size to the HTML element to

play07:39

overwrite the default not knowing that

play07:41

they're actually screwing with the

play07:42

accessibility preferences of potentially

play07:44

three percent of their user base setting

play07:47

a pixel font size on the HTML makes your

play07:49

entire website ignore the preferences of

play07:52

people that took the time to modify the

play07:54

setting big disrespect don't do this but

play07:56

this is where the relative units come

play07:59

into the mix unlike the pixel unit which

play08:01

is an absolute unit the m and RAM units

play08:04

are categorized as being relative units

play08:06

that are relative to a font size in the

play08:08

case of the munit it is relative to the

play08:10

font size of the element it is assigned

play08:12

to unless it is assigned to the font

play08:14

size property in which case it will be

play08:16

relative to the font size of the parent

play08:17

so for x example when I set the font

play08:19

size on the unordered list to 1M we see

play08:22

nothing happened and this is because

play08:24

over on the HTML the 1M that is assigned

play08:26

to the font size of the unordered list

play08:28

is asking the parent container for its

play08:30

font size well I didn't set a font size

play08:32

on the container so it asks the next

play08:34

parent but again I did instead of font

play08:36

size on the body and after the body

play08:38

there is no other parent to ask so it

play08:40

defaults to being relative to the font

play08:42

size of the root element which is 16

play08:44

pixels by default however if I select

play08:46

the container and give it a font size of

play08:48

32 pixels for example now 1M on the UL

play08:51

element is relative to the font size of

play08:53

the container because the container is

play08:55

the closest parent with a font size

play08:56

defined we see the font size on our list

play08:59

items are now way bigger but earlier I

play09:01

said using pixel values is problematic

play09:03

for accessibility reasons so what I can

play09:05

do is replace the 32 pixels with 2m and

play09:09

we see we get the same result the reason

play09:10

we get the same result is because on the

play09:13

container the munit is asking the parent

play09:15

of the container for its font size the

play09:17

body doesn't have a font size and

play09:18

because there is no other parent after

play09:20

the body it defaults to being relative

play09:21

to the root element which has a font

play09:23

size of 16 pixels by default also we set

play09:26

the M unit to 2 which means it's 16

play09:28

pixels times 2 which is 32 pixels the

play09:31

same value we had before when I try to

play09:33

change the default font size in my

play09:35

browser settings we see our list items

play09:37

are growing and shrinking just as

play09:39

expected but although the munit fixed

play09:41

the issue we had with accessibility it

play09:43

introduced another problem in our code

play09:45

imagine I give the body a font size of

play09:47

1.5 M we see our items increased in size

play09:50

now imagine I also add a font size of 2m

play09:53

on the list elements we see our items

play09:55

grew in size again the issue isn't with

play09:58

the size of our items but with the fact

play10:00

that we've got four font sizes that are

play10:03

depending on each other and this makes

play10:05

it super difficult to keep track of the

play10:07

actual size of each element so for

play10:09

example what does 2m represent in our

play10:12

list element well we have to look at the

play10:14

parent but the parent also uses the

play10:16

munit so we have to check the parent of

play10:18

the parent while the parent of the

play10:20

parent also uses the M unit so we have

play10:23

to ask the parent of the parent of the

play10:25

parent and you get my point this is

play10:27

crazy and you shouldn't be doing this

play10:29

instead what we can do is replace

play10:32

everything that has the M unit with the

play10:34

REM unit and now we see our items all

play10:36

have a more reasonable size the way the

play10:38

ram unit works is instead of being

play10:40

relative to the font size of the parent

play10:41

it is being relative to the font size of

play10:43

the root element the root element is the

play10:45

HTML element and the HTML element has a

play10:48

default of 16 pixels using the ram unit

play10:51

solved the cascading problem that we had

play10:53

with the munit where each font size was

play10:56

dependent on a chain of parents and as a

play10:58

general rule whenever a set of value on

play11:00

the font size I always use Rams I never

play11:02

use the M unit for font sizes but that

play11:05

doesn't mean that I never use it on the

play11:06

HTML I'm going to remove the unordered

play11:08

list and replace it with a button and

play11:10

give it the class of button then on my

play11:12

CSS I'll remove everything and select

play11:14

the button by its class name I'll give

play11:16

it a background color of red a color of

play11:18

white and I'll give it a font size of

play11:20

1.5 run our button needs some padding

play11:23

earlier I said that the M unit is

play11:24

relative to the font size of the element

play11:26

that is assigned to unless it is

play11:28

assigned to the font size property in

play11:30

which case it'll be relative to the font

play11:31

size of the parent this means that when

play11:33

I use the munit on the font size

play11:36

property it becomes relative to the

play11:38

parent element however if I give a

play11:40

padding to my button of 1M because 1M

play11:42

isn't on a font size it is then relative

play11:45

to the font size of its own element this

play11:47

is where the M unit really shines it's

play11:49

an amazing unit to use on paddings

play11:51

margins and widths because when it isn't

play11:53

assigned to the font size property it

play11:55

becomes relative to the font size of its

play11:57

own element and we can see that this is

play11:59

the case when I change the font size on

play12:01

my button we see the padding is always

play12:03

being relative to the font size of the

play12:04

button itself this means we don't really

play12:07

need to worry about the padding ever

play12:08

again when we change the font size on

play12:10

our button

play12:13

in this chapter we're going to be

play12:15

looking at percentages and the two main

play12:18

viewport units in my HTML all I have is

play12:20

a header with the class of header a div

play12:22

with the class of section container and

play12:24

nested inside the div is an H1 heading

play12:26

with the class of header title we're

play12:28

going to be creating a header so in my

play12:30

CSS I'll select the header by its class

play12:31

name and give it a background color of

play12:33

light blue a color of white and a

play12:35

padding of one amp for the top and

play12:37

bottom and zero for the left and right

play12:39

I'll also select the H1 by its class

play12:42

name and give it a font size of 2.5 RAM

play12:44

and a margin of zero then I'll select

play12:47

the container by its class name and give

play12:48

it the border of 3 pixel solid red just

play12:51

so we can see it and a width of 80

play12:53

percent you see our border shrunk in

play12:55

size if I comment out the width for one

play12:58

second we see the Border covers the

play12:59

entire width and this is because

play13:01

percentages are relative to their parent

play13:03

the parent of the section container is

play13:05

the header and the header is a block

play13:07

level element so it covers 100 of the

play13:09

width of the page when I uncomment the

play13:12

width on our section container it is

play13:14

relative to the width of the parent

play13:16

header and we can actually test this by

play13:18

adding a width of 50 on the header now

play13:20

the header is half in size and we see

play13:22

the section container's width of 80 is

play13:25

80 relative to the width of the parent

play13:27

header percentages are always relative

play13:30

to their parent however if you don't

play13:32

want the width to be relative to the

play13:33

parent you can use the VW unit for

play13:36

example if I replace 80 percent with 80

play13:38

VW we see our border now covers 80

play13:41

percent of the viewport and this is

play13:43

because the VW unit is a view per unit

play13:46

and is relative to the viewport width

play13:48

there's also the VH unit it's the same

play13:51

as VW but it's relative to the viewport

play13:53

height instead I'll comment out the

play13:55

width on our section container and add a

play13:57

height of 100 VH on the header we see

play13:59

this covers the entire viewport height

play14:01

and if I set it to 50 VH it'll cover

play14:03

half of the viewport height use

play14:05

percentages when you want to size

play14:07

relative to the parent and use viewport

play14:09

units when you want to size relative to

play14:11

the viewport

play14:15

the last unit I want to talk about is

play14:17

the CH unit this unit is pretty simple

play14:20

all it does is measures the width of the

play14:22

character 0 on your font the reason this

play14:25

is useful is because we can use it to

play14:26

set a Max width on a paragraph and the

play14:29

reason you would want to do that is

play14:30

because on this example we see my text

play14:33

is super long it's ugly to look at and

play14:35

is hard to read to fix it I can select

play14:37

it in the CSS and give it a Max width of

play14:40

60 CH we see this looks a lot better and

play14:43

the reason this works so well over using

play14:45

M's or Rams for setting a Max width is

play14:47

because in web design as a rule of thumb

play14:49

your paragraph should have a length per

play14:51

line of around 45 to 75 characters and

play14:54

using the CH unit makes the finding a

play14:56

specific Max character length super easy

play14:58

and there you have it hopefully now you

play15:01

have a better understanding of the units

play15:03

we talked about and for all the other

play15:04

units they are super rare and can be

play15:06

learned in a need to know basis if you

play15:08

enjoyed the video please consider liking

play15:10

the video and subscribing to the channel

play15:12

thanks for watching

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
CSS UnitsPixel UnitRelative UnitsWeb DesignResponsive DesignViewport UnitsAccessibilityFont SizeLayout