INCLUDING CSS IN HTML DOCUMENTS

You can include CSS in an HTML document in three ways:

  • Inline styles — Using the style attribute in the HTML start tag.
  • Embedded styles — Using the <style> element in the head section of a document.
  • External style sheets — Using the <link> element, pointing to an external CSS file.

In this tutorial we will cover all these three methods for inserting CSS one by one.

Inline Styles

Inline styles are used to apply the unique style rules to an element by putting the CSS rules directly into the start tag. It can be attached to an element using the style attribute.

The style attribute includes a series of CSS property and value pairs. Each "property: value" pair is separated by a semicolon (;), just as you would write into an embedded or external style sheets. But it needs to be all in one line i.e. no line break after the semicolon, as shown here:

EXAMPLE
<h1 style="COLOR:RED; FONT-SIZE:30PX;">THIS IS A HEADING</H1>
<p style="COLOR:GREEN; FONT-SIZE:22PX;">THIS IS A PARAGRAPH.</P>
<div style="COLOR:BLUE; FONT-SIZE:14PX;">THIS IS SOME TEXT CONTENT.</DIV>

Using the inline styles are generally considered as a bad practice. As style rules are embedded directly inside the HTML tag, it causes the presentation to become mixed with the content of the document; which makes the code hard to maintain and negates the purpose of using CSS.

Embedded Style Sheets

Embedded or internal style sheets only affect the document they are embedded in.

Embedded style sheets are defined in the <head> section of an HTML document using the <style>element. You can define any number of <style> elements in a HTML document but they must appear between the <head> and </head> tags. Here’s an example:

Example

<!DOCTYPE HTML>
<html LANG="EN">
<head>
    <title>MY HTML DOCUMENT</title>
    <style type="text/CSS">
        BODY { background-color: yellow; }
        p { color: #fff; }
    </style>
</head>
<body>
    <h1>THIS IS A HEADING</h1>
    <P>THIS IS A PARAGRAPH OF TEXT.</P>
</body>
</html>

External Style Sheets

An external style sheet is ideal when the style is applied to many pages of the website.

An external style sheet holds all the style rules in a separate document that you can link from any HTML file on your site. External style sheets are the most flexible because with an external style sheet, you can change the look of an entire website by changing just one file.

You can attach external style sheets in two ways — linking and importing.

Linking External Style Sheets

Before linking, we need to create a style sheet first. Let’s open your favorite code editor and create a new file. Now type the following CSS code inside this file and save it as “style.css”.

EXAMPLE
body{
    background: lightyellow;
    font: 18px arial, sans-serif;
}
h1{
    color: orange;
}

An external style sheet can be linked to an HTML document using the <link> tag. The <link> tag goes inside the <head> section, as shown in this example:


<!DOCTYPE HTML>
<html LANG="EN">
<head>
    <title>MY HTML DOCUMENT</title>
    <LINK REL="stylesheet" href="CSS/style.CSS">
</head>
<body>
    <h1>THIS IS A HEADING</h1>
    <P>THIS IS A PARAGRAPH OF TEXT.</P>
</body>
</html>

Importing External Style Sheets

The @import rule is another way of loading an external style sheet. The @import statement instructs the browser to load an external style sheet and use its styles.

You can use it in two ways. The simplest is within the header of your document. Note that, other CSS rules may still be included in the <style> element. Here’s an example:

<style="TEXT/CSS">
    @IMPORT URL("CSS/STYLE.CSS");
    P {
        color: blue;
        FONT-SIZE: 16px;
    }
</style>

Similarly, you can use the @import rule to import a style sheet within another style sheet.

@IMPORT URL("CSS/LAYOUT.CSS");
@IMPORT URL("CSS/COLOR.CSS");
BODY {
    color: blue;
    font-size: 14px;
}

Categories: CSS