151
CSS BOX MODEL

Css Boxmodel

  • Upload
    mzulje

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

Box model in css

Citation preview

Page 1: Css Boxmodel

CSSBOX MODEL

Page 2: Css Boxmodel

What is thebox model?

Page 3: Css Boxmodel

The CSS box model describesthe rectangular boxes that are

created for every element in thedocument tree.

Page 4: Css Boxmodel

The box modelcomponents

Page 5: Css Boxmodel

Source: Kindly provided by Hicks Design under Creative Commons License: http://www.hicksdesign.co.uk/boxmodel/

Page 6: Css Boxmodel

MarginCan be applied to the outside ofelements. Margins create spacebetween the edge of an element

and any adjacent elements.

p {margin: 5px; }

Page 7: Css Boxmodel

Background-colorSets the background color of an

element.

p {background-color: #eee; }

Page 8: Css Boxmodel

Background-imageApplies a background image to

an element. Appears will on top ofany background-color.

p {background-image:url(a.gif); }

Page 9: Css Boxmodel

BorderSpecifies the width, color andstyle of a border that is then

added to the outside of an HTMLelement.

p {border: 5px solid red; }

Page 10: Css Boxmodel

PaddingCan be applied to the outside ofthe content area of an element.

Creates space between the edgeof the element and its content.

p {padding: 5px; }

Page 11: Css Boxmodel

Content areaCan be given width, height and

overflow - depending on the typeof box model.

p {width: 500px; }

Page 12: Css Boxmodel

OverflowDetermines what happens when

an element’s content is largerthan the content box.

p {overflow: hidden; }

Page 13: Css Boxmodel

Box modelsize options

Page 14: Css Boxmodel

There are three different ways thatwidth and height can be applied

to boxes.

Page 15: Css Boxmodel

SizedA box is sized when it is given an

explicit width or height(eg. pixels or ems)

Page 16: Css Boxmodel

Duis autem vel eum iriure dolor inhendrerit in vulputate velit essemolestie consequat, vel illum doloreeu feugiat nulla facilisis.

Sized(eg. width: 200px)

Page 17: Css Boxmodel

Duis autem vel eum iriure dolor inhendrerit in vulputate velit essemolestie consequat, vel illum doloreeu feugiat nulla facilisis. Sized

(eg. height: 200px)

Page 18: Css Boxmodel

StretchedA box is stretched when it

stretches to the full width orheight of the parent container or

the viewport.

Page 19: Css Boxmodel

Lorem ipsum dolor sit amet consect etuer adipi scing elit seddiam nonummy nibh euismod tinunt ut laoreet dolore magnaaliquam erat volut. Ut wisi enim ad minim veniam, quisnostrud exerci tatio.

Stretched(eg. width: 100%)

Page 20: Css Boxmodel

Collapsed“Collapsed” means the width or

height shrinks around the content.A box can collapse when width orheight are set to auto, or in some

cases left undefined.

Page 21: Css Boxmodel

Lorem ipsum dolor sitc amunt

Collapsed(eg. width: auto)

Page 22: Css Boxmodel

Lorem ipsum dolor sitc amunt Collapsed(eg. height: auto)

Page 23: Css Boxmodel

Multiple boxmodels

Page 24: Css Boxmodel

Many people assume that there isonly one box model, and it isused for all HTML elements.

Page 25: Css Boxmodel

This is not the case! There areactually seven different boxmodels - each with different

characteristics.

Page 26: Css Boxmodel

The seven different box models:

block box modelinline box modelinline-block box modeltable box modeltable cell box modelabsolute box modelfloat box model

1234567

Page 27: Css Boxmodel

1. Blockbox model

Page 28: Css Boxmodel

Block box appearance:Block boxes form visually a

blocks, they flow down the pagein normal flow.

Page 29: Css Boxmodel

Lorem ipsum dolor sit amet consect etuer adipi scingelit sed diam nonummy nibh euismod tinunt ut laoreetdolore magna aliquam erat volut.

This is a heading

Dolor sit amet consect etuer adipi scing elit sed diamnonu nibh euismod tinunt ut laoreet dolore magnaaliquam erat magna aliquam.

Block boxes

Page 30: Css Boxmodel

Block box width:By default, block boxes stretch tothe width of their containing blockor the width of the viewport. Their

width can be sized but cannotcollapse.

Page 31: Css Boxmodel

Lorem ipsum dolor sit amet consect etuer adipi scing elitsed diam nonummy nibh euismod tinunt ut laoreetdolore magna aliquam erat volut. Ut wisi enim ad minimveniam, quis nostrud exerci tatio.

Duis autem vel eum iriure dolor inhendrerit in vulputate velit essemolestie consequat, vel illum doloreeu feugiat nulla facilisis.

Stretched

Sized

Page 32: Css Boxmodel

Block box height:By default, block box heightscollapse. Their height can be

sized but not stretched.

Page 33: Css Boxmodel

Collapsed

Duis autem vel eum iriure dolorin hendrerit in vulputate velitesse molestie consequat, velillum eu feugiat nulla facilisis velillum dolore eu feugiat.

Duis autem vel eum iriure dolorin hendrerit in.

Sized

Page 34: Css Boxmodel

Block box margin:Affects all sides of the box.

Margin-collapse occurs when incontact with block level elements

above or below.

p {margin: 0 0 1.5em;

}

Page 35: Css Boxmodel

Duis autem vel eum iriure dolorin hendrerit in vulputate velitesse molestie consequat, velillum eu feugiat nulla facilisis vel.

Vulputate velit esse molestieconsequat, vel illum dolore eufeugiat nulla.

Margin collapse

Page 36: Css Boxmodel

Block box padding:Can be applied to all sides.

Padding is added to the overallwidth/height of the box.

p {padding: 5px;

}

Page 37: Css Boxmodel

Total width = 220px

content = 200px 10px 10px

padding

Page 38: Css Boxmodel

Block box border: Can be applied to all sides.Border width is added to the

overall width/height of the box.

p {border: 5px solid red;

}

Page 39: Css Boxmodel

Total width = 210px

content = 200px 5px5px

border

Page 40: Css Boxmodel

Block box overflow:Overflow can be applied to block

boxes.The default is overflowvalue is “visible”.

div {overflow: hidden;

}

Page 41: Css Boxmodel

Duis autem vel eum iriure dolorin hendrerit in vulputate velitesse molestie consequat, velillum eu feugiat nulla facilisis velillum dolore eu feugiat.

Horizontal overflow

Vertical overflow

Page 42: Css Boxmodel

Duis autem vel eum iriure dolorin hendrerit in vulputate velitesse molestie consequat, velillum eu feugiat nulla facilisis velillum dolore eu feugiat.

overflow: visible

Page 43: Css Boxmodel

Duis autem vel eum iriure dolorin hendrerit in vulputate velitesse molestie consequat, velillum eu feugiat nulla facilisis velillum dolore eu feugiat.

overflow: hidden;

Page 44: Css Boxmodel

overflow: scroll(adds scrollbars to

top and side)

Page 45: Css Boxmodel

overflow: auto(adds scrollbars to

affected axis)

Page 46: Css Boxmodel

Block box model:Block boxes use an additive

model. Padding and border areadded to overall width/height.

Page 47: Css Boxmodel

Total width = 230px

content = 200px 5px10px 5px 10px

padding

border

Page 48: Css Boxmodel

Note:Older versions ofInternet Explorer

and boxes

Page 49: Css Boxmodel

Win/IE5 uses a different methodto set widths and heights for

boxes. If padding and border areapplied to an element, their

measurements are subtractedfrom the overall width.

Page 50: Css Boxmodel

Total width = 200px

content set to 200px but will be 170px in IE5 5px10px 5px 10px

padding

border

Page 51: Css Boxmodel

2. Inlinebox model

Page 52: Css Boxmodel

Inline box appearance:Inline boxes form visually as

lines. Width, height and overflowdo not apply to inline boxes.

Page 53: Css Boxmodel

Lorem ipsum dolor sit amet consect etuer adipi scingelit sed diam nonummy nibh euismod tinunt ut laoreetdolore magna aliquam erat volut.

This is a heading

Dolor sit amet consect etuer adipi scing elit sed diamnonu nibh euismod tinunt ut laoreet dolore magnaaliquam erat magna aliquam.

Inline boxes

Page 54: Css Boxmodel

Inline box width:By default, inline box width

collapses. The width cannot besized or stretched.

Page 55: Css Boxmodel

dolor sit amet consect etuer adipi scinAnsed nibh euismod tinunt laoremagna aliquam erat volut nostrud ex

Collapsed width

Page 56: Css Boxmodel

Inline box height: By default, inline box height

collapses. The height cannot besized or stretched.

Page 57: Css Boxmodel

dolor sit amet consect etuer adipi scinAnsed nibh euismod tinunt laoremagna aliquam erat volut nostrud ex

Collapsed height

Page 58: Css Boxmodel

Inline box margin:Can be applied to all sides butonly affects content on left and

right sides.

Page 59: Css Boxmodel

dolor sit amet consect etuer adipi scinsed nibh euismod tinunt laoremagna aliquam erat volut nostrud ex

Margin affects sides only

Page 60: Css Boxmodel

Inline box padding: Can be applied to all sides butonly affects content on left and

right sides.

Page 61: Css Boxmodel

Padding affects sides only

dolor sit amet consect etuer adipi scinsed nibh euismod tinunt laoremagna aliquam erat volut nostrud ex

Page 62: Css Boxmodel

Inline box border: Can be applied to all sides butonly affects content on left and

right sides.

Page 63: Css Boxmodel

dolor sit amet consect etuer adipi scinsed nibh euismod tinunt laoremagna aliquam erat volut nostrud ex

Border affects sides only

Page 64: Css Boxmodel

Inline box overflow:Overflow does not apply to inline

elements.

Page 65: Css Boxmodel

Inline box model:Margin, padding and border affect

content on either side of inlineboxes only.

Page 66: Css Boxmodel

dolor sit amet consect etuer adipi scinS nibh euismod tinunt laamagna aliquam erat volut nostrud ex

Border and padding affect sides only

Page 67: Css Boxmodel

3. Inline blockbox model

Page 68: Css Boxmodel

Inline block box appearance:Inline block boxes appear within

lines of text like inline boxes.However, they have width, height,margin, border and padding like a

block box.

Page 69: Css Boxmodel

Lorem ipsum dolor sit amet consectetuer adipi scing elit sed diam nibh.

Inline block box

Page 70: Css Boxmodel

Inline block box width:By default, inline block box widthcollapses. The width can be sized

or stretched.

Page 71: Css Boxmodel

Lorem ipsum dolor sit amet consectetuer adipi scing elit sed diam nibh.

Lorem ipsum dolorsit amet consect etuer adipi scing elit

Loremipsum dolorsit amet consect etuer adipi scing elit

Collapsed width

Sized width

Stretched width

Page 72: Css Boxmodel

Inline block box height: By default, inline block box

height collapses. The height canbe sized but not stretched.

Page 73: Css Boxmodel

Lorem ipsum dolor sit amet consectetuer adipi scing elit sed diam nibh.

Collapsed height

Lorem ipsum dolor sit amet consect

etuer adipi scing elit sed diam nibh.Sized height

Page 74: Css Boxmodel

Inline block box margin:Affects all sides of the box.

p {display: inline-block;margin: 0 0 1.5em;

}

Page 75: Css Boxmodel

Lorem ipsum dolor sit amet laite a

consect etuer adipi scing elit sediumdiam nibh. Margin affects all sides

Page 76: Css Boxmodel

Inline block box padding:Can be applied to all sides.

p {display: inline-block;padding: 5px;

}

Page 77: Css Boxmodel

Lorem ipsum dolor sit amet laite a

consect etuer adipi scing elit sediumdiam nibh. Padding affects all sides

Page 78: Css Boxmodel

Inline block box border: Can be applied to all sides.

p {display: inline-block;border: 5px solid red;

}

Page 79: Css Boxmodel

Lorem ipsum dolor sit amet laite a

consect etuer adipi scing elit sediumdiam nibh.

Border affects all sides

Page 80: Css Boxmodel

Inline block box overflow:Overflow can be applied to inline

block boxes.

div {display: inline-block;overflow: hidden;

}

Page 81: Css Boxmodel

Lorem ipsum dolor sit amet consect moreetuer adipi scing elit sed diam nibh.

Overflow

Page 82: Css Boxmodel

Inline block box model:Inline block boxes use an additive

model. Padding and border areadded to overall width/height.

Page 83: Css Boxmodel

4. Tablebox model

Page 84: Css Boxmodel

Table box appearance:Be default, tables are unlike other

box models. Width and heightrefer to outside of the borders.

Page 85: Css Boxmodel

Table box width:By default, table box width

collapses. The width can be sizedand stretched.

Page 86: Css Boxmodel

n1 n2

n3 n4

n1 n2

n3 n4

n1 n2

n3 n4

Collapsed width

Sized width

Stretched width

Page 87: Css Boxmodel

Table box height: By default, table box heightcollapses. The height can be

sized (as long as the size is largerthan table cell content) but not

stretched.

Page 88: Css Boxmodel

n1 n2

n3 n4

n1 n2

Collapsed height

Sized height

n1 n2

Page 89: Css Boxmodel

Table box margin: Affects all sides of the box.

Margin-collapse occurs when incontact with block level elements

above and below.

Page 90: Css Boxmodel

n1 n2

n3 n4

n1 n2

n3 n4

Margin collapse

Page 91: Css Boxmodel

Table box padding:Can be applied to all sides.

Padding is subtracted from to theoverall width/height of the box.Padding is not applied whenborder-collapse property is

applied to table.

Page 92: Css Boxmodel

n1 n2

n3 n4

Table padding

n1 n2

n3 n4Table padding with border-collapse

Page 93: Css Boxmodel

Table box border: Can be applied to all sides.

Border width is subtracted fromthe overall width/height of the

box.

Page 94: Css Boxmodel

n1 n2

n3 n4

Table border

n1 n2

n3 n4Table border with border-collapse

Page 95: Css Boxmodel

Table box overflow:Overflow cannot be applied to

table boxes - but can be appliedto the <tbody> element.

Page 96: Css Boxmodel

Table box model:Padding & border are subtracted

from overall width/height.

Page 97: Css Boxmodel

Total width = 200px

content = 200px 5px10px 5px 10px

padding

border

Page 98: Css Boxmodel

5. Table cellbox model

Page 99: Css Boxmodel

Table cell box appearance:Table cells form visually side by

side within table rows.

Page 100: Css Boxmodel

n1

n3 n4

Table celln2

Page 101: Css Boxmodel

Table cell box width:By default, table cell width iscollapses. This width can besized and allowed to stretch.

Content such as long words, andreplaced elements can override

width.

Page 102: Css Boxmodel

n1 n2

n1 n2

n1 n2

Collapsed width

Sized width

Stretched width

n1 verylongwordhere

Content can override width

Page 103: Css Boxmodel

Table cell box height:By default, table cell box heightcollapses. This height can be

sized but not stretched.

Page 104: Css Boxmodel

n1 n2 Collapsed height

n1 n2 Sized height

Page 105: Css Boxmodel

Table cell box margin:Does not apply.

Page 106: Css Boxmodel

Table cell padding:Can be applied to all sides.

Padding is added to the overallwidth/height of the box.

Page 107: Css Boxmodel

n1 n2 Table cell padding

Page 108: Css Boxmodel

Table cell box border: Can be applied to all sides.Border width is added to the

overall width/height of the box.Borders between cells will include

space unless cellspacing orborder-collapse are applied.

Page 109: Css Boxmodel

n1

n3 n4

Table cell bordersn2

n1

n3 n4

Table cell borders with no cellspacing n2

Page 110: Css Boxmodel

Table cell box overflow:Overflow does not apply.

Page 111: Css Boxmodel

Table cell box model:Table cell boxes use an additivemodel. Padding and border areadded to overall width/height.

Page 112: Css Boxmodel

Total width = 230px

content = 200px 5px10px 5px 10px

padding

border

Page 113: Css Boxmodel

6. Absolutebox model

Page 114: Css Boxmodel

Absolute box appearance:Absolute boxes are removed fromflow. Other elements in flow will

ignore the absolute box.

Page 115: Css Boxmodel

Left, right, top, bottom:Absolutely positioned boxes are

positioned in relation to thenearest parent with position or theviewport (if no positioned parent

is present).

Page 116: Css Boxmodel

Absolute box width 1:By default, absolute boxes are setto “width: auto” which will collapse

the boxes width.

Page 117: Css Boxmodel

Width: autoModel: collapsed

Lorem ipsum dolor sit

Page 118: Css Boxmodel

Absolute box width 2: By default, absolute boxes are

set to “left: auto” and “right: auto”which will also collapse the boxes

width.

Page 119: Css Boxmodel

Left: autoRight: auto

Model: collapse

Lorem ipsum dolor sit

Page 120: Css Boxmodel

Absolute box width 3:If both left and right are set to “0”

the box is stretched.

Page 121: Css Boxmodel

Left: 0Right: 0

Width: autoModel: stretched

Lorem ipsum dolor sit

Page 122: Css Boxmodel

Absolute box width 4:If left is a value, width is a valueand right is auto, the box is sized

and offset from the left.

Page 123: Css Boxmodel

Left: 10pxRight: auto

Width: 200pxModel: sized

Lorem ipsum dolor sit

Page 124: Css Boxmodel

Absolute box width 5:If right is a value, width is a valueand left is auto, the box is sized

and offset from the right.

Page 125: Css Boxmodel

Right: 10pxRight: auto

Width: 200pxModel: sized

Lorem ipsum dolor sit

Page 126: Css Boxmodel

Absolute box height:Absolute box height works like

width.

Page 127: Css Boxmodel

Absolute box margin:Margins can be applied to all

sides. Margins push the box awayfrom its position.

Page 128: Css Boxmodel

Lorem ipsum dolor sit Absolute box with no margin

Lorem ipsum dolor sit

Absolute box with margin

Page 129: Css Boxmodel

Absolute box padding 1:Padding will expand a collapsed

or sized box.

Page 130: Css Boxmodel

Lorem ipsum dolor sit

Lorem ipsum dolor sit

Absolute box with no padding

Absolute box with padding

Page 131: Css Boxmodel

Absolute box padding 2:Padding will reduce the width orheight of the content area of a

stretched box.

Page 132: Css Boxmodel

Absolute box without padding

Lorem ipsum dolor sit

Lorem ipsum dolor sit

Absolute box with padding

Page 133: Css Boxmodel

Absolute box border 1: Border will expand a collapsed or

sized box.

Page 134: Css Boxmodel

Lorem ipsum dolor sit Absolute box with border

Lorem ipsum dolor sit

Absolute box with no border

Page 135: Css Boxmodel

Absolute box border 2:Border will reduce the width orheight of the content area of a

stretched box.

Page 136: Css Boxmodel

Absolute box without border

Lorem ipsum dolor sit

Absolute box with border

Lorem ipsum dolor sit

Page 137: Css Boxmodel

Absolute box overflow:Overflow can be applied to

absolute boxes.

div {overflow: hidden;

}

Page 138: Css Boxmodel

Lorem ipsum dolor sitamet consect etueradipi scing elit seddiam nonummy nibheuismod tinunt ut.

Overflow: visible

Page 139: Css Boxmodel

7. Floatbox model

Page 140: Css Boxmodel

Floated box appearance:A floated box is positioned withinthe normal flow, then taken out ofthe flow and shifted to the left orright as far as possible. Content

may flow along the side of a float.

Page 141: Css Boxmodel

Float box width:The default is “width:auto” whichwill collapse the float box to the

width of the widest line. Float boxwidth can be sized or stretched.

Page 142: Css Boxmodel

Lorem ipsum dolor sit amet consect etuer adipi scing elitsed diam nonummy nibh euismod tinunt ut laoreet.

Duis autem vel eum iriure dolor inhendrerit in vulputate velit ess.

Stretched

Sized

Lorem ipsum dolor sit amet

Collapsed

Page 143: Css Boxmodel

Float box height: The default is “height:auto” which

will collapse the float box to theheight of all of its child elements.Float box height can be sized but

not stretched.

Page 144: Css Boxmodel

Collapsed

Duis autem vel eum iriure dolorin hendrerit in vulputate velitesse molestie consequat, velillum eu feugiat nulla facilisis velillum dolore eu feugiat.

Duis autem vel eum iriure dolorin hendrerit in.

Sized

Page 145: Css Boxmodel

Float box margin: Can be applied to all sides.

Left/top margins push float boxaway from original position.Right/bottom margins affect

wrapping content.

Page 146: Css Boxmodel

Float box padding:Can be applied to all sides.

Padding is added to the overallwidth/height of the box.

Page 147: Css Boxmodel

Float box border: Can be applied to all sides.Border width is added to the

overall width/height of the box.

Page 148: Css Boxmodel

Float box overflow: Overflow can be applied to float

boxes.

Page 149: Css Boxmodel

Float box model:Float boxes use an additive

model. Padding and border areadded to overall width/height.

Page 150: Css Boxmodel

Total width = 230px

content = 200px 5px10px 5px 10px

padding

border

Page 151: Css Boxmodel

Russ WeakleyMax Design

Site: maxdesign.com.auTwitter: twitter.com/russmaxdesignSlideshare: slideshare.net/maxdesignLinkedin: linkedin.com/in/russweakley