Return to Video

CSS font size

  • 0:01 - 0:04
    We can also use CSS to change
    the size of our text.
  • 0:04 - 0:07
    As you can see by looking at
    the headings on this page
  • 0:07 - 0:09
    compared to the paragraphs,
    the browser already provides
  • 0:09 - 0:12
    default styles with different sizes
    for different elements.
  • 0:12 - 0:14
    There's a body font size,
  • 0:14 - 0:16
    and bigger sizes
    for each level of heading.
  • 0:16 - 0:19
    Sometimes we want to tweak
    those sizes, though.
  • 0:19 - 0:22
    And we can do that with the CSS
    `font-size` property.
  • 0:22 - 0:24
    For example:
  • 0:24 - 0:29
    What if we want all of the text on our
    page to be just a little bit smaller?
  • 0:29 - 0:31
    Well, we'll go to the `style` tag,
  • 0:31 - 0:35
    and we'll add a CSS rule
    for the `body` tag.
  • 0:35 - 0:39
    And then inside that,
    put a `font-size` property.
  • 0:39 - 0:43
    Now, what should the value be
    for `font-size`?
  • 0:43 - 0:46
    How do we measure font size, anyway?
  • 0:46 - 0:48
    That's actually
    a really good question.
  • 0:48 - 0:50
    Because there are at least
    ten different units
  • 0:50 - 0:52
    that CSS supports for `font-size`.
  • 0:53 - 0:56
    Let's start with a unit
    that you've seen before: pixels.
  • 0:56 - 0:59
    We used pixels to decide
    how big to make images.
  • 0:59 - 1:01
    And we'll keep using them a lot in CSS.
  • 1:01 - 1:03
    Let's try 11 pixels.
  • 1:04 - 1:06
    Hah. Everything got smaller.
  • 1:06 - 1:09
    Even the headings got smaller, actually.
  • 1:09 - 1:11
    Why did the headings get smaller, too?
  • 1:11 - 1:15
    Shouldn't they be whatever pixel size
    the browser set them to before?
  • 1:16 - 1:16
    No.
  • 1:16 - 1:22
    Because the default browser style
    for headings isn't specified as a pixel.
  • 1:22 - 1:25
    It's specified using a different unit,
    called "em".
  • 1:25 - 1:27
    And that unit is a relative unit
  • 1:27 - 1:32
    that makes the heading font size
    scale proportionally to the body font.
  • 1:32 - 1:34
    Let me show you what I mean.
  • 1:34 - 1:36
    Let's go to our `h2` style rule
  • 1:37 - 1:39
    and add the `font-size` property.
  • 1:39 - 1:44
    This time, instead of thinking in pixels,
    I'm going to think relatively.
  • 1:45 - 1:50
    How much bigger, relatively,
    do we want ``s compared to body text?
  • 1:50 - 1:53
    Maybe, two times bigger?
  • 1:53 - 1:57
    For that, we write `2em`.
  • 1:57 - 2:00
    What actually happened now is that
    the browser has calculated
  • 2:00 - 2:02
    a pixel size for the font.
  • 2:02 - 2:05
    The browser knows that
    the body font size is 11 pixels.
  • 2:05 - 2:08
    And you told it that `h2` should be
    two times bigger,
  • 2:08 - 2:12
    so now all the ``s
    are 22 pixels high.
  • 2:12 - 2:17
    If we change the body font size
    to 12 pixels,
  • 2:17 - 2:19
    then how big will the ``s be?
  • 2:19 - 2:21
    That's right, 24 pixels.
  • 2:22 - 2:26
    What if we change the `h2` font size
    to 1.5em?
  • 2:27 - 2:31
    Now the `h2` will be
    12 times one point five,
  • 2:31 - 2:33
    which is 18 pixels high.
  • 2:33 - 2:38
    We can also specify a pixel size
    for the `h2` like we did for `body`,
  • 2:38 - 2:40
    and it would work the same way.
  • 2:40 - 2:42
    It depends on what you find easier.
  • 2:42 - 2:45
    And like I mentioned earlier, there are
    lots more units that we could use
  • 2:45 - 2:47
    besides pixels and em.
  • 2:47 - 2:50
    These are just two
    of the most popular units.
  • 2:50 - 2:51
    But if you're hungry for more,
  • 2:51 - 2:53
    check out the documentation
    for `font-size`,
  • 2:53 - 2:55
    or search the Internet for more tutorials.
  • 2:56 - 2:59
    It's time to "super size" your font size!
Title:
CSS font size
Description:

more » « less
Video Language:
English
Duration:
03:00
Josh Kline edited English subtitles for CSS font size

English subtitles

Revisions