Home

How to Create External Styles in CSS3

|
|  Updated:  
2016-03-26 13:11:53
|   From The Book:  
No items found.
Personal Finance For Dummies
Explore Book
Buy On Amazon

Most developers use external styles in CSS3 to reduce the amount of work required to maintain a site. A single .CSS file contains all of the styles for the site, which means that changing a style site-wide is as simple as changing that one file (rather than each page). Because the change occurs in just one place, there isn’t any chance of missing one or more changes on individual pages.

Creating and using an external style sheet isn’t much different from using an internal style sheet. The following example uses standard techniques to create an external style sheet:

  1. Create a new HTML5 file with your text editor.

  2. Type the code for the HTML page.

    <!DOCTYPE html>
    <html>
    <head>
      <title>A Simple Page</title>
    </head>
    <body>
      <h1>A Simple Heading</h1>
      <p>Simple text to go with the heading.</p>
    </body>
    </html>

    Make sure you type the code precisely. What you should end up with is the same plain page — one without any styles.

  3. Type the following code immediately after the

    tag. <pre class="code"><link rel="stylesheet" href="ExternalCSS.CSS" /></pre> <p class="child-para">The <link> tag tells the browser to look for an external resource. In this case, the rel attribute says that it should look for a style sheet and the href attribute provides the name of that style sheet.</p>
  4. Save the HTML5 file to disk.

  5. Create a new .CSS file with your text editor.

    Your editor may not support .CSS files. Any text file will do.

  6. Type the following code in the .CSS file.

    p
    {
      font-family: cursive;
      font-size: large;
      color: #0000ff;
      background-color: #ffff00;
    }
    h1
    {
      font-family: "Times New Roman",Georgia,serif;
      font-size: 40px;
      text-align: center;
      text-decoration: underline;
      color: #ff0000;
      background-color: #00ffff;
    }

    This may be the same code that you used to create your internal file. The only difference is that it now resides in an external file.

  7. Save the CSS file to disk as ExternalCSS.CSS.

    It’s absolutely essential that the name of the file precisely match the name found in the href attribute. Some platforms are case sensitive, so you must use the same case for the filename. For example, externalcss.css might be viewed as a different file from ExternalCSS.CSS.

  8. Load the page in your browser.

    image0.jpg

About This Article

This article is from the book: 

No items found.

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.