As noted in the introduction, Cascading Style Sheets comprise
a language to describe how the contents of an HTML document is
displayed, or “rendered.”
The CSS notes in this section are taken from Chapter 18 of your text.
But CSS is a very large topic, and you will
appreciate a good reference,
such as the ones at
Mozilla
or
W3Schools.
- Structure v. Appearance.
- The basic function of HTML is to express the structure of a document:
head, body, p, etc.
- Some HTML tags that change appearance are really structural tags:
em and strong specify the meaning (emphasis or strong
emphasis) and leave display up to the browser.
- Designers wanted more control.
- They prefer to use i and b, so they can control how
the text should be displayed.
- They like attributes such as align to control position
of the text.
- HTML sprouted more tags and attributes to control appearance.
- Cascading style sheets were created as a better way control appearance.
- CSS is a separate language which tells the browser how to present
the HTML.
- Key notion: Separation of structure and appearance.
- Style sheets control: colors, sizes, positions, font faces, styles
and sizes.
- Some styles duplicate controls available in HTML, some are in
addition.
- Using HTML style controls is deprecated, and new ones are not
added. New styling capbilities are added just to CSS.
- CSS instructions can appear in any of three places.
- In-line styles are given by a style attribute.
[ Inline Styles Source ]
- Most tags take a style.
- The <span>...</span> tag can be used for applying style to
arbitrary text.
- The <div>...</div> tag can be used for applying style to
larger sections involving multiple paragraphs or other block
items.
- Embedded style sheets. [ Embedded Styles Source ]
- Given in a <style>...</style>
section in the header.
- Often enclosed in comments to prevent cows by old browsers.
- Uses the name of a tag to tell what the styles
that tag should use: h1 { color: red }
- Advantages.
- Assign consistent styles for the whole page.
- Specify everything once in one place; One place to change.
- Can reduce page size.
- External style sheets are linked.
[ External Styles Source
Style Sheet ]
- In the header, say
<link rel="stylesheet" href="url" type="text/css">
instead of a <style>...</style> section.
- The type is probably not needed, but safe.
- Alternative: <style> @import url(url) </style>
- The first loads the page from HTML, the second starts a
section of CSS, and loads from CSS.
- The link can only be used from HTML, the import
can be used to load one style sheet into another.
- Advantages:
- Allows the styles for a site to specified in one place.
- Gives site a consistent look.
- Allows whole site's look to be changed by changing one document.
- Browser will cache the sheet, so download may be reduced
across the site.
- The controlling style is the one closest to the text. In-line beats
embedded, embedded beats external.
- Inheritance: Attributes are inherited from the enclosing tag.
For instance, the <body> styles are generally inherited
by everything in the body.