css: selectors and the box model

45
CSS — a crash course in — selectors the box model Idan Gazit - @idangazit pyweb-il #20 / 25th Oct 2010

Upload: idan-gazit

Post on 13-Dec-2014

3.089 views

Category:

Documents


18 download

DESCRIPTION

 

TRANSCRIPT

Page 1: CSS: selectors and the box model

CSS— a crash course in —

selectorsthe box model

Idan Gazit - @idangazitpyweb-il #20 / 25th Oct 2010

Page 2: CSS: selectors and the box model

Selectors

The Box Model

Precompilers - next time

Page 3: CSS: selectors and the box model

SELECTORS

Page 4: CSS: selectors and the box model

h1 { color: red; font-size: 32px;}

selector declaration block

Page 5: CSS: selectors and the box model

h1 { color: red; font-size: 32px;}declaration

selector declaration block

Page 6: CSS: selectors and the box model

h1 { color: red; font-size: 32px;}declaration

selector declaration block

property value

Page 7: CSS: selectors and the box model

h1 { color: red; font-size: 32px;}

selector declaration block

Page 8: CSS: selectors and the box model

IN THE BEGINNING,THERE WAS THE DOM

Page 9: CSS: selectors and the box model

<!DOCTYPE HTML><html><head> <title>Show off the DOM!</title></head><body> <div id="content"> <p> Picture yourself on a boat in a river, with tangerine trees and marmalade skies. </p> </div></body></html>

Page 10: CSS: selectors and the box model

HTML

head body

title div

p

Page 11: CSS: selectors and the box model

RELATIONSHIPS

Ancestor/Descendant

Parent/Child

Sibling

Page 12: CSS: selectors and the box model

HTML

head body

title div

p

ancestor

descendant

descendant

descendant

Page 13: CSS: selectors and the box model

HTML

head body

title div

p

ancestor

descendant

descendant

child

parent

Page 14: CSS: selectors and the box model

HTML

head body

title div

p

siblingsibling

Page 15: CSS: selectors and the box model

http://flic.kr/p/C3C52

Page 16: CSS: selectors and the box model

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

Page 17: CSS: selectors and the box model

p.largep of class “large”

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

ptype

#navany element with id “nav”

.intro.largemultiple classes

IE6

Page 18: CSS: selectors and the box model

div > pchild

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS div pdescendant

h1 + padjacent sibling

IE6

IE6

*universal

Page 19: CSS: selectors and the box model

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

div ~ pgeneral later sibling

CSS 3

IE6

Page 20: CSS: selectors and the box model

img[alt=“foo”]<img alt=“foo” … >

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

img[alt]<img alt=“…anything…” … >

img[alt~=“foo”]<img alt=“blah foo bar” … >

img[alt|=“foo”]<img alt=“blah-foo-bar” … >

IE6

IE6

IE6

IE6

Page 21: CSS: selectors and the box model

img[alt^=“foo”]<img alt=“foobar” … >

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

img[alt$=“foo”]<img alt=“barfoo” … >

img[alt*=“foo”]<img alt=“barfoobar” … >

IE6

IE6

IE6

CSS 3

Page 22: CSS: selectors and the box model

PSEUDO-CLASSES

:first-child

:link:visited

:hover:active:focus

:lang(foo)

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

IE6 - links only

IE7 - links only

IE8

IE8 - still not 100%

Page 23: CSS: selectors and the box model

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

div>p:first-child<div> <p>yes!</p> <p>no</p></div>

IE8

Page 24: CSS: selectors and the box model

FOREVER ALONE

:first-of-type:last-of-type, :only-of-type

:last-child, :only-child

:not

:empty

:nth-child(…), :nth-of-type(…):nth-last-child(), :nth-last-of-type()

:target

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

FF3

None of this works in IE < 9.

CSS 3

FF3

opera

Page 25: CSS: selectors and the box model

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

div>p:first-of-type<div> <h1>no</h1> <p>yes!</p> <p>no</p></div>

CSS 3None of this works in IE < 9.

Page 26: CSS: selectors and the box model

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

li:only-child<ul> <li>no</li> <li>no</li> <li>no</li></ul><ul> <li>yes!</li></ul>

CSS 3None of this works in IE < 9.

Page 27: CSS: selectors and the box model

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

div.special *:not(ul)<div class= “special”> <h1>yes</h1> <p>yes</p> <ul>… no …</ul></div>

CSS 3None of this works in IE < 9.

Page 28: CSS: selectors and the box model

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

:nth-child(formula)An+B:“Every A’th element starting from B”3n: 0, 3, 6, 9…3n+1: 1, 4, 7, 10…

even (== 2n+1)odd (== 2n)

CSS 3None of this works in IE < 9.

Page 29: CSS: selectors and the box model

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

p:targetp:target { color: red; }

<nav> … <a href=“#about”>About Us</a></nav>…<p id=“about”> About us blah blah blah</p>

CSS 3None of this works in IE < 9.

Page 30: CSS: selectors and the box model

PSEUDO-ELEMENTS

:first-line, :first-letter

:before, :after

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

IE8

webkit, opera

Page 31: CSS: selectors and the box model

SPECIFIC

HIERARCHICAL

ATTRIBUTE

PSEUDO-CLASSES

PSEUDO-ELEMENTS

li.optional:beforeli.optional:before { font-color: red; content: “optional - ”}

<h1>Bring for hike:</h1><ul> <li>hat</li> <li>water</li> <li class=“optional”>camera</li></ul>

Bring for hike: • hat • water • optional - camera

IE8

Page 32: CSS: selectors and the box model

SPECIFICITYstyle=“…” attribute in an element

IDs

attributes, classes, pseudo-classes

elements, pseudo-elements

Later rules of same specificity trump earlier ruleshttp://www.w3.org/TR/CSS2/cascade.html#specificity

Page 34: CSS: selectors and the box model

THE BOX MODEL

Page 35: CSS: selectors and the box model

WTF?Doesn’t work the way you expect.

Page 36: CSS: selectors and the box model
Page 37: CSS: selectors and the box model
Page 38: CSS: selectors and the box model

HOW BIG AM I?div { width: 400px; height: 500px; padding: 10px; border: 1px solid black; margin: 20px;}

Page 39: CSS: selectors and the box model

HOW BIG AM I?

width: 400px +10 +10 +1 +1 = 422px

height: 500px +10 +10 +1 +1 = 522px

paddingleft + right

borderleft + right

paddingtop + bottom

bordertop + bottom

Page 40: CSS: selectors and the box model

Mathsize + padding + border = actual size

Page 41: CSS: selectors and the box model

More Mathwidth: auto;

containing block width- margin- border- padding= calculated width.

Page 42: CSS: selectors and the box model

Margin CollapsingYes, there are more surprises.

Page 43: CSS: selectors and the box model

POP QUIZ<div id=“first”>…</div><div id=“second”>…</div>

#first { margin-bottom: 30px;}

#second { margin-top: 20px;}

Page 45: CSS: selectors and the box model

Thank you!@idangazit

db.tt/D7TAX6X— or —

www.slideshare.net/idangazit/css-selectors-and-the-box-model