Return to Video

CSS positioning

  • 0:00 - 0:02
    Now we'll learn how to use CSS
  • 0:02 - 0:04
    To really move things around.
  • 0:04 - 0:06
    Not just put them next to each other.
  • 0:06 - 0:08
    But to actually put things
  • 0:08 - 0:09
    On top of each other.
  • 0:09 - 0:12
    Here, we have a webpage
  • 0:12 - 0:16
    With a few headers, and images, and some
  • 0:16 - 0:18
    Paragraphs down here
  • 0:18 - 0:20
    And its currently all laid out with
  • 0:20 - 0:22
    The default positioning strategy
  • 0:22 - 0:24
    Which the browser uses
  • 0:24 - 0:27
    Which we call static or normal positioning
  • 0:27 - 0:29
    It just means that in line elements
  • 0:29 - 0:31
    Like images
  • 0:31 - 0:32
    Are laid from left to right
  • 0:32 - 0:34
    And block elements
  • 0:34 - 0:35
    Like headers and paragraphs
  • 0:35 - 0:38
    Are laid out from top to bottom
  • 0:38 - 0:40
    We can change that positioning strategy
  • 0:40 - 0:43
    Using the CSS position property
  • 0:43 - 0:46
    Let's try it on the landscape image
  • 0:46 - 0:50
    We'll type position, colon, and then
  • 0:50 - 0:51
    Relative for the value
  • 0:51 - 0:53
    The relative position strategy
  • 0:53 - 0:55
    Means position it how you normally would
  • 0:55 - 0:58
    But then offset it by some amount
  • 0:58 - 1:00
    Now, to tell the browser what amount
  • 1:00 - 1:02
    We want to offset by
  • 1:02 - 1:03
    We need to use some combination
  • 1:03 - 1:05
    Of four new CSS properties
  • 1:05 - 1:07
    Top, bottom, and left, and right
  • 1:07 - 1:09
    Like let's say we wanna have it be
  • 1:09 - 1:12
    Twenty pixels down
  • 1:12 - 1:14
    Say top twenty pixs
  • 1:14 - 1:15
    And ten pixels over
  • 1:15 - 1:17
    We'll say left ten pixs
  • 1:17 - 1:19
    So that's kind of neat,
  • 1:19 - 1:21
    But not really that neat
  • 1:21 - 1:23
    I wanna show you something waaay cooler
  • 1:23 - 1:25
    Absolute positioning
  • 1:25 - 1:27
    We can use it to take an element
  • 1:27 - 1:29
    Completely out of the normal flow
  • 1:29 - 1:31
    And put it anywhere on the screen
  • 1:31 - 1:32
    To do that
  • 1:32 - 1:33
    I will change relative
  • 1:33 - 1:34
    To absolute
  • 1:34 - 1:35
    On the landscape
  • 1:35 - 1:37
    And keep the top left
  • 1:37 - 1:38
    And you can see
  • 1:38 - 1:39
    That landscape is now
  • 1:39 - 1:42
    Hiding our images and dance party heading
  • 1:42 - 1:43
    And now we're going to fix that
  • 1:43 - 1:45
    Let's start with Winston
  • 1:45 - 1:48
    So we'll add a rule for Winston
  • 1:48 - 1:52
    And give Winston a position absolute
  • 1:52 - 1:54
    And then let me say
  • 1:54 - 1:56
    Top forty pixs
  • 1:56 - 1:58
    Oh, let's say top fifty pixs
  • 1:58 - 2:00
    And then left fifty pixs
  • 2:00 - 2:02
    Ok, that looks good
  • 2:02 - 2:04
    And hopper is really eager to get on top
  • 2:04 - 2:06
    As well
  • 2:06 - 2:07
    So let's say Hopper also needs
  • 2:07 - 2:12
    A position absolute
  • 2:12 - 2:15
    And let's say top thirty pixs
  • 2:15 - 2:18
    And left sixty pixs
  • 2:18 - 2:20
    Ok, so my goal is
  • 2:20 - 2:21
    To make it look like Hopper
  • 2:21 - 2:24
    Is kind of dancing in front of Winston
  • 2:24 - 2:27
    But right now it doesn't look that way
  • 2:27 - 2:28
    Because Winston is being drawn
  • 2:28 - 2:30
    On top of Hopper
  • 2:30 - 2:32
    To fix this,
  • 2:32 - 2:34
    I could either change the order of
  • 2:34 - 2:35
    The actual image tags
  • 2:35 - 2:37
    In the html
  • 2:37 - 2:39
    But a kind of better way is
  • 2:39 - 2:42
    To use the CSS z index property
  • 2:42 - 2:44
    We can use that to tell the browser
  • 2:44 - 2:45
    Exactly what order to draw
  • 2:45 - 2:46
    Elements in
  • 2:46 - 2:47
    By giving them differency
  • 2:47 - 2:48
    Indexes
  • 2:48 - 2:51
    So I'll start with a landscape
  • 2:51 - 2:53
    And give it a z index of one
  • 2:53 - 2:56
    And Winston goes on top with two
  • 2:56 - 2:59
    Hopper goes on top with three
  • 2:59 - 3:00
    Alright!
  • 3:00 - 3:03
    Now Hopper is dancing in front of Winston
  • 3:03 - 3:04
    Even if he doesn't like that
  • 3:04 - 3:05
    But he'll have to deal
  • 3:05 - 3:08
    But now we still have headings
  • 3:08 - 3:10
    And columns that are hidden
  • 3:10 - 3:12
    So let's see, let's try
  • 3:12 - 3:14
    And get the, maybe I want the
  • 3:14 - 3:17
    Dance party to be on top of everything
  • 3:17 - 3:19
    So I'm also gonna give that
  • 3:19 - 3:21
    Position absolute
  • 3:21 - 3:22
    And z index four
  • 3:22 - 3:23
    Ok, looks good
  • 3:23 - 3:24
    Maybe left ten pixs
  • 3:24 - 3:25
    Just move it over
  • 3:25 - 3:27
    Maybe, maybe a bit more
  • 3:27 - 3:29
    Alright, that looks good
  • 3:29 - 3:30
    Now, for the song lyrics
  • 3:30 - 3:31
    I actually just want them to
  • 3:31 - 3:33
    display underneath everything
  • 3:33 - 3:36
    So for those I'm thinking
  • 3:36 - 3:38
    Position relative and then
  • 3:38 - 3:39
    We could just do a top
  • 3:39 - 3:41
    Which is you know,
  • 3:41 - 3:43
    Would equal the height of the image
  • 3:43 - 3:45
    It would be two hundred twenty pixels
  • 3:45 - 3:48
    Alright, so that is looking really good
  • 3:48 - 3:50
    We've got a crazy dance party
  • 3:50 - 3:52
    Going on now
  • 3:52 - 3:54
    Now, if you pause a talkthrough
  • 3:54 - 3:56
    And try scrolling the page
  • 3:56 - 3:58
    You'll see that everything
  • 3:58 - 3:59
    Scrolls together
  • 3:59 - 4:01
    And the important thing is
  • 4:01 - 4:03
    That absolute positioning is relative
  • 4:03 - 4:04
    To the top of the webpage
  • 4:04 - 4:06
    So, as you scroll down the webpage
  • 4:06 - 4:08
    Things that were ten pixels
  • 4:08 - 4:11
    Top, are going to be moving off screen
  • 4:11 - 4:12
    Because you are going farther away from
  • 4:12 - 4:15
    The top of the webpage
  • 4:15 - 4:16
    Another option is
  • 4:16 - 4:17
    Fixed positioning
  • 4:17 - 4:19
    Which will actually make it seem like
  • 4:19 - 4:20
    Things don't move at all
  • 4:20 - 4:22
    And, if you want to try that out
  • 4:22 - 4:23
    We can just change
  • 4:23 - 4:26
    h1 from absolute to fixed
  • 4:26 - 4:29
    And now, pause and try scrolling
  • 4:29 - 4:31
    And you will see that
  • 4:31 - 4:34
    Dance party just stays in the same place
  • 4:34 - 4:36
    Because now, it is actaully
  • 4:36 - 4:39
    Relative to the top of the screen
  • 4:39 - 4:41
    The window
  • 4:41 - 4:44
    Ok, so we've managed to use
  • 4:44 - 4:46
    Three different position properties
  • 4:46 - 4:48
    To do some pretty cool stuff
  • 4:48 - 4:49
    When would we
  • 4:49 - 4:51
    Actually use absolute or fixed positioning
  • 4:51 - 4:53
    Well, you could use them
  • 4:53 - 4:54
    To make a game,
  • 4:54 - 4:56
    Like I did here
  • 4:56 - 4:57
    Because you'll want to layout all
  • 4:57 - 4:59
    The parts of the scene in the browser
  • 4:59 - 5:00
    But you can also use them
  • 5:00 - 5:02
    For normal webpages
  • 5:02 - 5:03
    Like on Khan Academy
  • 5:03 - 5:04
    We use absolute positioning for the models
  • 5:04 - 5:06
    That pop up in the middle of the page
  • 5:06 - 5:08
    And use fixed positioning
  • 5:08 - 5:09
    For the search box on out team page
  • 5:09 - 5:12
    So that it is always visible as you scroll
  • 5:12 - 5:14
    You probably won't use positioning
  • 5:14 - 5:15
    In every webpage
  • 5:15 - 5:17
    But when you do use it
  • 5:17 - 5:19
    You'll be really happy that it exists
Title:
CSS positioning
Description:

more » « less
Video Language:
English
Duration:
05:20
199coopere edited English subtitles for CSS positioning

English subtitles

Revisions