selectorA { property1: value1; property2: value2; } selectorB { property1: value3; property2: value4; }
selectorA { property1: value1; property2: value2; } selectorB { property1: value3; property2: value4; }
selector:pseudo-class::pseudo-element { -vendor-property: value; } selector[attribute], selector ~ relation { property: -vendor-value; -vendor-property: -vendor-value; -vendor-property: weirdsyntax; }
<ul> <li id="myID" class="myClass">item 1</li> <li class="myClass">item 2</li> <li>item 3</li> </ul>
var chil = $('#bar .foo');
Natively
var el = document.querySelector('#bar'); var chil = el.querySelectorAll('.foo');
or
chil = document.querySelectorAll('#bar .foo');
Reference combinator
E /attr/ F
label /for/ input
Parent selector maybe in CSS4
E! > F
:has()
function hasaclass(slctr){ var i = j = 0, elements = document.querySelectorAll('li'), cur = document.querySelectorAll(slctr); for (; i < elements.length; i++){ elements[i].classList.remove('active'); } for (; j < cur.length; j++){ cur[j].classList.add('active'); } }
Every slide in this deck has the class of 'slide'.
Using the console, figure out how many slides there are in the whole deck
The * selector, or global selector, has no value.
* {} 0-0-0
Combinators, like ~, >, and + have no value
ul li {} 0-0-2 ul > li {} 0-0-2
img[alt] {} <img src="image.jpg" alt="accessible"> <img src="image.jpg" title="inaccessible"> form [type] {} <input type=date> <select>
p[lang|="en"]{/* <p lang="en-us"> <p lang="en-uk"> */ }
a[title~=more] {/* <a title="want more info about this?">}
a[href^=mailto] {background-image: url(emailicon.gif);} a[href^=http]:after {content: " (" attr(href) ")";}
a[href$=pdf] {background-image: url(pdficon.gif);} a[href$=pdf]:after {content: " (PDF)";}
Note: Multiple attributes work! a[title][href]
@media print { abbr[title]:after { content: "(" attr(title) ")"; } a[href^=http]:after { content: "(" attr(href) ")"; } }
Note: The top line of the example is editable. The CSS will impact the contents on the rest of the page.
E[foo="bar" i]
input[type=checkbox i]Test page
:enabled :disabled :checked :indeterminate (Level 4)
input[type=checkbox]:checked + label { color: red; }
:valid :invalid :required :optional :in-range :out-of-range :read-only :read-write :default
input:valid { border: 1px solid green;} input:invalid { border: 1px solid red;} input:required, input[aria-required="true"] {border-width: 5px;} input:optional {border-width: 10px;} input:out-of-range { background-color: #cc0000;} input:in-range { background-color:lightgreen;}
<input type="number" min="5" max="7" required aria-required="true"/> <input type="number" min="0" step="0.1" max="10"/>
:nth-child() :nth-last-child() :nth-of-type() :nth-last-of-type() :first-child* :last-child :first-of-type :last-of-type :only-child :only-of-type :root :empty :not(:empty)
:first-child :last-child :first-of-type :last-of-type :only-child :only-of-type
Easier to explain by example
:nth-child(3n) :nth-last-child(odd) :nth-of-type(5) :nth-last-of-type(3n+1)
Target element or elements based on argument passed to the selector
li:first-child, li:last-child { font-weight: bold; } li:first-of-type, li:last-of-type{ text-decoration:line-through; } li:nth-child(even) { background-color: #CCC; } li:nth-child(3) { color: #CCC; } li:nth-of-type(odd) { background-color: #FFF; } li:nth-of-type(4n) { color: #ddaa00; } li:nth-of-type(3n-1) { text-align: right; }
li:only-of-type{width: 100%;} li:nth-of-type(1):nth-last-of-type(2), li:nth-of-type(2):nth-last-of-type(1){width: 50%;} li:nth-of-type(1):nth-last-of-type(3), li:nth-of-type(3):nth-last-of-type(1), li:nth-of-type(2):nth-last-of-type(2){width: 33.33%;} li:nth-of-type(1):nth-last-of-type(4), li:nth-of-type(2):nth-last-of-type(3), li:nth-of-type(3):nth-last-of-type(2), li:nth-of-type(4):nth-last-of-type(1){width: 25%;}
E:nth-match(n of selector) E:nth-last-match(n of selector)
li:nth-match(2n of .foo) li:nth-last-match(3n+1 of .bar)
E:not(s1)
div:not(.excludeMe)
CSS Selectors Level 4
E:not(s1, s2)
li:not(.foo, .bar)
E:matches(s1, s2)
li:matches([title], [role])
E:empty
p:empty
Matches:
<p></p> <p><!-- this is a comment --></p> <hr/> <img/> <br/> <input/>
CSS Selectors Level 4
E:blank
p:blank
Also matches:
<p> </p>
:link :visited
CSS Selectors Level 4
:any-link :local-link :local-link(n)
:local-link { text-decoration: none; color: inherit; }
a[href^=http],a[href^="http://"], a[href^="https://"], a:not(:local-link(0)){ /* external link */ }
:hover :active :focus
CSS Selectors Level 4
:active-drop :valid-drop :invalid-drop
:target
CSS Selectors Level 4
:scope
:lang(en)
CSS Selectors Level 4
:lang(zh, *-hant) :dir(ltr|rtl)
:root
Selects the document root, which is <html>
The * selector, or global selector, has no value.
* {} 0-0-0
Combinators, like ~, >, and + have no value
ul li {} 0-0-2 ul > li {} 0-0-2
:not has no value, but parameter selector does
.myClass:not(p) {} 0-1-1
Specificity is not inheritance
http://estelle.github.io/development/exercise/selectors.html - (solution)
::first-line ::first-letter ::selection (not in specification) ::before ::after
Pseudo-classes select elements that
already exist.
Pseudo-elements create "faux" elements you can style.
Use single colon because of IE
.thisSlide *::selection { background-color: #ddaa00; color: #333333; }
Prefix with -moz- for Firefox
Use single colon because of IE
.thisSlide *::selection { background-color: #ddaa00; color: #333333; }
For mobile but impacts desktop too:
.thisSlide { -webkit-tap-highlight-color: #bada55; -webkit-user-select: none; -webkit-touch-callout: none; }
p:before { content: "before content - "; font-weight: bold; } p:after{ content: " - after content"; font-weight: bold; }
<p>the content</p>
the content
<p> <before>before content - </before> the content <after> - after content</after> </p>↪
::-ms-browse ::-ms-value ::-ms-check ::-ms-clear ::-ms-expand ::-ms-fill ::-ms-fill-lower ::-ms-fill-upper ::-ms-reveal ::-ms-thumb ::-ms-ticks-after ::-ms-ticks-before ::-ms-tooltip ::-ms-track
Many, many more pseudo-elements with prefixes currently treated as a shadow DOM