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;}
By creating a separate file describing a style, you can apply the style to all your pages by load the external style sheet.
Create a file with the CSS styling into a file with the extension .css.
File: mystyle.css
h1 { /* comments in CSS are different from comments in html */ color: blue; background: yellow; } p { font-style: italic; }
Then using the <link> tab include the CSS style sheet into the HTML file
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>
You can embed the style sheet using the <style>
element
whereever you want, but generally within the <head>
element.
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 method for creating CSS selectors
/* Select all paragraph element*/ p { color: red; } /* Select all h1 element*/ 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; }What about this?
p a em { color: blue; }
Styles come from many sources.
The style cascades down from universal specifications in the browser to the more specific ones you specify.
For example, most browsers define the styling of an <h1> element. If you do not specify a style for <h1> yourself, that style defined in the browser cascades down and becomes the effective style for <h1> in your page.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>s and <span>s in my HTML page?