The basic idea: "I want the following HTML to look this way".
CSS selectors that specify the HTML segment selected {
define a style for the selection
}
Any HTML tag (like h1, p, li...etc) can serve as a CSS selector and the style specified will be applied to all occurrences of that specified tag.
h1 {
/* comments in CSS are different from comments in html */
/*
property: value ;
*/
color: blue;
background: yellow;
}
<style> element inside <head>
style attribute. Least preferred — mixes content and presentation.
By creating a separate file describing a style, you can apply the style to all your pages by loading the external style sheet.
Create a file with the CSS rules and save it with the
.css extension.
File: mystyle.css
h1 {
/* comments in CSS are different from comments in html */
color: blue;
background: yellow;
}
p {
font-style: italic;
font-weight: bold;
}
Then use the <link> tag inside <head> to load the CSS file into your HTML:
File: 01_external_css.html
<!DOCTYPE html> <html> <head> <title> My First HTML Page with External CSS </title> <link rel="stylesheet" href="mystyle.css" type="text/css" /> </head> <body> <h1> My Heading </h1> <p> This is a paragraph. </p> </body> </html>
Here, we are assuming that mystyle.css and the html file is in the same directory. If it is in another directory, specify the URL the same way you did with links (eg. absolute or relative pathnames).
<!DOCTYPE html>
<html>
<head>
<title> My First HTML Page with Embedded CSS </title>
<style>
h1 {
/* comments in CSS are different from comments in html */
color: blue;
background: yellow;
}
p {
font-style: italic;
}
</style>
</head>
<body>
<h1> My Heading </h1>
<p> This is a paragraph. </p>
</body>
</html>
Place the <style> element inside <head>. It must come before the body content so styles are loaded before the page renders.
This method is considered least desirable since they intertwine content with presentation.
<!DOCTYPE html> <html> <head> <title> My First HTML Page with Embedded CSS </title> </head> <body> <h1 style="color: red; background: blue;"> My Heading </h1> <p style="font-style: normal; font-weight: bold"> This is a paragraph. </p> </body> </html>
CSS selectors to specify the HTML parts {
define a style for the selection
}
Here are some methods for creating CSS selectors:
/* Select all paragraph elements */ p { color: red; } /* Select all h1 elements */ h1 { color: blue; } /* Select the body element */ body { background: black; }
Comma Separated Lists: applies the style rules to all the tags listed. Hence, the following:
ol, ul, dl {
color: red;
}
is the same as
ol { color: red; } ul { color: red; } dl { color: red; }
Contextual Selectors: used to specify elements in a specific context where the element is nested inside another element.
The following, without commas in between the elements, specifies only the <em> elements nested inside the <p> element.
p em {
color: blue;
}
<em> only when it is inside an <a> that is inside a <p>)
p a em {
color: blue;
}
Styles come from many sources.
When styles from different sources conflict, CSS has rules for which one wins. Sources with higher priority override lower-priority ones.
For example, most browsers define a default style for<h1>.
If you do not write your own rule for <h1>,
the browser's default applies — it wins by default because nothing overrides it.
As soon as you add your own rule, yours wins.

In the Elements panel, click any element to see which CSS rules apply to it. The Computed tab shows the final resolved value after all cascade rules have been applied — this is the one that actually controls what you see.
Now you can specify arbitrary areas of HTML code with CSS by using the <div> and <span> tag
What if I have more than one <div> or <span> in my HTML page and want to style them differently?
→ Use class and id attributes — covered next class.
Download recipe.html. You will apply all three kinds of CSS to the
<h1> heading and use DevTools to watch the cascade resolve in real time.
style.css in the same folder as your recipe:h1 {
background: yellow;
color: red;
}
Link it from <head> (that is in between the <head> and </head> tags!:
<link rel="stylesheet" href="style.css">
<style> block inside <head> after the <link>:
h1 {
color: green;
}
<h1> in a <span>:
<h1><span style="color: blue;">Fresh</span> Multi-Bean...</h1>
Before you run it — predict:
<h1> be?Confirm with DevTools:
color — what does it show?<h1> background is yellowcolor value for each partYour job: apply specific styles to exactly the right elements — without touching anything else.
Start by creating a new file targets.html and paste in this HTML:
<!DOCTYPE html>
<html>
<head>
<title>Selector Challenge</title>
<style>
/* your selectors go here */
</style>
</head>
<body>
<h2>Favorite Foods</h2>
<ul>
<li>Pizza with <em>extra cheese</em></li>
<li>Tacos</li>
<li>Ramen with <em>soft egg</em></li>
</ul>
<h2>Least Favorite Foods</h2>
<ol>
<li>Liver</li>
<li>Brussels sprouts with <em>butter sauce</em></li>
</ol>
<p>I will <em>never</em> eat liver.</p>
<p>But I <em>always</em> make room for pizza.</p>
</body>
</html>
Write one CSS rule for each target — using only what's inside the <style> block:
<em> inside <li> red. The <em> tags inside <p> should stay unstyled.<em> inside <ul> <li> bold. The one inside <ol> <li> should not be bold.<li> in the <ol> italic. The <ul> items should not be italic.Before you write any CSS — predict:
<em> inside a <li>?<ul> branch?Confirm with DevTools:
li em rule? Is it active or struck through?<p>) → Inspect. Does your rule appear at all?<ol>) → Inspect. Is it red? Is it bold? Should it be?