Lecture 15: Inline and External Styles

Overview:

  1. Inline CSS
  2. External CSS using <link />
  3. External CSS using @import

Inline CSS

Attribute Usage and Effect Values Accepted Deprecated?
style Used to specify CSS for an element. Can be applied to any tag valid inside <body>, including <body> itself. Any CSS property: value combination Not deprecated

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Inline CSS</title>
</head>
<body>
<h1 style="color: green; font-size: 24px; letter-spacing: 3px;">
This level 1 heading is styled</h1>

<h1>This level 1 heading is unstyled because the inline styles applied
to the other heading are restricted to that one tag.</h1>

</body>
</html>

See how this example renders

External CSS using <link />

Attribute Usage and Effect Value(s) to Use Deprecated?
href Contains the stylesheet address. Relative paths are preferred.

If the stylesheet is not found, modern browsers render the page without the styles. Netscape 4.x returns the server error page.

This needs to be included for the link to work.
File name of stylesheet, as well as directory path if the stylesheet is in a different directory than the (X)HTML file. Relative paths preferred. Not deprecated
media Specifies the environment / client device where the stylesheet will be applied.

This is covered in INP 170: Web Coding II.

media="all" can be used to 'hide' a style sheet from Netscape 4.x

Opera uses media="projection" in its full-screen mode

We will not be using the media attribute in this class.
all | aural | braille | embossed | handheld | print | projection | screen | tty | tv Not deprecated
rel Specifies the relationship between the documents. The <link /> tag can be used for more than just calling estylesheets.

Be sure to always specify this attribute.
rel="stylesheet" Not deprecated
title Useful only if you have 2 or more stylesheets brought in via <link />. The one with the title is given a secondary priority, as it is considered to be a preferred stylesheet, rather than a persistent stylesheet.

Persistent stylesheets are always applied, while preferred stylesheets are applied unless an alternate stylesheet is selected (at which point preferred stylesheets turn off).

Alternate stylesheets are covered in the more advanced coding classes.

This attribute is usually not included.
Text (e.g., title="larger") Not deprecated
type Specifies the content type of the linked document.

Be sure to always specify this attribute, as it allows the client device to skip the file if it does not support that content type.

Thus a device not supportive of CSS could just skip the stylesheet and not waste time and bandwidth downloading the file.
type="text/css" Not deprecated

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>&lt;link /&gt; CSS</title>
    <link rel="stylesheet" type="text/css" href="link.css" />
</head>
<body>
<h1>This level 1 heading is styled by an external CSS file</h1>

<h1>This level 1 heading is also styled by an external CSS file</h1>

</body>
</html>

The link.css file contains:

h1 {color: green; font-size: 24px; letter-spacing: 3px;}

See how this example renders

External CSS using @import

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Imported CSS</title>
    <style type="text/css"><!--
    @import url(link.css);
    --></style>
</head>
<body>
<h1>This level 1 heading is styled by an external CSS file</h1>

<h1>This level 1 heading is also styled by an external CSS file</h1>

</body>
</html>

The link.css file contains:

h1 {color: green; font-size: 24px; letter-spacing: 3px;}

See how this example renders