CSS Units & Sizes Tutorial for Beginners

Dave Gray
22 Feb 202221:25

Summary

TLDRIn this beginner CSS tutorial, Dave explores various CSS units, focusing on pixels, percentages, and relative units like em and rem. He explains the importance of not using absolute pixel values for font sizes to maintain flexibility and accessibility, and demonstrates how to apply these units for different elements like paragraphs and headings. Dave also discusses viewport units, their use cases, and potential issues, emphasizing the importance of understanding how units affect layout and design.

Takeaways

  • 🌐 The tutorial is part of a beginner CSS series using Chrome, Visual Studio Code, and the Live Server extension for real-time web page viewing.
  • πŸ“ CSS units determine the size of elements on a webpage, with pixels (px) being the most commonly used absolute length unit, equivalent to 1/96th of an inch.
  • 🎨 Using pixels for font size is discouraged as it removes user control over default browser settings, which typically set the base font size to 16 pixels.
  • πŸ”² Setting absolute font sizes can lead to undesired effects when user preferences for larger or smaller text conflict with the CSS settings.
  • πŸ”„ Percentage units in CSS represent a fraction of another value, making them relative to the parent container's dimensions.
  • πŸ“ The 'em' unit is relative to the font size of the root element (default browser setting), and is useful for scalable font sizes and responsive design.
  • πŸ”§ Using 'em' units for margins and paddings can lead to compounding effects, causing sizes to increase based on the font size of parent elements.
  • 🎯 The 'rem' unit is recommended for font sizes as it scales relative to the root element, maintaining consistency across the webpage.
  • πŸ“ The 'ch' unit is based on the width of the character '0', useful for setting consistent widths for text elements like columns.
  • πŸ“± Viewport units (vw and vh) are relative to the viewport's width and height, with 1vw equal to 1% of the viewport's width and 1vh equal to 1% of the viewport's height.
  • 🚫 Setting body width to 100% viewport width can cause horizontal scrollbars when content overflows, whereas using percentages avoids this issue.
  • πŸ“ˆ To make the body element fill the entire page height, use 'min-height: 100vh' to ensure it covers at least the full viewport height, even if the content does not.

Q & A

  • What is the main topic of the tutorial?

    -The main topic of the tutorial is about understanding and using different CSS units for styling web pages, with a focus on beginner-level knowledge.

  • Which tools does Dave use in the tutorial?

    -Dave uses the Chrome web browser, Visual Studio Code editor, and the Live Server extension for Visual Studio Code to view and edit the web page.

  • What is the significance of pixels (px) as a unit in CSS?

    -Pixels are absolute length units in CSS, where one pixel is equal to 1/96 of an inch. It represents a single pixel on the screen, and it's the most commonly used absolute unit for specifying sizes, especially for things like borders.

  • Why shouldn't pixels be used for font sizes?

    -Pixels shouldn't be used for font sizes because it sets an absolute size that cannot be adjusted by the user in their browser settings, taking away the user's control over the default font size.

  • What is the role of percentages in CSS?

    -Percentages in CSS represent a fraction of another value, making them relative to the parent element's dimensions. They are commonly used for setting widths and heights of elements as a proportion of their containing element's size.

  • What is the difference between 'em' and 'rem' units?

    -The 'em' unit is relative to the font size of its own element's parent, while 'rem' (root em) is relative to the font size of the root element, which is typically the default browser setting. Using 'rem' ensures consistent scaling that doesn't get amplified through the DOM tree.

  • When would you use 'em' units instead of 'rem'?

    -You would use 'em' units for properties like margin or padding that should scale relative to their parent element's font size, rather than the root font size.

  • What is the viewport width (vw) and viewport height (vh) units?

    -Viewport width (vw) and viewport height (vh) are relative units where 1vw is 1% of the viewport's width and 1vh is 1% of the viewport's height. They are useful for creating responsive designs that adapt to the size of the viewport.

  • What is the issue with setting the body element to 100% viewport width?

    -Setting the body element to 100% viewport width can cause a horizontal scrollbar to appear when the content exceeds the page height, which is generally undesirable as it disrupts the user experience.

  • How can you ensure the body element is the full height of the page?

    -You can set the body element's min-height to 100vh (viewport height units) to ensure it expands to at least the full height of the viewport, while still allowing it to grow with the content if necessary.

  • What is the importance of understanding CSS units?

    -Understanding CSS units is crucial for effective web design and development as it allows developers to create responsive and adaptable layouts that cater to different screen sizes and user preferences.

