Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

An Introduction to CSS

By Ben Hunt
2007-12-17


On-page CSS definitions

A better way to write CSS is to define your styles once in the document (preferably in the document <head> section).

Code

<head>
  <style type="text/css">
    div {
      background-color:#339;
      color:#fff;
      padding:15px;
      border-bottom:5px solid red;
      margin-bottom:15px;
    }
  </style>
</head>

<body>
  <div>
    I am affected by the definition above..
  </div>
  <div>
    So am I, but the styles are only defined once.
  </div>
</body>

Looks like

I am affected by the definition above..
So am I, but the styles are only defined once.

The benefit of this over the previous approach is that the style definitions are only written once. In this case, they'd apply to any <div> elements on the page.

Use this method when you want to apply similar styles to multiple elements on a page, but not on any other pages. To apply the same styles to things on multiple pages, you need to use method 3 below.



Tutorial Pages:
» The old way to style content
» How CSS works
» On-page CSS definitions
» Separate style sheets
» Advantages of separate stylesheets
» How CSS styles apply to HTML elements


 | Bookmark
Related Tutorials:
» Planning Your Stylesheet - The Definitive Guide
» CSS Shorthand Properties
» Print Stylesheet - The Definitive Guide
» Using CSS Selectors to Highlight Unedited Form Fields
» Overlapping tabbed navigation in CSS
» Cascading Style Sheets - CSS

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources