Estelle Weyl | @estellevw | Github | Press → key to advance.
<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');
function hasaclass(selector){ var i = j = 0, elements = document.querySelectorAll('.current .ex li'), update = document.querySelectorAll('.current .ex ' + selector); for (; i < elements.length; i++){ elements[i].classList.remove('active'); } for (; j < update.length; j++){ update[j].classList.add('active'); } }
All supported since IE9, and all other browsers
img[alt] {} <img src="image.jpg" alt="accessible"> <img src="image.jpg" title="inaccessible"> form [type] {} <input type=date> <select>
IE6 sucks
IE7 support basic non-empty attribute
Note: Multiple attributes work! a[title][href]
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)";}
E[att] {/* has the attribute at all */} E[att=val] {/* exact */} E[att~=val] {/* val is a space separated word */} E[att|=val] {/* with a dash */} E[att^=val] {/* begins with val */} E[att$=val] {/* ends with val */} E[att*=val] {/* val is anywhere as a substring */}
@media print{ abbr[title]:after { content: "(" attr(title) ")"; } a[href^=http]:after { content: "(" attr(href) ")"; } }
Note: Multiple attributes work! a[title][href]
: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: #C61800; } 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%;}
div:not(.excludeMe)
Based on current state of UI
:enabled :disabled :checked
:invalid :required :optional
:in-range :out-of-range :read-only :read-write :default
Example:
input[type=checkbox]:checked + label { color: red; }
:lang :target :empty
link & user action pseudo-classes
:link :visited :hover :active :focus
:root
Selects the document root, which is <html>
::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
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>Try it out
.thisSlide *::selection { background-color: #990000; color: #ffffff; }
Prefix with -moz- for Firefox
Use single colon because of IE
.thisSlide *::selection { background-color: #990000; color: #ffffff; }
For mobile but impacts desktop too:
.thisSlide { -webkit-tap-highlight-color: #bada55; -webkit-user-select: none; -webkit-touch-callout: none; }
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