Outlines

00:00

🌐 Introduction to CSS Units and Tutorial Setup

The video begins with an introduction to CSS units, setting up the Chrome web browser, Visual Studio Code editor, and the Live Server extension for viewing web pages. The tutorial focuses on the most common CSS units, using the paragraph and heading elements to demonstrate the effects of different units on font size and page layout. The importance of not using absolute pixel values for font size is emphasized to maintain user control over browser settings.

05:01

πŸ“Š Understanding Percentage and Relative Units

This paragraph delves into the use of percentage units for width, explaining how they relate to the parent element's width. The concept of relative length units is introduced, with a focus on 'em' units for font size, which are tied to the root element's font size. The potential for unintended consequences when using 'em' units is discussed, highlighting the importance of using 'rem' units for consistent sizing across the document.

10:01

πŸ”  Exploring 'em', 'rem', and 'ch' Units

The video continues with an exploration of 'em' units for font size, emphasizing their relationship to the parent element's font size. 'rem' units are introduced as a safer alternative, always referencing the root element's font size. The 'ch' unit, based on the width of the character '0', is explained as useful for defining columns in a layout. The paragraph also touches on the default styles set by the browser and how they can be overridden with custom CSS.

15:01

πŸ“ Applying Viewport Width and Height Units

This section discusses the use of viewport width (vw) and viewport height (vh) units, which are relative to the browser's viewport size. The video demonstrates how setting the body element's width to 100% viewport width units can lead to issues when content overflows the page height. A better approach is suggested, using percentage units for width and setting the body's min-height to 100% viewport height to ensure it fills the entire page vertically, even when there's not enough content.

20:02

🎨 Final Thoughts and Encouragement

The video concludes with a brief overview of using viewport height units to make the body element fill the entire page height. The speaker encourages viewers to strive for progress over perfection, emphasizing the value of making consistent, incremental improvements in coding skills. A call to action is made for viewers to like, share, and subscribe to the channel for more content.

Mindmap

Keywords

πŸ’‘CSS Units

CSS Units refer to the different types of units used in Cascading Style Sheets (CSS) to define measurements such as length, size, and font-size. In the video, the tutorial focuses on explaining the most common CSS units like pixels (px), percentages (%), ems, and viewport units, and how they are used to control the visual presentation of web elements.

πŸ’‘Chrome Web Browser

The Chrome Web Browser is a popular web browser developed by Google that is used to access and display web pages. In the context of the video, the Chrome browser is used to demonstrate how changes made in the CSS file are reflected in the browser, allowing for real-time preview of the web page's design and layout.

πŸ’‘Visual Studio Code Editor

Visual Studio Code (VS Code) is a free, open-source code editor developed by Microsoft. It is highly customizable and supports a wide range of programming languages, making it a popular choice among web developers. In the video, VS Code is used as the editing environment to write and edit CSS code.

πŸ’‘Live Server Extension

The Live Server extension is an add-on for Visual Studio Code that allows web developers to run a local development server with live reload feature. This means that any changes made to the HTML or CSS files are automatically refreshed in the browser, providing a seamless development experience without the need to manually refresh the browser window.

πŸ’‘Pixels (px)

Pixels (px) are the smallest unit of measurement on a screen, corresponding to a single dot in the display. In web design, pixels are often used as a unit of measurement in CSS to ensure precise control over the size and placement of elements. However, using pixels for font sizes can make text less adaptable to user preferences and different screen resolutions.

πŸ’‘Percentage (%)

Percentage is a CSS unit that represents a fraction or a part of another value. It is used to define the size of an element relative to its parent container or the browser window. Percentages are useful for creating responsive designs that adapt to different screen sizes and maintain the layout proportions.

