CSS Level 1 Selectors
- E
- .class
- #id
- E F
- :link
- :active
Estelle Weyl | @estellevw | Github | Press → key to advance.
Selectors define which elements & pseudo elements rulesets are applied to.
selectorA { property1: value1; property2: value2; } selectorB, selectorC { property1: value3; property2: value4; }
selector:pseudo-class::pseudo-element { property: value; } selector[attribute], selector ~ relation { property: -vendor-value; -vendor-property: -vendor-value; -vendor-property: experimentalValue; }
<ul> <li id="myID" class="myClass">item 1</li> <li class="myClass">item 2</li> <li>item 3</li> </ul>
E E F .class #ID :link :active
span#ID, .class, a.external:link { ... }
div#unique .repeated a.external:link { ... }
var chil = $('#bar .foo');
Natively
var el = document.querySelector('#bar'); var chil = el.querySelectorAll('.foo');
or
chil = document.querySelectorAll('#bar .foo');
function makeActive(slctr){ let i = j = 0, elements = document.querySelectorAll('.cur .ex li'), cur = document.querySelectorAll('.cur .ex ' + slctr); for (; i < elements.length; i++){ elements[i].classList.remove('active'); } for (; j < cur.length; j++){ cur[j].classList.add('active'); } }
Will be covered in depth later
1-0-0
: ID selector0-1-0
: Class selector (Also attribute selector & pseudoclass)0-0-1
: Element Selectorbody.blue div#someId a#otherId.classy { color: blue; } #myPage #mySection article.home p.important a { color: red; }
2-X-X X-2-X X-X-3
1-0-0
: ID selector0-1-0
: Class selector (Also attribute selector & pseudoclass)0-0-1
: Element SelectorThe * selector, or global selector, has no weight.
* {} 0-0-0
Combinators, like ~, >, and + have no weight
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]{ /* <p lang="en-us"> <p lang="fr-fr"> */ }
p[id="home"] {...}
p[lang|="en"]{ /* <p lang="en-us"> <p lang="en-uk"> */ }
a[title~="word"] { /* <a title="Three word sentence">*/ }
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)"; }
a[title*="word"] { /* <a title="Sentence with 4 words"> */ }
Multiple attributes work!
a[title][href]
input[type="number"][min][max]
E[title*="foobar" i]
E[title*="FooBar" i]
input[type="checkbox"]
input[type="ChEcKbOx"]
Relevant if attribute value is case senstive (Strings, not keywords)
E[id="foobar"]
E[id="FooBar"]
a[target="ABC" s ] /* example */
a[target="abc" s ]
Relevant if attribute value is NOT case sensitive in CSS.
case sensitivity in CSS is not exactly the same as in HTMLinput[placeholder] {/* matches any input with a placeholder */} input[type=email] {/* exact match */} abbr[title~=unicorn] {/* matches unicorn but not unicorns */} abbr[title|=en] {/* matches en-us and en-uk */} a[href^=mailto] {/* starts with */} a[href$=pdf]{/* ends in */} abbr[title*=unicorn] {/* matches unicorn and unicorns */} abbr[title*=UNICORN i] {/* matches unicorn and UnIcOrNs */}
E:[att] /* have 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 */ E:[att*=VAL i] /* anywhere as a case-insenstive substring */ E:[att*=VAL s] /* anywhere as a case-sensitive substring */
Note: The CSS is editable. Changing the CSS will impact the contents on the rest of the page.
:valid :invalid :required :optional :in-range :out-of-range :read-only :read-write :enabled :disabled
:checked :indeterminate :default
:placeholder-shown :blank :user-invalid
Based on the UI state of a checkbox
:checked :default :indeterminate :enabled :disabled
:checked
Based on the UI state of a checkbox
:checked + label
Example:indeterminate
<input type="number" min="5" max="7" required aria-required="true"/> <input type="number" min="0" step="0.1" max="10"/>
:user-invalid
:-moz-ui-invalid
:read-only :read-write :placeholder-shown :blank (:empty)
:read-write
& :read-only
:placeholder-shown
:placeholder-shown
When the placeholder attribute value is visible
:empty
E:empty
<E/> <E></E> <E><!-- this is a comment --></E> <E title="this is an empty element"/>
Changed to be like -moz-only-whitespace
<E> <!-- has white space --> </E>
Use case: empty JS content hooks, hide the bullet from empty list items, removing box-model values for margins collapsing,
:blank
No browser support...
E:blank
<input name="novalue" value="">
<textarea name="novalue" value=""></textarea>Not supported, but
:empty
matches