< Return to Video

CSS box model

  • 0:00 - 0:02
    You know how we made something that
  • 0:03 - 0:05
    looked like a box in the last
  • 0:05 - 0:07
    talk-through by making this `div` and then
  • 0:07 - 0:08
    giving it a `background-color`?
  • 0:08 - 0:11
    Well, actually, every element on your
  • 0:11 - 0:13
    webpage is considered a box
  • 0:13 - 0:15
    by the browser, whether or not it looks
  • 0:15 - 0:18
    like a box to you. This heading is a box..
  • 0:18 - 0:22
    this paragraph is a box..even this `span`
  • 0:22 - 0:25
    that we made is a box. Some of the boxes
  • 0:25 - 0:30
    are big, some are small, some are in line
  • 0:30 - 0:33
    like the `span`, some are block, like the
  • 0:33 - 0:36
    `div`. But they're all considered boxes.
  • 0:36 - 0:39
    Why does this matter? 'Cause there's
  • 0:39 - 0:41
    something called the "box model," which
  • 0:41 - 0:44
    you can see in the diagram I just
  • 0:44 - 0:46
    pasted in. Every element's box has four
  • 0:46 - 0:49
    parts: the actual content, the padding,
  • 0:49 - 0:52
    the border, and the margin. We can use CSS
  • 0:52 - 0:54
    to modify the padding, border, and margin.
  • 0:54 - 0:57
    So you'll understand soon what those
  • 0:57 - 0:59
    really are. Let's start with the margin.
  • 0:59 - 1:02
    That's the transparent area around the box
  • 1:02 - 1:04
    that separates the box from other
  • 1:04 - 1:07
    elements. We'll specify margin using our
  • 1:07 - 1:10
    favorite unit: pixels. To add 15 pixels
  • 1:10 - 1:13
    of margin on every side of the official
  • 1:13 - 1:17
    info box, we can just write: `margin:
  • 1:17 - 1:22
    15px;`. So, you see the change that made?
  • 1:22 - 1:25
    What if we want a different amount of
  • 1:25 - 1:27
    margin on each side? Like more on the
  • 1:27 - 1:30
    top-bottom than the left-right? We can
  • 1:30 - 1:32
    write those amounts in clockwise order,
  • 1:32 - 1:37
    starting from the top. So top 15px, right
  • 1:37 - 1:44
    0px, bottom 10px, left 6px. OR, we could
  • 1:44 - 1:46
    use specific properties for each side, but
  • 1:46 - 1:49
    we only wanna specify a few sides. Like if
  • 1:49 - 1:51
    we want to have some space around the cat
  • 1:51 - 1:57
    picture, on the right..and then, we also
  • 1:57 - 2:01
    just want some on the bottom...and we're
  • 2:01 - 2:04
    happy to have the default margin for the
  • 2:04 - 2:07
    other sides. There's also a special value
  • 2:07 - 2:09
    for margin that'll help us do something
  • 2:09 - 2:11
    fancy with our pages. To show you that,
  • 2:11 - 2:14
    I'm gonna add a `div` around the entire
  • 2:14 - 2:17
    page. I'll give it an ID of "container."
  • 2:17 - 2:21
    Let's put this ta-a-a-ag here, so it
  • 2:21 - 2:26
    contains everything. Now, gonna add a rule
  • 2:26 - 2:29
    for that `div` to give it a width of 400
  • 2:29 - 2:34
    px, and I wanna center it on the page. I
  • 2:34 - 2:38
    COULD specify a `margin-left: 100px;`,
  • 2:38 - 2:41
    because that looks centered to me, but it
  • 2:41 - 2:43
    may not be centered to you, because your
  • 2:43 - 2:45
    browser may be bigger or smaller. What we
  • 2:45 - 2:47
    want is to be able to say, "give it
  • 2:47 - 2:49
    however much margin it needs so that it's
  • 2:49 - 2:53
    got equal margin on both sides." That is
  • 2:53 - 2:56
    exactly what `margin: auto;` does. And
  • 2:56 - 2:58
    it's a great way to center `div`s on a
  • 2:58 - 3:01
    page. Now that we've centered that `div`,
  • 3:01 - 3:04
    we might wanna make it even more distinct
  • 3:04 - 3:06
    by adding a border around the edge of it
  • 3:06 - 3:10
    using the CSS border property. Let's add a
  • 3:10 - 3:13
    red border to that `div`. We type
  • 3:13 - 3:15
    "border:," and then we just need to
  • 3:15 - 3:17
    specify three aspects of the border:
  • 3:17 - 3:20
    thickness, style, and color. For a thin
  • 3:20 - 3:23
    border, I'll just type "1px," uh, for a
  • 3:23 - 3:26
    solid line, nothing fancy, we'll just
  • 3:26 - 3:29
    type "solid," and...to make it red I'll
  • 3:29 - 3:32
    just pop up my R G B color picker and pick
  • 3:32 - 3:38
    a nice...fiery red. Okay. Just like with
  • 3:38 - 3:40
    margin, we can specify the border for
  • 3:40 - 3:43
    just, like, one side. Like if we want a
  • 3:43 - 3:46
    REALLY thick purple border on the top,
  • 3:46 - 3:49
    we'll add another property. "border-top:
  • 3:49 - 3:57
    10px solid purple;." Cool! Now, let's add
  • 3:57 - 4:00
    a frame to the image. and play around with
  • 4:00 - 4:02
    border styles. So going up to our
  • 4:02 - 4:08
    "cute-cat," uh, let's see, "border: 6px,"
  • 4:08 - 4:13
    let's try "groove red." Okay. Now I don't
  • 4:13 - 4:16
    like groove, let's try "double?" Eh, let's
  • 4:16 - 4:19
    try "ridge." Ah-ha! Now that looks like a
  • 4:19 - 4:23
    frame. Now how about a border around our
  • 4:23 - 4:26
    official info? Uh, let's see, "border:,"
  • 4:26 - 4:28
    let's not make it too big, "2px," let's
  • 4:28 - 4:31
    try "dotted" and then, uh, let's pick an,
  • 4:31 - 4:36
    uh, just a subtle gray, uh, lemme try
  • 4:36 - 4:38
    "dashed" instead. Okay, that - that's what
  • 4:38 - 4:41
    I want. Now with all these borders,
  • 4:41 - 4:42
    something is bothering me a little.
  • 4:42 - 4:45
    Actually, something is bothering me a LOT.
  • 4:45 - 4:49
    See how the text is butting up next to the
  • 4:49 - 4:52
    border in the "container?" And - and
  • 4:52 - 4:53
    butting against the text in the official
  • 4:53 - 4:57
    info? That's so gross-looking, and it
  • 4:57 - 5:00
    makes it harder to read the text. THAT is
  • 5:00 - 5:02
    where padding comes in. Whenever your
  • 5:02 - 5:04
    elements have `background-color`s or
  • 5:04 - 5:05
    `border`s, you almost ALWAYS want to add
  • 5:05 - 5:08
    padding, to put a bit of space between the
  • 5:08 - 5:10
    contents and the edges. Let's add some
  • 5:10 - 5:13
    padding to the "container," just...let's
  • 5:13 - 5:18
    do 15px all around the container. Oh. So
  • 5:18 - 5:21
    much better. Uh, let's add...let's add
  • 5:21 - 5:23
    some padding to our official info, let's
  • 5:23 - 5:28
    see: "padding: 6px," oh, beautiful. Now,
  • 5:28 - 5:29
    we don't need to add padding to the image,
  • 5:29 - 5:31
    since images actually look good inside
  • 5:31 - 5:34
    frames like that. And there you have it:
  • 5:34 - 5:36
    the box model. Margin, border, and
  • 5:36 - 5:38
    padding. I used big values and bright
  • 5:38 - 5:40
    colors to show them off to you, but my
  • 5:40 - 5:43
    page DOES look a bit cheesy now. If you
  • 5:43 - 5:44
    want your page to look sleek and
  • 5:44 - 5:47
    sophisticated, try using more subtle
  • 5:47 - 5:49
    whites and grays. Whatever you do, make
  • 5:49 - 5:51
    sure you use these properties, because
  • 5:51 - 5:53
    they'll have a big effect on how your page
  • 5:52 - 5:54
    looks and feels.
Title:
CSS box model
Description:

more » « less
Video Language:
English
Duration:
05:56
Abby Kurtz edited English subtitles for CSS box model

English subtitles

Revisions