πŸ’‘em

The em unit in CSS is a relative length unit that is based on the current font size of the element. It is used to create scalable and responsive designs where the size of an element is relative to its parent's font size. However, using em units for font size can lead to compound scaling if used recursively, which can cause unexpected results.

πŸ’‘rem

The rem (root em) unit is a CSS unit that is based on the font size of the root element (html), making it a scalable and flexible unit for typography. Unlike the em unit, rem sizes are not affected by the font size of the parent element, ensuring a consistent and predictable scaling of text and other elements across the document.

πŸ’‘Viewport Units

Viewport units, such as vw (viewport width) and vh (viewport height), are CSS units that define an element's size relative to the dimensions of the viewport. These units make it easier to create responsive designs that adapt to the size of the device's screen, ensuring that elements scale proportionally with the available space.

πŸ’‘CSS Box Model

The CSS Box Model is a design concept that describes how elements are laid out on a web page. It consists of the content, padding, border, and margin of an element, each of which can be controlled using CSS properties. Understanding the box model is crucial for web developers to create precise layouts and manage the spacing between elements.

πŸ’‘CSS Reset

A CSS Reset is a technique used to reduce browser inconsistencies in default styling by resetting all element styles to a common baseline. This allows developers to start with a clean slate and ensures that their own CSS rules are applied consistently across different browsers and devices.

Highlights

Introduction to CSS units, focusing on the most common ones used in web design.

Explanation of absolute length units, specifically pixels (px), and their equivalence to screen size.

Discussion on the default browser font size and its impact on setting font sizes in pixels.

Demonstration of how changing user settings in the browser affects the font size set in pixels.

Explanation of relative length units, starting with the percentage (%) and its relation to parent elements.

Example of setting the width of an element using percentages and how it relates to the parent's width.

Introduction to the 'rem' unit for font sizes, explaining its relation to the root element's font size.

Comparison between 'em' and 'rem' units, highlighting the importance of using 'rem' for consistent font sizing.

Explanation of the 'ch' unit, which is based on the width of the character '0'.

Use case of the 'ch' unit for setting the width of a paragraph based on the number of characters before wrapping.

Discussion on the 'viewport width' (vw) and 'viewport height' (vh) units, and their relation to the browser's viewport.

Practical example of setting the main element's width using viewport width units.

Explanation of the potential issue with using 100% viewport width when content overflows the page height.

Solution for making the body element full height of the page using viewport height units.

Use of the CSS 'box-sizing' property to ensure padding and border sizes are included in the element's width and height.

Demonstration of the default styles applied by the browser and how to override them using CSS.

Explanation of the CSS 'reset' technique and its purpose in web development.

Encouragement for viewers to strive for progress over perfection in their coding journey.

Transcripts

play00:00

[Music]

play00:05

hello and welcome hi i'm dave this

play00:07

tutorial is part of a beginner css

play00:09

series i'll be using the chrome web

play00:11

browser the visual studio code editor

play00:14

and the live server extension for visual

play00:16

studio code to view the web page

play00:18

there are links to these tools starter

play00:20

code files and all resources in the

play00:23

description below

play00:25

let's move on to css units units

play00:27

determine the size of everything in your

play00:29

page today we're going to cover the most

play00:31

common css units on the left i have

play00:34

visual studio code on the right i have

play00:36

the chrome browser it has an h1 heading

play00:39

on the page and we have one paragraph on

play00:42

our page and we're displaying this page

play00:44

with the live server extension so

play00:46

anytime we make a change and save our

play00:48

css file we'll automatically see the

play00:50

changes over here on the page i'm going

play00:52

to start out by just putting in a

play00:53

paragraph selector

play00:55

and then i'm going to put in the font

play00:58

size

play00:59

and after that without even typing

play01:01

anything look what visual studio code

play01:03

gives us a lot of suggestions there are

play01:06

so many different units we can choose

play01:08

from so today we'll cover only the most

play01:11

popular ones is we don't need to cover

play01:13

all of them just the ones that you'll

play01:14

