CSS Programming/Inline

From Wikibooks, the open-content textbooks collection

< CSS Programming
Jump to: navigation, search
TODO

TODO
This page has been merged into CSS Programming/Applying CSS to (X)HTML and thus can be deleted or archived. Admittedly, its page history is still here. --Dan Polansky (talk) 13:13, 7 September 2008 (UTC)

[edit] Introduction

This section relates to the use of CSS in (X)HTML. See Applying CSS to XML for details of using CSS with XML.

The technique in this section does not have the maintenance benefit of using linked stylesheets. It should only be used during testing or for writing self-contained, single page examples. Production quality webpages and examples with more than one page should use linked stylesheets. Dynamically generated webpages may need to use inline CSS but this should be kept to a minimum. Even in dynamic pages any CSS that is common to multiple pages should generally be moved to a linked stylesheet.

It is expected that inline CSS will be removed from XHTML 2.0.

[edit] Inline CSS

Inline CSS is CSS specified directly in the element you want to apply it to. The style attribute contains the CSS.

For example

 <div style="font-weight:bold">Bold Font</div>

would render as:

Bold Font

The style attribute can contain more than one CSS declaration (font-weight:bold is a CSS declaration). The declarations are separated by semi-colons.

For example

 <div style="font-weight:bold;color:blue">Blue Bold Font</div>

would render as:

Blue Bold Font

[edit] A Better Technique

It is better practice to use the id attribute to identify an element that requires an individual style. The element can then be selected by a CSS selector in a linked or embedded stylesheet. This allows the possibility of providing different styles for the element in different media, e.g. screen or print.

The previous example would be rewritten as

 <div id="thisOne">Blue Bold Font</div>

and the CSS rule

 #thisOne { font-weight:bold;color:blue }

would be included in a linked stylesheet.

Personal tools
Create a book
  • Add wiki page
  • Collections help