Aims:
id and class attributes
h1
{
color: red;
background-color: blue;
}
mystylesheet.css
head of the HTML document, include the following:
<link rel="stylesheet" href="mystylesheet.css" />(There are lots of bells and whistles that we'll ignore.)
head of the HTML
*
{
color: red;
}
h1
{
color : red;
}
h1, h2, em
{
color: red;
}
id attributesrc
with img)
id attribute
Running example: mojitos.html
#ingredients
{
color: red;
}
*#ingredients
{
color: red;
}
But this is redundant. Why?
section#ingredients
{
color: blue;
}
Is this redundant in general?
class attributeclass attribute
.cocktail
{
color: red;
}
*.cocktail
{
color: red;
}
But this is redundant. Why?
i.cocktail
{
color: red;
}
Is this redundant in general?
a' elements that are children of lis:
li > a
{
color: red;
}
a' elements that are descendants
of nav elements:
nav a
{
color: red;
}
nav li li
{
color: red;
}
nav li#current
{
color: red;
}
section ol, ul
{
color: red;
}
section ol, section ul
{
color: red;
}
<h1>Ingredients</h1>?
li's?ids in mojitos.html are probably unnecessary* | Matches all elements |
|---|---|
E | Matches all elements with tag name E |
E#I | Matches all E elements with id of I. Omitting E is the same as *#I |
E.C | Matches all E elements with class of c. Omitting E is the same as *.C |
E > F | Matches all F elements that are children of E elements |
E F | Matches all F elements that are descendants of E elements |
E + F | Matches all F elements that are immediately preceded by sibling E |
E ~ F | Matches all F elements preceded by sibling E |
E[A], to select an element E that has an attribute A
img[alt]
{
border: 1px solid red;
}
E[A="V"], to select an element E that has an attribute A with a particular value V
a[href="http://www.cs.ucc.ie/"]
{
background-color: yellow;
}
E[A*="S"], to select an element E that has an attribute A whose value contains a given substring S
a[href*="amazon"]
{
background-color: yellow;
}
E[A^="S"], to select an element E that has an attribute A whose value starts with a particular substring S
a[href^="http://"]
{
background-color: yellow;
}
E[A$="S"], to select an element E that has an attribute A whose value ends with a particular substring S
img[src$=".jpg"]
{
border: 1px solid red;
}
a:link
{
color: blue;
}
a:visited
{
color: red;
}
a:hover
{
color: yellow;
}
tr:hover
{
background-color: #DFE7F2;
}
li:first-child
{
color: blue;
}
li:last-child
{
color: yellow;
}
li:nth-child(3n+1)
{
color: red;
}
tr:nth-child(even)
{
background-color: #DFE7F2;
}
only-child, only-of-type, first-of-type, last-of-type,…)
section:not(#facts)