see and construct your pages with most

play01:17

frequently so i'm not even going to put

play01:19

in a definition right now let's just

play01:21

delete that and switch over to the mdn

play01:24

page for values and units and we can see

play01:28

a chart here of absolute length units

play01:31

and really the only absolute length unit

play01:33

we will use commonly is pixels and you

play01:36

can see here is the equivalency it's one

play01:39

pixel is equal to 196 of an inch and

play01:42

really one pixel as you might guess

play01:44

means one pixel on your screen

play01:46

we abbreviate that with px and i say

play01:49

it's the only absolute value we're going

play01:51

to cover because as mdn says it's really

play01:54

the only value that you will commonly

play01:56

use as far as absolute units go

play01:59

so let's switch back to our page and now

play02:02

here in our css page i'll once again

play02:05

select the

play02:06

paragraph and we can set a

play02:10

absolute size to the font in our

play02:12

paragraph if we want to the default in

play02:15

any browser is usually 16 so if i set it

play02:17

to 16

play02:19

we don't see a change but if i set it to

play02:21

32

play02:23

we definitely see a change it gets twice

play02:25

as big but i don't usually want to use

play02:28

an absolute value like pixels for a font

play02:31

size and let me show you why

play02:33

normally

play02:34

we have a default browser size as i

play02:36

mentioned that's usually 16 pixels

play02:39

however if i set that on the root

play02:41

element

play02:42

font size

play02:44

16 pixels we don't see a change here but

play02:47

if i go into my settings in chrome which

play02:49

you can do with the three dots and then

play02:51

choose settings once you launch that

play02:54

you'll be on a settings page and then

play02:56

you can choose appearance

play02:58

you go to a font size user preference

play03:00

and medium is what most are on but

play03:03

there's also other choices from very

play03:05

small to very large i'll choose very

play03:07

large we definitely see a change on the

play03:09

settings page right when i do that

play03:12

back in our document

play03:13

we don't see a change it didn't change

play03:16

at all i can refresh and it won't make

play03:18

any difference it did not change our

play03:20

font size with the user settings and

play03:22

that's because we took that choice away

play03:24

when we said no these absolutely must be

play03:27

16 pixels here that's our root size but

play03:30

if i delete this and we just let the

play03:33

browser handle the default size

play03:35

instantly all of our font gets bigger

play03:37

and our user settings take over we don't

play03:39

want to take that choice away from the

play03:41

users so we don't want to use an

play03:43

absolute font size such as a pixel

play03:46

setting so i'll go ahead and delete that

play03:49

so where would we want to use pixels in

play03:52

absolute value there are other settings

play03:54

that we might want an absolute value for

play03:56

so i'll select the h1 that i have here

play03:59

and i'm going to set a border on it and

play04:01

i'll set it to 2 pixels and then just so

play04:04

we can see it better i'll set it to

play04:06

dashed and red so it really stands out

play04:08

and save and now we can see this border

play04:10

around our h1 element and we have this

play04:13

absolute value for the pixel width and

play04:16

no matter what happens to the page on a

play04:18

small viewport large viewport whether

play04:21

it's a mobile device or a large desktop

play04:24

screen

play04:24

this will remain as two pixels and

play04:27

that's okay in this instance let's go

play04:29

back to our mdn page

play04:31

and now that we've covered the only

play04:32

pixel size that we were going to cover

play04:34

and that was down here on our pixel

play04:36

chart

play04:37

we're going to now cover percentage and

play04:40

we see that up here in a different chart

play04:42

this is a numbers links and percentages

play04:44

chart here

play04:45

so we're going to use percentage and it

play04:47

says it represents a fraction of some

play04:49

other value so 50 of something else

play04:53

percentages are always relative to

play04:56

another quantity so that's a

play04:58

consideration as well now when we set

play05:00

percentages we wouldn't normally do that

play05:03

with a font size either so let's set a

play05:05

percentage for a width on our h1 and now

play05:10

by default an h1 is a block level

play05:12

element and so by default it has a width

play05:14

of 100 so if we do that we're not really

play05:18

