selectorA {
property1: value1;
property2: value2;
}
selectorB {
property1: value3;
property2: value4;
}
Estelle Weyl | front-end.social/@estelle | Github | Press → key to advance.
selectorA {
property1: value1;
property2: value2;
}
selectorB {
property1: value3;
property2: value4;
}
selector:pseudo-class::pseudo-element {
property: value;
}
selector[attribute],
selector ~ relation,
selector:directive(feature) {
property: value;
}
<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
1-0-0: ID selector0-1-0: Class selector (Also attribute selector & pseudoclass)0-0-1: Element SelectorThe * 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]
E[foo="bar" i]
input[id="myID" i]
If the attribute value is case sensitive
the attribute value in the selector is too
input[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 */
@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.
Based on current state of UI
:enabled :disabled :checked :indeterminate
input[type=checkbox]:checked + label {
color: red;
}
:default :valid :invalid :required :optional :in-range :out-of-range :read-only :read-write :placeholder-shown :user-invalid
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: pink;}
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"/>
:root :empty :blank :nth-child() :nth-last-child() :first-child :last-child :only-child :nth-of-type() :nth-last-of-type() :first-of-type :last-of-type :only-of-type
: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: hsl(205, 87%, 50%);
}
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(1):nth-last-of-type(4) ~ li {width: 25%;}
:root
Selects the document root, which is <html>
E:empty
<E/> <E></E> <E><!-- this is a comment --></E> <E title="this is an empty element"/>
E:not(s1)
E:not(s1, s2)
div:not(.excludeMe)
div:not(.excludeMe, .excuseYou)
E:is(s1, s2)
li:is([title], [role]) a {}
li[title] a,
li[role] a {}
:is(#home, #contact) aside :is(a:active, a:focus){}
#home aside a:active,
#contact aside a:active,
#home aside a:focus,
#contact aside a:focus {}
Relational "parent" selector
:has
Contains a header
header:has(h1, h2, h3, h4, h5, h6)
Contains no headers
header:not(:has(h1, h2, h3, h4, h5, h6))
Contains something that is not a header
header:has(:not(h1, h2, h3, h4, h5, h6))CanIUse.com
html[lang|="en"]
p:lang(en)
:lang(*-ch)
:dir(ltr|rtl)
<a> with an href attribute
:link
:visited
:any-link
:any-link is the same as :is(:link, :visited)
:hover :active :focus
Always style :focus when you style :hover
:focus-visible :focus-within
:hover :active :focus :focus-visible
a:visited:hover button:active:focus
Never, ever, ever do....
*:focus { outline: none; }
:target
myPage.html#anchor
<div id="anchor">ipsum lorem....
div:target::first-line {
font-weight: bold;
}
:current :future :past
:playing :paused
0-0-0: Global selector1-0-0: ID selector0-1-0: Class selector (Also attribute selector & pseudoclass)0-0-1: Element SelectorThe * selector, or global selector, has a specifity of 0.
Combinators, like ~, >, space, 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
.disabled {cursor: default !important;}
p.btn {cursor: pointer;}
v.
.disabled.disabled.disabled {cursor: default;}
p.btn {cursor: pointer;}
#TheirWidget {background-color: blue !important;}
#3rdPartyWidget {background-color: white;}
v.
#TheirWidget#TheirWidget {background-color: blue ;}
#3rdPartyWidget {background-color: white;}
a:not(#idDoesNotExist#idDoesNotExist#idDoesNotExist)
::first-line
::first-letter
::selection
::before
::after
Pseudo-classes select elements that
already exist.
Pseudo-elements create "faux" elements you can style.
.thisSlide *::selection {
background-color: #990000;
color: #ffffff;
}
<p id="x">the content</p>
the content
<p>
<before>before content - </before>
the content
<after> - after content</after>
</p>
Try it out Highlight Pseudo-elements:
::selection ::spelling-error ::grammar-error ::search-text ::target-text
Other Pseudo-elements:
::marker (<li>) ::placeholder (<input>) ::details-content (<details>)
.thisSlide *::selection {
background-color: #F63;
}