< Return to Video

CSS width height overflow

  • 0:00 - 0:04
    When we use `div`s, it's often
    because we want to divide our page
  • 0:04 - 0:08
    into different boxes, resize those boxes,
    and move them around.
  • 0:08 - 0:12
    It takes a good eye for design to decide
    how exactly to lay out a page
  • 0:12 - 0:13
    so that it looks good.
  • 0:13 - 0:16
    But right now, we're going to ignore
    the goal of making a page look good
  • 0:16 - 0:19
    and just show you how to change stuff up.
  • 0:19 - 0:22
    Let's resize the "official-info" `div`.
  • 0:22 - 0:26
    By default, a div takes up 100 percent
    of the available width.
  • 0:26 - 0:29
    That's why you see the grey box
    going across the whole page.
  • 0:29 - 0:32
    But maybe I only want it
    to take up 300 px.
  • 0:33 - 0:37
    Well, I'll add another property to
    the CSS rule up here:
  • 0:37 - 0:39
    width: 300px;.
  • 0:40 - 0:43
    That worked, but let's do something
    more interesting.
  • 0:43 - 0:45
    Let's use percentage width,
  • 0:45 - 0:49
    and say that the div will always take up
    70 percent of the available width.
  • 0:50 - 0:53
    Now the text is constrained
    to that smaller box,
  • 0:53 - 0:55
    and the `div` has gotten taller.
  • 0:55 - 0:59
    If we really want to, we can also
    constrain the height of the box
  • 0:59 - 1:01
    with the height property:
  • 1:01 - 1:03
    height: 180px;
  • 1:04 - 1:07
    Hmm, okay. Something funny happened.
  • 1:07 - 1:10
    The grey box resized to 180 pixels,
  • 1:10 - 1:13
    but the text is overflowing
    outside of that grey box.
  • 1:14 - 1:16
    Why'd that happen?
  • 1:16 - 1:20
    That is because of a property
    we call "overflow".
  • 1:20 - 1:24
    The default value
    for `overflow` is `visible`,
  • 1:24 - 1:28
    which means that the element resizes,
    but the content flows outside of it,
  • 1:28 - 1:30
    which looks a bit silly.
  • 1:30 - 1:33
    What other options
    do we have for overflow?
  • 1:33 - 1:36
    Well, we can try `hidden`.
  • 1:37 - 1:40
    That cuts the content off,
    trimming it at the border.
  • 1:40 - 1:42
    That's not what we want though,
  • 1:42 - 1:46
    because then our viewers won't be able
    to read all of amazing official info.
  • 1:46 - 1:49
    We could also try `auto`,
  • 1:49 - 1:53
    which tells the browser to add scroll bars
    if the content overflows.
  • 1:53 - 1:57
    Now I can scroll around
    inside the box to see the text.
  • 1:57 - 1:59
    If we want, we can be even more specific:
  • 1:59 - 2:03
    we can specify different
    overflow properties for each direction.
  • 2:03 - 2:04
    For example, if we wanted
  • 2:04 - 2:08
    to overflow with scrollbars
    in the y direction, up and down,
  • 2:08 - 2:11
    we'll just:
    overflow-y: auto;
  • 2:11 - 2:14
    but then if we want to trim it
    in the x direction,
  • 2:14 - 2:17
    we can say:
    overflow-x: hidden;.
  • 2:18 - 2:21
    Be careful any time you're using overflow,
  • 2:21 - 2:24
    because scrollbars can get
    really annoying for the user.
  • 2:25 - 2:27
    Especially scrollbars within scrollbars--
  • 2:27 - 2:29
    avoid those if you can.
  • 2:29 - 2:34
    Now going back to width/height--
    we can actually use width and height
  • 2:34 - 2:37
    for all sorts of elements,
    like our images.
  • 2:38 - 2:42
    Now that you know CSS, you can
    use the width/height CSS properties
  • 2:42 - 2:45
    instead of the width/height attributes.
  • 2:45 - 2:53
    Let's make this cat image a bit bigger
    by giving it an id, "cute-cat",
  • 2:53 - 2:55
    deleting the attribute,
  • 2:55 - 3:00
    and going up to our style rule, let's say:
    #cute-cat {
  • 3:00 - 3:03
    width: 120px;.
  • 3:04 - 3:06
    Just like before with attributes,
  • 3:06 - 3:09
    it's best to only specify
    the width or the height,
  • 3:09 - 3:13
    and let the browser figure out
    the best value for the other dimension.
  • 3:13 - 3:15
    Otherwise, we'll have a squashed kitty!
  • 3:15 - 3:19
    Well, okay, that sounds kind of awesome,
    so let's just try that for a second:
  • 3:19 - 3:20
    height: 40px;
  • 3:20 - 3:22
    Haha, squashed kitty-- yay!
  • 3:22 - 3:25
    Okay, I got that urge out.
  • 3:25 - 3:29
    I'll be a responsible web developer
    and delete it now.
  • 3:29 - 3:31
    The more knowledge you have,
  • 3:31 - 3:33
    the more responsibility
    you need to take on.
  • 3:33 - 3:36
    Just because you can
    add scrollbars to everything,
  • 3:36 - 3:39
    and squash all the kitties,
    it doesn't mean that you should.
  • 3:39 - 3:43
    Because you're usually making
    websites for other people to use,
  • 3:43 - 3:47
    and what you think is hilarious
    might be what they find super-hard to use.
  • 3:48 - 3:52
    But if you make a few funny things
    here on Khan Academy, I won't mind.
Title:
CSS width height overflow
Description:

more » « less
Video Language:
English
Duration:
03:55

English subtitles

Revisions