changing anything and now it's just 100

play05:21

as expected so let's go ahead and change

play05:24

that it looks at the parent though it's

play05:26

taking up one hundred percent of the

play05:28

parent's width or whatever was

play05:30

given to it so whatever it has available

play05:33

to it it took up one hundred percent and

play05:35

now this is only taking up

play05:37

50 percent

play05:38

so let's look at the parent for the h1

play05:41

it is inside of a header element

play05:43

so if we take this header element

play05:45

and we set it to a width

play05:48

of 50 percent

play05:49

then we should see a difference and yes

play05:52

it's even smaller so we're taking up

play05:55

only 50 percent of the header element

play05:57

which is taking up 50 of the page so

play06:00

really this h1 would be 25 of the page

play06:03

likewise if we change this to a hundred

play06:06

for the header

play06:07

then we'll see our h1 takes up fifty

play06:10

percent of the page so that's the thing

play06:12

to remember about setting a percentage

play06:14

for the width is that you are putting it

play06:17

relative to the parent it's only using

play06:20

what it has available to it if the

play06:22

header is 100 percent

play06:24

then the h1 that is the child has a 100

play06:27

percent of the page available to it

play06:29

however if the header is smaller let's

play06:31

put it at 80 percent then

play06:34

we're only using up fifty percent of

play06:37

eighty percent of the page and once

play06:39

again if you want an element that is a

play06:41

block level element to take up one

play06:43

hundred percent of the width of the page

play06:45

you don't really need to set it because

play06:46

that is the default value already so now

play06:49

we're taking up half of the page by

play06:52

setting the h1 to 50

play06:54

because it is 50

play06:56

of the full 100 width and you may see

play06:59

other uses for percentages as well but

play07:02

you commonly see them to set the sizing

play07:05

of different elements as far as the

play07:08

width of the page or sometimes the

play07:10

height but also we don't need to set the

play07:12

height because it will grow

play07:14

as the content grows i'm going to go

play07:17

back to my settings and set it from very

play07:19

large back to medium which is where most

play07:22

have their browser default to now when

play07:24

we look at our page the font is smaller

play07:27

but let's talk about another relative

play07:30

unit and this one does show on the mdn

play07:32

page in the chart down here under

play07:34

relative length units and it's the rim

play07:37

this is the unit that we'll typically

play07:39

use for font size and it relates to the

play07:41

font size of the root element now i

play07:44

already suggested we shouldn't set a

play07:47

font size on the root element so for our

play07:50

html element

play07:52

we typically wouldn't set a font size in

play07:54

here because we want to let the browser

play07:57

handle that and set the default font

play07:59

size so let's not do this for the html

play08:02

element i'll just go ahead and cut this

play08:05

i'm going to put it below the h1 and i'm

play08:07

going to set a font size

play08:09

for our paragraphs instead and this font

play08:12

size if we set it to one ram

play08:15

that's already the default so it

play08:16

shouldn't change anything and we see the

play08:18

size is the same so it says one root

play08:22

element which the root element is the

play08:24

default font size set by the browser the

play08:27

em doesn't stand for element by the way

play08:29

that's m so we have one root em or one

play08:32

root m

play08:34

but that means the root size that is

play08:37

defined and in our case when we don't

play08:39

set it then it's set by the browser so

play08:42

hopefully that clears up some confusion

play08:44

but if we put in two m's then it's going

play08:46

to double the default size and now we're

play08:49

probably at 36 pixels right here because

play08:51

most browsers default to that 16 pixel

play08:54

size

play08:55

so you can see how this relates back to

play08:57

the root now if we set a font size on

play09:00

the parent because our paragraph is in a

play09:02

main element

play09:04

we set this font size to

play09:07

to rim also

play09:09

it doesn't change the paragraph font

play09:11

size at all because the font

play09:13

set here is not relative to the main

play09:17

element it's not relative to the parent

play09:19

it's relative to the root which is

play09:21

defined by the browser so it's always

play09:23

going to look back to that default root

play09:25

size if we had set a font size in the

play09:28

html element it would be looking at that

play09:31

