| |
External Style Sheet
|
An external style sheet is useful when the style is applied to many
pages to develop a standard look-and-feel for a site. With an external
style sheet included into each site page, you can change the look of
an entire Web site by changing the one style sheet file. Each page
must link to the style sheet using the <link> tag. The <link> tag goes
inside the head section:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
<body>
Normal <b>bold</b> and <i>italic</i><br>
Headline <b class="headline">bold</b> and green style <i class="green">italic</i>
<p>Regular text</p>
<p>More regular text</p>
</body>
|
The browser will read the style definitions from the file
mystyle.css, and format the document according to it. An external
style sheet can be written in any text editor. The file should not
contain any html tags. Your style sheet should be saved with a .css
extension. An example of a style sheet file is:
mystyle.css
B.headline {color:red; font-size:22px; font-family:arial}
I.green {color:green; font-size:34px}
|
Normal bold and italic
Headline bold and green style
italic
Regular text
More regular text. |
| |
Internal Style Sheet
|
An internal style sheet has style definitions embedded within the
HTML file which we have already seen. An HTML file with CSS to modify
only certain bold text red and italic text green:
<head>
<style>
B.headline {color:red; font-size:22px; font-family:arial}
I.green {color:green; font-size:34px}
</style>
</head>
<body>
Normal <b>bold</b> and <i>italic</i><br>
Headline <b class="headline">bold</b> and green style <i class="green">italic</i>
<p>Regular text</p>
<p>More regular text</p>
</body>
|
Normal bold and italic
Headline bold and green style
italic
Regular text
More regular text. |
| |
Inline Styles
|
An inline style loses many of the advantages of style sheets by
mixing content with presentation. Use this method sparingly, such as
when a style is to be applied to a single occurrence of an element. To
use inline styles you use the style attribute in the relevant tag. The
style attribute can contain any CSS property. The example shows how to
change the color and the left margin of a paragraph:
This <b style="color: red">Hello</b> <i style="color: green">World</i>! is red and green.
|
This Hello World!
is red and green |
| |
Multiple Style Sheets
Inheritance
|
Child properties are more specific than parent properties for the
same selector.
For example, a parent or external style sheet has these properties
for the h3 selector:
h3 { text-align: left; font-size: 8pt; color: red}
|
And a child or internal style sheet has these properties for the h3
selector:
h3 { text-align: right; font-size: 20pt}
|
If the page with the internal style sheet also links to the
external style sheet the properties for h3 will be:
text-align: right; font-size: 20pt; color: red
|
The color: red is inherited from the external style sheet and the
text-align: right and the font-size: 20pt is replaced by the
internal style sheet.
|