Home

How to Use an External Style Sheet for HTML5 and CSS3 Programming

|
|  Updated:  
2021-10-29 19:57:46
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon
CSS3 supports external style sheets. This technique allows you to define a style sheet as a separate document and import it into your web pages. To see why this might be attractive, take a look at the example.

image0.jpg

When you look at the code for externalStyle.html, you might be surprised to see no obvious style information at all!

<!DOCTYPE html>
<html lang = "en-US">
 <head>
 <meta charset = "UTF-8">
 <title>externalStyle.html</title>
 <link rel = "stylesheet"
   type = "text/css"
   href = "myStyle.css" />
 </head>
 <body>
 <h1>External Style</h1>
 <p>
  This page has styles set for paragraphs, body, and header 1.
 </p>
 <p>
  The styles are defined in an external style sheet.
 </p>
 </body>
</html>
Where you normally see style tags (in the header), there is no style. Instead, you see a tag. This special tag is used to connect the current document with another document.

How to define the external style

When you use an external style, the style elements aren’t embedded in the page header but in an entirely separate document.

In this case, the page is connected to a special file called myStyle.css. This file contains all the CSS rules:

/* myStyle.css */
body {
 background-color: #333300;
 color: #FFFFFF;
}
h1 {
 color: #FFFF33;
 text-align: center;
 font: italic 200% fantasy;
}
p {
 background-color: #FFFF33;
 color: #333300;
 text-align: right;
 border: 3px groove #FFFF33;
}
The style sheet looks just like a page-level style, except for a few key differences:
  • The style sheet rules are contained in a separate file. The style is no longer part of the HTML page but is an entirely separate file stored on the server. CSS files usually end with the .css extension.

  • There are no tags. These aren’t needed because the style is no longer embedded in HTML.

  • The code begins with a comment. The /* */ pair indicates a comment in CSS. Truthfully, you can put comments in CSS in the page level. External CSS files frequently have comments in them.

  • The style document has no HTML. CSS documents contain nothing but CSS. This comes closer to the goal of separating style (in the CSS document) and content (in the HTML document).

  • The document isn’t tied to any particular page. The great advantage of external CSS is reuse. The CSS document isn’t part of any particular page, but any page can use it.

How to reuse an external CSS style

External style sheets are really fun when you have more than one page that needs the same style. Most websites today use multiple pages, and they should share a common style sheet to keep consistency.

image1.jpg

The code shows how easily this is done:

<!DOCTYPE html>
<html lang = "en-US">
 <head>
 <meta charset = "UTF-8">
 <title>SecondPage.html</title>
 <link rel = "stylesheet"
   type = "text/css"
   href = "myStyle.css" />
 </head>
 <body>
 <h1>Second Page</h1>
 <p>
  This page uses the same style as
  <a href = "externalStyle.html">externalStyle.html</a>.
 </p>
 </body>
</html>
External style sheets have some tremendous advantages:
  • One style sheet can control many pages: Generally, you have a large number of different pages in a website that all share the same general style. You can define the style sheet in one document and have all the HTML files refer to the CSS file.

  • Global changes are easier: If you’re using external styles, you make a change in one place and it’s automatically propagated to all the pages in the system.

  • Separation of content and design: With external CSS, all the design is housed in the CSS, and the data is in HTML.

  • Easy upgrades: Because the design parameters of the entire site are defined in one file, you can easily change the site without having to mess around with individual HTML files.

The link tag

The tag is the key to adding a CSS reference to an HTML document. The tag has the following characteristics:
  • The tag is part of the HTML page. Use a tag in your HTML document to specify which CSS document will be used by the HTML page.

  • The tag only occurs in the header. Unlike the tag, the tag can occur only in the header.

  • The tag has no visual presence. The user can’t see the tag, only its effects.

  • The <link> tag is used to relate the document with another document. You use the tag to describe the relationship between documents.

  • The tag has a rel attribute, which defines the type of relationship. For now, the only relationship you’ll use is the stylesheet attribute.

  • The tag also has an href attribute, which describes the location of the other document.

Link tags are often used to connect a page to an externally defined style document.

Most people refer to the hyperlinks created by the anchor () tag as hyperlinks or links. This can lead to some confusion because, in this sense, the link tag doesn’t create that type of link.

How to specify an external link

To use the tag to specify an external style sheet, follow these steps:
  1. Define the style sheet.

    External style sheets are very similar to the ones you already know. Just put all the styles in a separate text document without the tags.

  2. Create a link element in the HTML page’s head area to define the link between the HTML and CSS pages.

    My link element looks like this:

     <link rel = "stylesheet"
       type = "text/css"
       href = "myStyle.css" />
  3. Set the link’s relationship by setting the rel = "stylesheet" attribute.

    Honestly, stylesheet is almost the only relationship you’ll ever use, so this should become automatic.

  4. Specify the type of style by setting type = "text/css".

  5. Determine the location of the style sheet with the href attribute.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].