but without setting it it's the default

play09:33

size

play09:34

so what happens though if we switch

play09:36

these units if we're not using rem

play09:40

if we're using

play09:41

m just em so now

play09:44

i'll delete the r there and save oh boy

play09:48

that got a lot bigger well here's the

play09:50

problem that we would have if we were

play09:52

using ms for our font size it gets

play09:55

amplified so our main element was

play09:58

looking at the root so it doubled that

play10:01

to 32 pixels

play10:03

and then using two m's

play10:05

is looking at the main element which is

play10:07

the parent of the paragraph and so then

play10:10

it doubled that so now we're at 64

play10:12

pixels so that's what happens and you

play10:15

can get unintended consequences if you

play10:18

just use m's and that's why using the

play10:20

rim and always looking at the root

play10:22

definitely helps because the way we can

play10:25

get confused for example is if i remove

play10:27

this setting on the main and i save well

play10:30

now this is also looking at the root

play10:32

because there's no other font size saved

play10:34

and so we think okay we're good to use

play10:36

just m's but the problem is

play10:39

if somewhere along the way in our

play10:41

project and we have many elements we're

play10:43

styling

play10:44

we set another font size on a parent

play10:46

element suddenly it's amplified when we

play10:49

use m's so i'm going to switch this back

play10:51

to two rims and get closer to the size

play10:54

we'd want and it's going to look at the

play10:56

root so when would we use

play10:58

m's instead of rims we know we want to

play11:00

use rems on font size well when we set

play11:03

the font size on an element

play11:05

m doesn't look at the parent it looks at

play11:07

the element itself and so we could use

play11:10

it for something like margin or padding

play11:12

which i know we haven't covered and we

play11:14

will in more detail when we look at the

play11:16

css box model but i'm going to go ahead

play11:19

and do that here for the h1 so if i set

play11:21

a font size for the h1 and let's make it

play11:25

bigger let's make it three

play11:27

rim here and we save we instantly have

play11:30

a bigger font size on the h1 but now i

play11:34

could set

play11:35

the padding

play11:37

to

play11:38

1

play11:39

m which is the equivalent

play11:42

of the font size of that element so now

play11:45

this would also be three rims so it

play11:46

should be a pretty good size padding yes

play11:49

we have a lot of padding so i probably

play11:51

don't want that much maybe i want to

play11:52

drop it to

play11:54

0.5

play11:55

and now that's that's more reasonable

play11:57

that looks pretty good for a heading

play11:59

there so we added that padding which is

play12:01

inside this border it added extra

play12:04

padding and that is essentially half of

play12:08

three rims which would be one and a half

play12:10

and if we relate all this back to the

play12:12

default 16 pixel size

play12:15

one and a half of those

play12:17

would be 24 pixels whereas three rims

play12:21

would be

play12:22

48 pixels another instance where we may

play12:24

use m's instead of rims would be for the

play12:27

padding or margin setting on say a

play12:30

button if we created a button we would

play12:32

have text in the button we could set the

play12:34

font size for that button with rem but

play12:38

then we could go ahead and set the

play12:39

padding and or margin with the m units

play12:43

let's go back to the mdn page and look

play12:45

at our chart one other relative unit i'd

play12:48

like to look at today is the ch that

play12:51

stands for character and we have the

play12:53

advanced measure width of the glyph 0

play12:56

which essentially means the character 0

play12:59

of the element's font so whatever font

play13:01

we're using it's the measurement of 0

play13:04

and that helps us determine the

play13:06

character width and that kind of comes

play13:08

from print

play13:10

magazine newspaper layout and those sort

play13:12

of things where you only want to have a

play13:14

certain width based on the character

play13:17

size so in this example we have a font

play13:20

size here for our paragraph i'm going to

play13:23

go ahead and set the width of the

play13:24

paragraph and let's say we don't want

play13:26

any more than an estimated

play13:29

40 characters per line before the line

play13:31

wraps in the paragraph if we set that

play13:34

width notice it now has approximately 40

play13:37

characters before the line wraps and

play13:39

that kind of helps us define columns if

