W3C

Bert Bos & Eva Kasal | CSS3 in Style

Selectors

Help your selectors to reach their target without adding ids and classes in html.

Child selector: E > F
Sibling the child (F) of another element (E)

.sommaire_catalogue>li{
	background:url(../images/
    selectors/folder.png) no-repeat;
}

Selectors

Adjacent sibling combinator: E + F
targets an element (F) immediatly preceded by another (E)

h1+p{
	font-weight:bold;
	color:#777;
	font-size:1.4em;
	padding:1em;
}

Selectors

General sibling combinator: E ~ F
targets an element (F) preceded by another (E)
e.g.: each p following h1.english, both have the same parent (body there)

h1.english ~ p{
	font-weight:bold;
	color:#777;
	font-size:1.4em;
	padding:1em;
}

Selectors

Attribute selector: E[foo]
targets an element (E) with a "foo" attribute
e.g.: each img with an alt attribute

img[alt]{
	border:10px rgb(147,103,94) solid;
	border-radius:10px;
}

Selectors

Attribute selector: E:not(s)
targets an element (E) that does not match "s"
e.g.: each img with NO alt attribute

img:not([alt]){
	border:10px rgb(147,103,94) solid;
	border-radius:10px;
}

Selectors

Attribute selector: E[foo*="bar"]
targets an element (E) whose "foo" attribute contains the string "bar"
e.g.: each a whose "href" attribute contains the substring "glossaire"

a[href*="glossaire"]{
	color:gray;
}

Selectors

Attribute selector: E[foo^="bar"]
targets an element (E) whose "foo" attribute begins with the string "bar"
e.g.: each a whose "href" attribute begins with the substring "http"

a[href^="http"]{
	color:#6996d3;
	padding-left:1em;
	text-decoration:none;
}

Selectors

Attribute selector: E[foo$="bar"]
targets an element (E) whose "foo" attribute ends with the string "bar"
e.g.: each a whose "href" attribute ends with the substring ".pdf"

a[href$=".pdf"]{
	background:url(../images/
    selectors/pictopdf.gif) no-repeat 100% 50%;
}

Selectors

E:first-child
targets an element (E) that is the first child of its parent
e.g.: each <th> that is the first child of a <tr>

th:first-child{
	border-left:none;
}
th:nth-child(1){ /* is similar to :first-child */
	border-left:none;
}

Selectors

E:last-of-type
targets an element (E) that is the last child of its type in its parent
e.g.: each <th> that is the last th of a <tr>

th:last-of-type{
	border-bottom:none;
}

Selectors

E:nth-child(2n+4)
targets each 2nd E element beginning with the 4th child of its parent
e.g.: each 2nd <td> beginning with 4th child of its parent

td:nth-child(2n + 4){
	background:rgba(29, 53, 91, .3);
}

Selectors

E:nth-child(odd)
targets each E odd child of its parent
e.g.: each two <td> child of its parent

td:nth-child(odd){
	background:orange;
}

Selectors

:empty
targets each empty element

:empty{
	background:rgba(253,143,149, .5);
}

Selectors

:target
each element witch is the target of a link

:target{
	border:4px gold solid;
}


<a href="#grant">Un gringo...</a> <figure id="grant">I'm targetted when the user click on the link </figure>