1 of 61

CSS Layout Basics

2 of 61

Download the exercise/demo code

Download the code for this unit:

https://github.com/WebDevCourseMaterial/CssLayoutBasics

3 of 61

CSS Layout teaser

Demo first! Lets take a look at the Google CSS walkthrough to start learning about CSS layout.

Then we'll explain the details later.

We just want to see some of the properties in action via a demo first.

4 of 61

Box model

5 of 61

Example

6 of 61

Example

The visual box is 482 pixels wide and 282 pixels tall.

Visual box = Content size + padding + border

The margin is a transparent force field.

7 of 61

margin

http://www.w3schools.com/css/css_margin.asp

Granular properties:

margin-top: 100px;

margin-bottom: 100px;

margin-right: 50px;

margin-left: 50px;

Shorthand (top, right, bottom, left):

margin: 100px 50px;

margin: 100px 50px 100px;

margin: 100px 50px 100px 50px;

8 of 61

margin shorthand practice

Granular properties:

margin-left: 25px;

margin-right: 50px;

margin-top: 100px;

Write the shorthand:

margin: __________________________________ ;

9 of 61

margin shorthand practice

Shorthand:

div {

margin-right: 50px;

margin: 100px 20px;

}

How did the margins just get set?

margin-top: _________________________ ;

margin-bottom: _________________________ ;

margin-right: _________________________ ;

margin-left: _________________________ ;

10 of 61

padding

http://www.w3schools.com/css/css_padding.asp

Granular properties:

padding-top:25px;

padding-bottom:25px;

padding-right:50px;

padding-left:50px;

Shorthand (top, right, bottom, left):

padding:25px 50px;

padding:25px 50px 25px;

padding:25px 50px 25px 50px;

11 of 61

Box model with Chrome dev tools

Open the demo.

box_model/

three_boxes.html

Right click to inspect element.

12 of 61

Positioning the box model

Flow / block style:

  • display
  • position
  • float

Size:

  • width
  • height

Position:

  • left
  • top
  • right
  • bottom

These default to 'auto'

Defaults

display: block or inline

position: static

float: none

13 of 61

Display property

http://www.w3schools.com/cssref/pr_class_display.asp

p.block {

display: block;

}

p.inline {

display: inline;

}

p.inline-block {

display: inline-block;

}

p.none {

display: none;

}

14 of 61

Display block (user agent default for div)

display_property/

display_block.html

15 of 61

Display inline

display_property/

display_inline.html

width

height

16 of 61

Display inline-block

display_property/

display_inline_block.html

17 of 61

Display block, none, then inline

display_property/

display_block_none_inline.html

18 of 61

Position property

http://www.w3schools.com/cssref/pr_class_position.asp

div.static {

position: static;

}

div.relative {

position: relative;

}

div.absolute {

position:absolute;

}

div.fixed {

position: fixed;

}

19 of 61

position: static;

position_property/

position_static.html

right

top

20 of 61

position: relative;

position_property/

position_relative.html

21 of 61

position: absolute;

position_property/

position_absolute.html

Out of the flow

22 of 61

position: fixed;

position_property/

position_fixed.html

Out of the flow

23 of 61

Position property

http://www.w3schools.com/cssref/pr_class_position.asp

div.static {

position: static;

}

div.relative {

position: relative;

}

div.absolute {

position:absolute;

}

div.fixed {

position: fixed;

}

Positioned

Positioned

Positioned

Out of the flow

Out of the flow

24 of 61

Questions you should ask

  • Is it in the flow (do other elements leave space for it)?
    • Yes (static, relative) - It takes up space. Other element respect it.
      • Is it positioned (does it display outside in the space left)?
        • Yes (relative) - Leave in flow but adjust display position
        • No (static) - Ignore position and display in flow location
    • No (absolute, fixed) - Not in page flow (no space), forced to block display ignore display setting, always positioned

  • Is it block or inline (or inline-block)?
    • Block - Respect the size properties, line to itself. This is my horizontal space and I don't share!!!
    • Inline - Ignore the size properties, put in the flow, adjust location only if relative position
    • Inline-Block - Respect the size properties, however default is wrap-content instead of full line, other elements welcome on my line.

25 of 61

Display and position exercises

Work the first three exercises for display and position.

Exercise instructions

26 of 61

Interactions between properties

Flow / block style

  • display
  • position
  • float

Box model properties

  • margin
  • padding
  • border

Size

  • width
  • height

Position

  • left
  • top
  • right
  • bottom