play13:42

we're doing that in some type of layout

play13:44

now we've acknowledged that the browser

play13:47

sets a default font size but it actually

play13:49

sets a lot of different default styles

play13:52

as well so let's go ahead and right

play13:55

click on this h1 heading and choose

play13:57

inspect or you could press ctrl shift

play14:00

and the letter i either way what we're

play14:03

doing is opening up the dev tools it may

play14:05

scrunch our page here a little bit but

play14:07

that's okay if i highlight this h1 now

play14:10

it is showing several things about the

play14:12

page it's showing that there's a large

play14:15

margin that's going to be the orange

play14:17

when i highlight over

play14:19

it's showing the green area and that's

play14:21

padding that we set

play14:23

and then the blue area is the h1 itself

play14:26

right here so we see all of those things

play14:28

in dev tools when we hover over these

play14:31

elements and by the way i'm on the

play14:32

elements tab of dev tools and then if

play14:35

you don't have your dev tools on the

play14:37

right you can click the three dots here

play14:39

and there's a dock side setting maybe

play14:41

it's on the bottom for you but if you

play14:42

want it on the right you can choose dock

play14:45

to the right just like i have here so we

play14:47

can really see more about all of these

play14:49

elements in dev tools what we can also

play14:52

see as we come down here are the styles

play14:55

and then we see the h1 and the styles

play14:58

that i have set on the h1 element but

play15:01

underneath that we see some others as

play15:03

well and it says user agent style sheet

play15:06

this is what the browser is setting on

play15:08

the h1 element by default that we don't

play15:10

have to and you can see we have put in a

play15:13

different font size so we have

play15:15

overridden the font size for the h1

play15:17

element so it has crossed that out

play15:19

everything else here the display block

play15:22

and the different margins and even the

play15:23

font weight are applied from that user

play15:26

style sheet so those are default styles

play15:28

and we can override them just like i did

play15:30

with the font size and before we get

play15:32

into these next two relative units we

play15:35

are going to override some of these so

play15:37

what i want to do

play15:39

is start a css reset which i'll talk

play15:42

about more

play15:43

in the tutorial of the css box model as

play15:46

well but i'm going to use

play15:48

the selector that selects all elements

play15:50

again our wildcard selector and here i'm

play15:53

going to set the margin to zero

play15:56

the padding

play15:57

to zero and one other setting that says

play16:00

box

play16:01

sizing and i'm going to set that to

play16:03

border box and when i save

play16:05

notice all the padding and margin is

play16:08

gone now

play16:09

our h1 is right up in the left corner we

play16:12

don't have any space between the

play16:14

paragraph and the h1 we got rid of all

play16:16

of those default margins and paddings

play16:18

that were creating that extra space but

play16:21

that's important when we go back to the

play16:23

mdn page and we look at these next two

play16:25

relative units we have viewport width

play16:28

which is one percent of the viewport's

play16:30

width

play16:30

and viewport height which is one percent

play16:32

of the viewport's height so let's look

play16:34

at how we can apply those but if we had

play16:37

that extra margin and padding there we

play16:39

wouldn't be able to see the detail that

play16:41

i want to show okay i'm going to take

play16:43

our main element here and set a width on

play16:46

it with our viewport width units

play16:49

just so we can see

play16:51

what it is capable of and so i'll do

play16:53

that i also need to set a background

play16:55

color just so we can see what we're

play16:56

doing better

play16:58

and i'll set this to let's say sky blue

play17:01

and save so now we can see the width of

play17:04

our paragraph is actually wider than the

play17:07

width of our main element and so it is

play17:10

overflowing that main element but we

play17:12

have actually set the main element to 50

play17:16

of the width of the full page and you

play17:18

can see that's matching the 50 percent

play17:21

width that we have up here but i'm using

play17:24

the viewport width units right here

play17:26

there may be times when you want to use

play17:28

the viewport width but most of the time

play17:30

i find myself using the percentage and

play17:34

getting the same result and i'll give an

play17:36

example where the percentage is actually

play17:38

a better choice so let me remove that 50

play17:41

percent and i'll remove this background

