Selectors identify specific elements to which styles will be applied. There are several different kinds of selectors.
6.1 Type selectors
They match DOM elements with the same name:
1 2 3 4 5
h1/* Selects all level 1 headings */ p/* Selects all paragraphs */ strong/* Selects all strong elements */ em/* Selects all em elements */ div/* Selects all divs */
6.2 Descendant selectors
These match elements that are contained by (or “descended from”) another element.
1 2
h1em/* Selects em elements contained in an h1 */ divp/* Selects p elements contained in a div */
6.3 Class selectors
These match elements of any type that have been assigned a specific class.
1 2 3
.caption /* Selects elements withclass"caption" */ .label /* Selects elements withclass"label" */ .axis /* Selects elements withclass"axis" */
Because elements can have more than one class, you can target elements with multiple classes by stringing the classes together, as in the following:
1 2 3
.bar.highlight/* Could target highlighted bars */ .axis.x/* Could target an x-axis */ .axis.y/* Could target a y-axis */
.axis could be used to apply styles to both axes, for example, whereas .axis.x would apply only to the x-axis.
6.4 ID selectors
These match the single element with a given ID. (Remember, IDs can be used only once each in the DOM.) IDs are preceded with a hash mark.
1 2 3
#header /* Selects element with ID "header" */ #nav /* Selects element with ID "nav" */ #export /* Selects element with ID "export" */
Selectors get progressively more useful as you combine them in different ways to target specific elements. You can string selectors together to get very specific results. For example:
1 2 3 4
div.sidebar /* Selects divs withclass"sidebar", but not other elements withthatclass */ #button.on /* Selects element with ID "button", but only when theclass"on"is applied */
Remember, because the DOM is dynamic, classes and IDs can be added and removed, so you might have CSS rules that apply only in certain scenarios.
<html> <head> <styletype="text/css"> p { font-size: 24px; font-weight: bold; background-color: red; color: white; } </style> </head> <body> <p>If I were to ask you, as a mere paragraph, would you say that I have style?</p> </body> </html>
8.2 Reference an external stylesheet from the HTML.
1 2 3 4 5 6 7 8 9
<html> <head> <linkrel="stylesheet"href="style.css"> </head> <body> <p>If I were to ask you, as a mere paragraph, would you say that I have style?</p> </body> </html>
8.3 Attach inline styles.
1
<p style="color: blue; font-size: 48px; font-style: italic;">Inline styles are kind of a hassle</p>
# rect <rect x="0"y="0"width="500"height="50"/> # circle <circle cx="250"cy="25"r="25"/> # ellipse <ellipse cx="250"cy="25"rx="100"ry="25"/> # line <line x1="0"y1="0"x2="500"y2="50"stroke="black"/> # text <text x="250"y="25">Easy-peasy</text> <text x="250"y="25"font-family="serif"font-size="25" fill="gray">Easy-peasy</text> # path
9.2 Styling SVG Elements
fill -> A color value. stroke -> A color value. stroke-width -> A numeric measurement (typically in pixels). opacity -> 0.0 (completely transparent) ~ 1.0 (completely opaque) font-family -> for text font-size -> for text