< Return to Video

CSS floating elements

  • 0:00 - 0:04
    I'm laying out a webpage for hopper,
    one of our avatars here on Khan Academy.
  • 0:05 - 0:10
    It's got an image, some cool links,
    and a paragraph.
  • 0:10 - 0:16
    I think that this picture of Hopper
    would look better next to the paragraph,
  • 0:16 - 0:18
    so I'm going to cut
  • 0:18 - 0:20
    and paste it down here.
  • 0:20 - 0:24
    Hm. Okay, that doesn't look
    as good as I hoped it would.
  • 0:24 - 0:26
    The text starts
    at the bottom of the image.
  • 0:26 - 0:29
    I was hoping that the text
    would wrap around the image,
  • 0:29 - 0:31
    like in newspapers and magazines.
  • 0:31 - 0:34
    We'll need a new
    CSS property for that: `float`.
  • 0:34 - 0:37
    It's a way of floating elements
    in and around other elements,
  • 0:37 - 0:40
    and it's perfect
    for wrapping text around images.
  • 0:40 - 0:44
    So we go up to our "pic" rule, and say:
    `float: `.
  • 0:44 - 0:48
    And then for the value, we need to decide
    if we want the picture to float
  • 0:48 - 0:50
    to the left-hand side
    or the right-hand side.
  • 0:51 - 0:52
    Let's try left.
  • 0:53 - 0:54
    Great. Perfect.
  • 0:55 - 0:57
    Well, okay, not quite perfect,
  • 0:57 - 1:00
    because the text
    is really close to the image.
  • 1:00 - 1:02
    Do you remember
    what property we should use
  • 1:02 - 1:04
    to separate the text from the image?
  • 1:05 - 1:07
    It's part of the box model,
    which we just learned.
  • 1:07 - 1:09
    Margin.
  • 1:09 - 1:14
    Let's add some `margin: right`
    and `margin: bottom` to this image
  • 1:14 - 1:16
    to give it a little breathing room.
  • 1:16 - 1:22
    Ah, okay, that's much better.
  • 1:22 - 1:26
    We can also float elements
    that aren't images.
  • 1:27 - 1:29
    Like if we want something like a sidebar.
  • 1:29 - 1:31
    Let's-- what about this list of links?
  • 1:32 - 1:35
    We can take this list of links
    and float it to the right.
  • 1:35 - 1:38
    So let's add a rule for `#hopper-links`,
  • 1:38 - 1:41
    and float it to the right.
  • 1:41 - 1:44
    Okay, it's floating
    but it's taking up a lot of width--
  • 1:44 - 1:46
    more than I was hoping it'd take up.
  • 1:47 - 1:51
    Let's restrict the width to 30 percent
  • 1:51 - 1:55
    so it'll always take up
    30 percent of the page,
  • 1:55 - 1:59
    and the rest of the page will fill in
    the remaining 70 percent.
  • 1:59 - 2:02
    Generally, whenever we float a `div`,
    we have to give it a width.
  • 2:02 - 2:05
    Because otherwise,
    `div`s try to take up all of the space,
  • 2:05 - 2:07
    and nothing can wrap around them.
  • 2:07 - 2:11
    Seems like it also needs a little bit
    of a `margin: left`, too.
  • 2:11 - 2:13
    Ah, okay.
  • 2:13 - 2:18
    Now, notice the footer at the bottom
    of the contact info for Hopper.
  • 2:19 - 2:22
    It's doing this weird thing where
    it's overlapping the sidebar.
  • 2:22 - 2:24
    And that's because it's "wrapping".
  • 2:24 - 2:26
    We don't want our footer to wrap, though.
  • 2:26 - 2:28
    We want it to always be
    on the bottom of everything,
  • 2:28 - 2:30
    because it's a footer.
  • 2:30 - 2:32
    To make it not wrap, we can use
    the `clear` property,
  • 2:32 - 2:34
  • 2:34 - 2:37
    and put `clear: both`.
  • 2:37 - 2:38
    Hah.
  • 2:38 - 2:40
    We could use
    `clear: left` or `clear: right`
  • 2:40 - 2:42
    if we only wanted to not wrap around
  • 2:42 - 2:44
    right-floating elements
    or left-floating elements.
  • 2:45 - 2:48
    But usually we want to not wrap around
    any floating elements,
  • 2:48 - 2:49
    so we say `clear: both`.
  • 2:49 - 2:52
    This is starting to look like
    a real webpage.
  • 2:52 - 2:55
    We have a main area, a sidebar, a footer.
  • 2:55 - 3:00
    In fact, you now know the CSS properties
    that make most webpage layouts happen.
  • 3:00 - 3:05
    Put together some `div`s with width,
    height, padding, margin, float, and clear,
  • 3:05 - 3:08
    and there are all sorts
    of webpage layouts at your fingertips.
Title:
CSS floating elements
Description:

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

English subtitles

Revisions