play17:43

color that's making our page look bad

play17:45

and we'll come back up to the top and

play17:48

underneath our select all i'm going to

play17:50

choose the body element here and as i

play17:53

said before we don't need to set a width

play17:55

of 100

play17:56

but i'll put this in and i'm going to

play17:58

set this to

play18:00

100 viewport with units instead and save

play18:04

now we don't really notice a change and

play18:07

i have seen this on pages and i've even

play18:08

done it myself but i figured out the

play18:11

problem if we have a width of 100

play18:13

viewport width units we do have a

play18:16

problem when the content out the

play18:18

vertical content outgrows the page and

play18:20

i'll show you why so i need to go back

play18:22

to the html

play18:24

and in the html i'm going to quickly add

play18:27

some more content so i'll say

play18:29

paragraph times 10 and we'll do this

play18:32

with

play18:33

some just sample lorem text that's an

play18:35

image abbreviation so when i press tab

play18:38

it adds all those extra paragraphs and

play18:40

save now we've got lots of content here

play18:43

and now we have a problem and here's the

play18:46

problem

play18:47

our scroll bar showed up on the right

play18:50

which is good we need that to see all of

play18:52

this content but what also happened is

play18:55

we had a scroll bar show up down here on

play18:57

the bottom

play18:58

and that is because our viewport width

play19:01

when we set it to 100 as we did on the

play19:03

body right here does not account for

play19:05

this scroll bar on the right so as soon

play19:08

as this scroll bar on the right shows up

play19:10

and it shows up when our content

play19:12

outgrows

play19:13

our height

play19:14

so when we need to scroll to see the

play19:16

content is when this shows up as it

play19:18

should

play19:18

but then

play19:20

that little extra bit that we can't see

play19:22

creates this scroll bar so we can see it

play19:25

and that's annoying and most developers

play19:27

don't like that horizontal scroll bar

play19:29

showing up

play19:30

so if you were to set the width 100

play19:33

would be much more desirable because now

play19:36

we still get the scroll bar here where

play19:38

we can scroll up and down vertically we

play19:40

do not have a scroll bar on the bottom

play19:42

and we don't usually want that but

play19:44

overall as i've mentioned before we

play19:46

don't even need to set 100 percent width

play19:49

on the body as it should default to that

play19:51

anyway i'm going to jump back to the

play19:53

html and go ahead and undo all of that

play19:56

extra content we added

play19:58

and save so we're back to our normal

play20:00

content for the page

play20:02

let's go back to style as well

play20:04

and let's right click once again on the

play20:07

page and i just kind of i think i

play20:08

selected the paragraph here so when

play20:10

devtools opens up it should be on the

play20:13

paragraph and we'll see that here in

play20:15

just a second

play20:16

yep i'm on the paragraph now let's

play20:18

highlight the body notice the body is

play20:20

not the full height of the page it only

play20:22

grows with the content but sometimes we

play20:26

want a body element that is the full

play20:29

height of the page even if we don't have

play20:31

enough content to do that and we can do

play20:34

that with the viewport height unit and

play20:36

i'm going to do that over here

play20:38

in our code i'll choose the body element

play20:41

but i don't want to set height because

play20:43

that would also limit it to 100 vh and

play20:46

then we could outgrow that if our

play20:48

content were to outgrow what i want to

play20:50

do is set the

play20:52

min height to 100 bh

play20:54

and save and now that will still allow

play20:57

the body to grow with the content if it

play20:59

outgrows the viewport but at the same

play21:02

time it's now the full page and there

play21:04

could be instances where you want that

play21:06

remember to keep striving for progress

play21:08

over perfection and a little progress

play21:10

every day will go a very long way

play21:13

please give this video a like if it's

play21:15

helped you and thank you for watching

play21:17

and subscribing you're helping my

play21:18

channel grow

play21:20

have a great day and let's write more

play21:22

code together very soon

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
CSS TutorialWeb DevelopmentVisual Studio CodeChrome BrowserLive ServerFont SizeCSS UnitsResponsive DesignWeb AccessibilityViewport Units