27 of 61

Experiment time

When do the size and position properties get ignored?

block

inline

inline-block

static

???

???

???

relative

???

???

???

absolute

(fixed)

???

???

???

Size = width & height

Position = top, left, right, & bottom

28 of 61

block static

block = respect size

static = ignore position

29 of 61

block relative

block = respect size

relative = respect position

30 of 61

inline static

inline = ignore size

static = ignore position

31 of 61

inline relative

inline = ignore size

relative = respect position

32 of 61

inline-block static

inline-block = respect size

static = ignore position

33 of 61

inline-block relative

inline-block = respect size

relative = respect position

34 of 61

block absolute

absolute = ignore display, respect size, respect position

block = ignored (forced to block)

35 of 61

inline absolute

absolute = ignore display, respect size, respect position

block = ignored (forced to block)

36 of 61

inline-block absolute

absolute = ignore display, respect size, respect position

block = ignored (forced to block)

37 of 61

Experiment results

When do the size and position properties get ignored?

block

inline

inline-block

static

ignore position

ignore position

ignore size

ignore position

relative

ignore size

absolute

(fixed)

ignore display

force block

ignore display

force block

ignore display

force block

Size = width & height

Position = top, left, right, & bottom

38 of 61

Summary (in priority order)

  1. If display is none then display wins.
    1. It's like the element was never existed.
  2. If position is absolute or fixed then position wins
    • Out of the flow forced to block (inline meaningless)
  3. (intentionally left blank for now)
  4. Display as a happy combination
    • block static block relative
    • inline static inline relative
    • inline-block static inline-block relative

39 of 61

Organized results

block

inline

inline-block

static

ignore position

ignore position

ignore size

ignore position

relative

ignore size

All

absolute

(fixed)

ignore the display property force to block

2. Out of the flow

4. Happy combination

1. Display is none (don't even look at anything else)

40 of 61

Lab 0 - Lights Out CSS Only

With CSS

Without CSS

41 of 61

We're not done?

This matrix has another dimension.

Float Property

42 of 61

Float property

http://www.w3schools.com/cssref/pr_class_float.asp

img.right {

float: right;

}

img.left {

float: left;

}

img.none {

float: none;

}

43 of 61

Float none (the default)

float: none;

44 of 61

Float left

Both are

float: left;

45 of 61

Float right

Both are

float: right;

46 of 61

Clearing a float (nobody on my...)

Both are

float: left;

clear: left;

47 of 61

Float exercises

Work the three float exercises.

Exercise instructions

48 of 61

Experiment time?

block

inline

inline-block

static

???

???

???

relative

???

???

???

absolute

(fixed)

???

???

???

block

inline

inline-block

static

???

???

???

relative

???

???

???

absolute

(fixed)

???

???

???

block

inline

inline-block

static

???

???

???

relative

???

???

???

absolute

(fixed)

???

???

???

float: right;

float: left;

float: none;

49 of 61

Float set, but Absolute on

Position: absolute wins. Float setting is ignored (much like the display gets ignored)

float: right;

float: left;

50 of 61

Float left ignores display respect relative

51 of 61

Float right ignores display respect relative

52 of 61

Summary (in priority order)

  • If display is none then display wins.
    • It's like the element was never there.
  • If position is absolute or fixed then position wins
    • Out of the flow.
    • If float was set ignore. Force float to none.
    • If display was set ignore. Force display to block.
  • If float is left or right then float wins
    • If display was set ignore. Force to inline-block.
    • Respect relative property for position if set.
  • Display as an happy combination
    • block static block relative
    • inline static inline relative
    • inline-block static inline-block relative
    • float static float relative

53 of 61

Floats gone wild

Not as exciting as you might think

54 of 61

Starting point (no problems yet)

float: none;

55 of 61

Floats that don't fit on the line

float: left;

56 of 61

Same problem on the right side

float: right;

57 of 61

Float interacting with a non-float

float: left;

float: none;

58 of 61

Out of the flow with regard to parent

float: left;

clear: left;

59 of 61

Containing a float via overflow: hidden;

.wrapper {

overflow: hidden;

}

float: left;

clear: left;

Parent has the property

overflow hidden

60 of 61

Contain your floats

.row {

overflow: hidden;

}

61 of 61

CSS Layout example again

Demo again! Lets take a look at the Google CSS walkthrough again.

Can you guess correctly what every line will do?

Add to the end a rule to contain the floats.

#wd-c {overflow: hidden; }