Slide 1

Slide 1 text

CSS Selectors Samnang Chhun March Web Design Meetup

Slide 2

Slide 2 text

Contents What’s CSS? What’s CSS Selector? Different Types of CSS Selectors CSS Specificity Best Practices Pre-processors

Slide 3

Slide 3 text

What’s CSS? “Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation semantics (the look and formatting) of a document written in a markup language. ” http://en.wikipedia.org/wiki/Cascading_Style_Sheets

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

CSS Selectors “In CSS, selectors are patterns used to select the element(s) you want to style. ” http://www.w3schools.com/cssref/css_selectors.asp

Slide 7

Slide 7 text

CSS Selectors Universal Selector Element Type Selector Class Selector ID Selector Attribute Selector Selector Grouping Combinators Pseudo-classes Pseudo-elements

Slide 8

Slide 8 text

Universal Selector * { ! -moz-box-sizing: border-box; ! -webkit-box-sizing: border-box; ! -moz-box-sizing: border-box; } http://css-tricks.com/things-it-might-be-funuseful-to-try-the-universal-selector-on/

Slide 9

Slide 9 text

Element Type Selector body { padding: 0; margin: 0; background: #ffffff; } p { background: yellow; }

Slide 10

Slide 10 text

Class Selector .intro { background-color: yellow; } p.error { background-color: red; }

Slide 11

Slide 11 text

ID Selector #first-name { color: green; } ul#navigation { display: inline-block; }

Slide 12

Slide 12 text

Attribute Selector input[type="submit"] { color: green; } a[alt~=flower] { background-color: yellow; } /* en, en-us, en-* */ p[lang|=en] { background-color: yellow; } a[href^=http:] { background-color: green; } a[src$=.png] { background-color: blue; } a[href*=”example.com”] { background-color: red; }

Slide 13

Slide 13 text

Selector Grouping #foo td, th { color: blue; } /* Equivalent to */ #foo td { color: blue; } th { color: blue; }

Slide 14

Slide 14 text

Combinators /* Descendant Selector */ ul.nav li { list-style: none; } /* Child Selector */ ul>li { list-style-type: disc; } /* Adjacent Sibling */ h2+p { background-color: yellow; } /* General Sibling Selector */ h2~p { background-color: yellow; }

Slide 15

Slide 15 text

Pseudo-classes a:visited, a:hover { text-decoration: underline; } li:first-child { color: green; } tr:nth-child(2n+1) { color: blue; } tr:nth-child(odd) { /* same match */ color: blue; } tr:nth-child(1) { color: blue; } tr:first-child { /* same match */ color: blue; } http://reference.sitepoint.com/css/css3psuedoclasses

Slide 16

Slide 16 text

Pseudo-elements p:first-letter { font-size: 1.75em; } p:first-line { color: green; } #breadcrumbs:before { content: "You are here:"; margin-right: 0.5em; } span.centimeters:after { content: "cm"; color: #cccccc; }

Slide 17

Slide 17 text

CSS Specificity Who is the winner?

Slide 18

Slide 18 text

CSS Specificity http://specificity.keegan.st/

Slide 19

Slide 19 text

Avoid a universal key selector. Make your rules as specific as possible. Remove redundant qualifiers. Avoid using descendant selectors, especially those that specify redundant ancestors. Use class selectors instead of descendant selectors. Best Practices https://developers.google.com/speed/docs/best-practices/rendering https://developer.mozilla.org/en-US/docs/CSS/Writing_Efficient_CSS

Slide 20

Slide 20 text

SASS (SCSS) Less Stylus Pre-processors

Slide 21

Slide 21 text

Thanks @samnangchhun [email protected]