Friday, September 25, 2009

CSS Frequently Asked Questions

Table of Contents

Credits and thanks to Håkon Wium Lie and Bert Bos for their contribution in writing this FAQ. Answers submitted by Håkon and Bert are marked with an asterisk (*).

Note: the main focus of this FAQ is the CSS styling language therefore the browser bugs or misinterpretations are not included. Should any of the properties or values did not "work" in your browser, it simply means that your browser does not support it.

General Questions

  1. What is CSS?
  2. What are Style Sheets?
  3. What is external Style Sheet? How to link?
  4. What is embedded style? How to link?
  5. What is inline style? How to link?
  6. What is imported Style Sheet? How to link?
  7. What is alternate Style Sheet? How to link?
  8. What is persistent style?
  9. What is preferred style?
  10. How do I combine multiple sheets into one?
  11. What is CSS rule 'ruleset'?
  12. What is CSS rule 'at-rule'?
  13. What is selector?
  14. What is CLASS selector?
  15. What is ID selector?
  16. What is contextual selector?
  17. What is attribute selector? [CSS2]
  18. What is parent-child selector? [CSS2]
  19. What is CSS declaration?
  20. What is 'important' declaration?
  21. What is property?
  22. What is shorthand property?
  23. What is value?
  24. What is initial value?
  25. Can I attach more than one declaration to a selector?
  26. What is class?
  27. What is grouping?
  28. What are pseudo-elements?
  29. What are pseudo-classes?
  30. What is cascade?
  31. What is cascading order?
  32. What are inline, block, parent, children, replaced and floating elements?
  33. How does inheritance work?
  34. Are Style Sheets case sensitive?
  35. Which characters can CSS-names contain?
  36. Can I include comments in my Style Sheet?
  37. Can Style Sheets and HTML stylistic elements be used in the same document?
  38. Which set of definitions, HTML attributes or CSS properties, take precedence?
  39. Can CSS be used with other than HTML documents?


General Questions

  1. What is CSS?

    CSS stands for Cascading Style Sheets and is a simple styling language which allows attaching style to HTML elements. Every element type as well as every occurance of a specific element within that type can be declared an unique style, e.g. margins, positioning, color or size.

    Go to top

  2. What are Style Sheets?

    Style Sheets are templates, very similar to templates in desktop publishing applications, containing a collection of rules declared to various selectors (elements).

    Go to top

  3. What is external Style Sheet? How to link?

    External Style Sheet is a template/document/file containing style information which can be linked with any number of HTML documents. This is a very convenient way of formatting the entire site as well as restyling it by editing just one file.

    The file is linked with HTML documents via the LINK element inside the HEAD element. Files containing style information must have extension .css, e.g. style.css.

    <HEAD>
    <LINK REL=STYLESHEET HREF="style.css" TYPE="text/css">
    </HEAD>

    Go to top

  4. What is embedded style? How to link?

    Embedded style is the style attached to one specific document. The style information is specified as a content of the STYLE element inside the HEAD element and will apply to the entire document.

    <HEAD>
    <STYLE TYPE="text/css">
    <!--
    P {text-indent: 10pt}
    -->
    </STYLE>
    </HEAD>

    Note: The styling rules are written as a HTML comment, that is, between <!-- and --> to hide the content in browsers without CSS support which would otherwise be displayed.

    Go to top

  5. What is inline style? How to link?

    Inline style is the style attached to one specific element. The style is specified directly in the start tag as a value of the STYLE attribute and will apply  exclusively to this specific element occurance.

    <P STYLE="text-indent: 10pt">Indented paragraph</P>

    Go to top

  6. What is imported Style Sheet? How to link?

    Imported Style Sheet is a sheet that can be imported to (combined with) another sheet. This allows creating one main sheet containing declarations that apply to the whole site and partial sheets containing declarations that apply to specific elements (or documents) that may require additional styling. By importing partial sheets to the main sheet a number of sources can be combined into one.

    To import a style sheet or style sheets include the @import notation or notations in the STYLE element. The @import notations must come before any other declaration. If more than one sheet is imported they will cascade in order they are imported - the last imported sheet will override the next last; the next last will override the second last, and so on. If the imported style is in conflict with the rules declared in the main sheet then it will be overridden.

    <LINK REL=STYLESHEET HREF="main.css" TYPE="text/css"><STYLE TYPE="text=css"><!--@import url(http://www.and.so.on.partial1.css);@import url(http://www.and.so.on.partial2.css); .... other statements--></STYLE>

    Go to top

  7. What is alternate Style Sheet? How to link?

    Alternate Style Sheet is a sheet defining an alternate style to be used in place of style(s) declared as persistent and/or preferred .

    Persistent style is a default style that applies when style sheets are enabled but can disabled in favor of an alternate style, e.g.:

    <LINK REL=Stylesheet HREF="style.css" TYPE="text/css">

    Preferred style is a default style that applies automatically and is declared by setting the TITLE attribute to the LINK element. There can only be one preferred style, e.g.:

    <LINK REL=Stylesheet HREF="style2.css" TYPE="text/css" TITLE="appropriate style description">

    Alternate style gives an user the choice of selecting an alternative style - a very convenient way of specifying a media dependent style. Note: Each group of alternate styles must have unique TITLE, e.g.:

    <LINK REL="Alternate Stylesheet" HREF="style3.css" TYPE="text/css" TITLE="appropriate style description" MEDIA=screen>
    <LINK REL="Alternate Stylesheet" HREF="style4.css" TYPE="text/css" TITLE="appropriate style description" MEDIA=print>

    Alternate stylesheets are not yet supported.

    Go to top

  8. What is persistent style?

    Se Alternate style

    Go to top

  9. What is preferred style?

    Se Alternate style

    Go to top

  10. How do I combine multiple sheets into one?

    To combine multiple/partial style sheets into one set the TITLE attribute taking one and the same value to the LINK element. The combined style will apply as a preferred style, e.g.:

    <LINK REL=Stylesheet HREF="default.css" TITLE="combined">
    <LINK REL=Stylesheet HREF="fonts.css" TITLE="combined">
    <LINK REL=Stylesheet HREF="tables.css" TITLE="combined">

    Go to top

  11. What is CSS rule 'ruleset'?

    There are two types of CSS rules: ruleset and at-rule. Ruleset identifies selector or selectors and declares style which is to be attached to that selector or selectors. For example P {text-indent: 10pt} is a CSS rule. CSS rulesets consist of two parts: selector, e.g. P and declaration, e.g. {text-indent: 10pt}.

    P {text-indent: 10pt} - CSS rule (ruleset)
    {text-indent: 10pt} - CSS declaration
    text-indent - CSS property
    10pt - CSS value

    Go to top

  12. What is CSS rule 'at-rule'?

    There are two types of CSS rules: ruleset and at-rule. At-rule is a rule that applies to the whole style sheet and not to a specific selector only (like in ruleset). They all begin with the @ symbol followed by a keyword made up of letters a-z, A-Z, digits 0-9, dashes and escaped characters, e.g. @import or @font-face.

    Go to top

  13. What is selector?

    CSS selector is equivalent of HTML element(s). It is a string identifying to which element(s) the corresponding declaration(s) will apply and as such the link between the HTML document and the style sheet.

    For example in P {text-indent: 10pt} the selector is P and is called type selector as it matches all instances of this element type in the document.

    in P, UL {text-indent: 10pt} the selector is P and UL (see grouping); in .class {text-indent: 10pt} the selector is .class (see class selector).

    Go to top

  14. What is CLASS selector?

    Class selector is a "stand alone" class to which a specific style isdeclared.  Using the CLASS attribute the declared style can then beassociated with any HTML element. The class selectors are created by a period followed by the class's name. The name can contain characters a-z, A-Z, digits 0-9, period, hyphen, escaped characters, Unicode characters 161-255, as well as any Unicode character as a numeric code, however, they cannot start with a dash or a digit. (Note: in HTML the value of the CLASS attribute can contain more characters).It is a good practice to name classes according to their function than their appearance.

    .footnote {font: 70%}  /* class as selector */

    <ADDRESS CLASS=footnote/>This element is associated with the CLASS footnote</ADDRESS>
    <P CLASS=footnote>And so is this</P>

    Go to top

  15. What is ID selector?

    ID selector is an individually identified (named) selector  to which a specific style is declared.  Using the ID attribute the declared style can then be associated with one and only one HTML element per document as to differentiate it from all other elements. ID selectors are created by a character # followed by the selector's name. The name can contain characters a-z, A-Z, digits 0-9, period, hyphen, escaped characters, Unicode characters 161-255, as well as any Unicode character as a numeric code, however, they cannot start with a dash or a digit.

    #abc123 {color: red; background: black}

    <P ID=abc123>This and only this element can be identified as abc123</P>

    Go to top

  16. What is contextual selector?

    Contextual selector is a selector that addresses specific occurrence of an element. It is a string of individual selectors separated by white space, a search pattern, where only the last element in the pattern is addressed providing it matches the specified context.

    TD P CODE {color: red}

    The element CODE will be displayed in red but only if it occurs in the context of the element P which must occur in the context of the element TD.

    TD P CODE, H1 EM {color: red} 

    The element CODE will be displayed in red as described above AND the element EM will also be red but only if it occurs in the context of H1

    P .footnote {color: red} 

    Any element with CLASS footnote will be red but only if it occurs in the context of P

    P .footnote [lang]{color: red} 

    Any element with attribute LANG will be red but only if it is classed as "footnote" and occurs in the context of P

    Go to top

  17. What is attribute selector? [CSS2]

    Attribute selector is a selector defined by 1) the attribute set to element(s), 2) the attribute and value(s),  3) the attribute and value parts:

    1a) A[title] {text-decoration: underline}
    All A elements containing the TITLE attribute will be underlined

    1b) A[class=name] {text-decoration: underline}
    The A elements classed as 'name' will be underlined

    2) A[title="attribute element"] {text-decoration: underline}
    The A elements containing the TITLE attribute with a value that is an exact match of the specified  value, which in this example is 'attribute element', will be underlined

    3) A[title~="attribute"] {text-decoration: underline}
    The A elements containing the TITLE attribute with a value containing the specified word, which in this example is 'attribute', will be underlined

    Go to top

  18. What is parent-child selector? [CSS2]

    Parent-child selector is a selector representing the direct descendent of a parent element. Parent-child selectors are created by listing two or more tilde (~) separated selectors.

    BODY ~ P {background: red; color: white}
    The P element will be declared the specified style only if it directly descends from the BODY element:
    <BODY><P>Red and white paragraph </P></BODY>

    BODY ~ P ~ EM {background: red; color: white}
    The EM element will be declared the specified style only if it directly descends from the P element which in its turn directly descends from the BODY element:
    <BODY><P><EM>Red and white EM</EM></P></BODY>

    Go to top

  19. What is CSS declaration?

    CSS declaration is style attached to a specific selector. It consists of two parts; property which is equivalent of HTML attribute, e.g. text-indent: and value which is equivalent of HTML value, e.g. 10pt. NOTE: properties are always ended with a colon.

    Go to top

  20. What is 'important' declaration?

    Important declaration is a declaration with increased weight. Declaration with increased weight will override declarations with normal weight. If both reader's and author's style sheet contain statements with important declarations the author's declaration will override the reader's.

    BODY {background: white ! important; color: black}

    In the example above the background property has increased weight while the color property has normal.

    Go to top

  21. What is property?

    Property is a stylistic parameter (attribute) that can be influenced through CSS, e.g. FONT or WIDTH. There must always be a corresponing value or values set to each property, e.g. font: bold or font: bold san-serif.

    Go to top

  22. What is shorthand property?

    Shorthand property is a property made up of individual properties that have a common "addressee". For example properties: font-weight, font-style, font-variant, font-size, font-family, refer to the font. To reduce the size of style sheets and also save some keystrokes as well as bandwidth they can all be specified as one shorthand property font, e.g.:

    H1     {font-weight: bold;        font-style: italic;         font-variant: small-caps;        font-size: 160%;        font-family: serif}

    can be all shorthanded to a space separated list:

    H1 {font: bold italic small-caps 160% serif}

    Note: To make things even simpler the line-height property can be specified together with the font-size property:

    H1 {font: bold italic small-caps 160%/170% serif}

    Go to top

  23. What is value?

    Value is a 'physical' characteristic of the property. Property declares what should be formatted, e.g. FONT  while value suggests how the property should be formatted, e.g. 12pt. By setting the value 12pt to the property FONT it is suggested that the formatted text be displayed in a 12 point font. There must always be a corresponding property to each value or set of values.

    H1 {font: bold 180%}

    In the example above the H1 selector is declared the FONT property which in its turn is declared the values BOLD and 180%.

    The values suggesting alternatives are specified in a comma separated list, e.g.

    H1 {font-family: font1, font2}

    Go to top

  24. What is initial value?

    Initial value is a default value of the property, that is the value given to the root element of the document tree. All properties have an initial value. If no specific value is set and/or if a property is not inherited the initial value is used. For example the background property is not inherited, however, the background of the parent element shines through because the initial value of background property is transparent. 

    <P style="background: red">Hello <strong>World</strong></P>
    Content of the element P will also have red background

    Go to top

  25. Can I attach more than one declaration to a selector?

    Yes. If more than one declaration is attached to a selector they must appear in a semi colon separated list, e.g.;

    Selector {declaration1; declaration2}
    P {background: white; color: black}

    Go to top

  26. What is class?

    Class is a group of 1) instances of the same element to which an unique style can be attached or 2) instances of different elements to which the same style can be attached.

    1) The rule P {color: red} will display red text in all paragraphs. By classifying the selector P different style can be attached to each class allowing the display of some paragraphs in one style and some other paragraphs in another style.

    2) A class can also be specified without associating a specific element to it and then attached to any element which is to be styled in accordance with it's declaration. All elements to which a specific class is attached will have the same style.

    To classify an element add a period to the selector followed by an unique name. The name can contain characters a-z, A-Z, digits 0-9, period, hyphen, escaped characters, Unicode characters 161-255, as well as any Unicode character as a numeric code, however, they cannot start with a dash or a digit. (Note: in HTML the value of the CLASS attribute can contain more characters). (Note: text between /* and */ are my comments).

    CSS
    P.name1 {color: red} /* one class of P selector */
    P.name2 {color: blue} /* another class of P selector */
    .name3 {color: green} /* can be attached to any element */

    HTML
    <P class=name1>This paragraph will be red</P>
    <P class=name2>This paragraph will be blue</P>
    <P class=name3>This paragraph will be green</P>
    <LI class=name3>This list item will be green</LI>

    It is a good practice to name classes according to their function than their appearance; e.g. P.fotnote and not P.green. In CSS1 only one class can be attached to a selector. CSS2 allows attaching more classes, e.g.:

    P.name1.name2.name3 {declaration}<P class="name1 name2 name2">This paragraph has three classes attached</P>

    Go to top

  27. What is grouping

    Grouping is gathering (1) into a comma separated list two or more selectors that share the same style or (2) into a semicolon separated list two or more declarations that are attached to the same selector (2).

    1. The selectors LI, P with class name .first and class .footnote share the same style, e.g.:

    LI {font-style: italic}
    P.first {font-style: italic}
    .footnote {font-style: italic}

    To reduce the size of style sheets and also save some typing time they can all be grouped in one list.

    LI, P.first, .footnote {font-style: italic}

    2. The declarations {font-style: italic} and {color: red} can be attached to one selector, e.g.:

    H2 {font-style: italic}
    H2 {color: red}

    and can also be grouped into one list:

    H2 {font-style: italic; color: red}

    Go to top

  28. What are pseudo-elements?

    Pseudo-elements are fictional elements that do not exist in HTML. They address the element's sub-part (non-existent in HTML) and not the element itself. In CSS1 there are two pseudo-elements: 'first-line pseudo-element' and 'first-letter pseudo-element'. They can be attached to block-level elements (e.g. paragraphs or headings) to allow typographical styling of their sub-parts. Pseudo-element is created by a colon followed by pseudo-element's name, e.g:

    P:first-lineH1:first-letter

    and can be combined with normal classes; e.g:

    P.initial:first-line

    First-line pseudo-element allows sub-parting the element's first line and attaching specific style exclusively to this sub-part; e.g.:

    P.initial:first-line {text-transform: uppercase}

    <P class=initial>The first line of this paragraph will be displayed in uppercase letters</P>

    First-letter pseudo-element allows sub-parting the element's first letter and attaching specific style exclusively to this sub-part; e.g.:

    P.initial:first-letter { font-size: 200%; color: red}

    <P class=initial>The first letter of this paragraph will be displayed in red and twice as large as the remaining letters</P>

    Go to top

  29. What are pseudo-classes?

    Pseudo-classes are fictional element types that do not exist in HTML. In CSS1 there is only one element type which can be classed this way, namely the A element (anchor).  By creating three fictional types of the A element individual style can be attached to each class. These three fictional element types are: A as unvisited link, A as active link and A as visited link. Pseudo-classes are created by a colon followed by pseudo-class's name. They can also be combined with normal classes, e.g.:

    A:link {background: black; color: white}A:active {background: black; color: red}A:visited {background: transparent; color: black}

    <A HREF....>This anchor (or rather these anchors) will be displayed as declared above</A>

    A.foot:link {background: black; color: white}A.foft:active {background; black: color: red}A.foot:visited {background: transparent; color: black}

    <A CLASS=foot HREF....>This anchor and all other anchors with CLASS foot will be displayed as declared above</A>

    Go to top

  30. What is cascade?

    Cascade is a method of defining the weight (importance) of individual styling rules thus allowing conflicting rules to be sorted out should such rules apply to the same selector. 

    Declarations with increased weight take precedence over declaration with normal weight:

    P {color: white ! important}  /* increased weight */
    P (color: black} /* normal weight */

    Go to top

  31. What is cascading order?

    Cascading order is a sorting system consisting of rules by which declarations are sorted out so that there are not conflicts as to which declaration is to influence the presentation. The sorting begins with rule no 1. If a match is found the search is over. If there is no match under rule no 1 the search continues under rule no 2 and so on.

    1. Find all declarations that apply to a specific selector/property
      and
      Declare the specified style if the selector matches the element
      if there isn't any
      Let the element inherit its parent property
      if there isn't any
      Use initial value

    2. Sort by weight (! important)
      Increased weight take precedence over normal weight

    3. Sort by origin
      Rules with normal weight declared in author's style sheet will override rules with normal weight declared in user's personal style sheets
      Rules with increased weight declared in user's personal style sheet will override rules with normal weight declared in author's style sheet
      Rules with increased weight declared in author's style sheet will override rules with increased weight declared in user's personal style sheets
      Author's and user's rules will override UA's default style sheet.

    4. Sort by selector's specificity
      More specific selector will override less specific one:

      ID-selector (most specific), followed by
      Classified contextual selectors (TABLE P EM.fot)
      Class selectors (EM.fot)
      Contextual selectors - the "lower down" the more weight, (TABLE P EM), (TABLE P EM STRONG) - STRONG has more weight than EM.

    5. Sort by order specified
      If two rules have the same weight, the latter specified overrides ones specified earlier. Style sheets are sorted out as follows:

      The STYLE attribute (inline style) overrides all other styles
      The Style element (embedded style) overrides linked and imported sheets
      The LINK element (external style) overrides imported style
      The @import statement - imported style sheets also cascade with each other in the same order as they are imported

    Go to top

  32. What are inline, block, parent, children, replaced and floating elements?
    Inline
    elements which do not have line breaks. Can occur in block elements or other inline elements, cannot contain block elements.

    Inline elements in HTML 3.2; EM, STRONG, DFN, CODE, SAMP, KBD, VAR, CITE, TT, I, B, U, STRIKE, BIG, SMALL, SUB, SUP, A, IMG, APPLET, FONT, BASEFONT, BR, SCRIPT, MAP, INPUT, SELECT, TEXTAREA.

    Inline elements in HTML 4.0; EM, STRONG, DFN, CODE, SAMP, KBD, VAR, CITE, ABBR, ACRONYM, TT, I, B, BIG, SMALL, SUB, SUP, A, IMG, OBJECT, BR, SCRIPT, MAP, Q, SPAN, BDO, INPUT, SELECT, TEXTAREA, LABEL, BUTTON, (INS, DEL).

    Inline elements in HTML 4.0 Transitional; EM, STRONG, DFN, CODE, SAMP, KBD, VAR, CITE, ABBR, ACRONYM, TT, I, B, U, S, STRIKE, BIG, SMALL, SUB, SUP, A, IMG, APPLET, OBJECT, FONT, BASEFONT, BR, SCRIPT, MAP, Q, SPAN, BDO, IFRAME, INPUT, SELECT, TEXTAREA, LABEL, BUTTON, (INS, DEL).

    Block
    elements which do have line breaks. May occur in other block elements, cannot occur in inline elements, may contain both block and inline elements.

    Block elements in HTML 3.2; H1, H2, H3, H4, H5, H6, ADDRESS, P, DL, DT, DD, UL, OL, DIR, MENU, LI, DIV, CENTER, BLOCKQUOTE, PRE, HR, ISINDEX, TABLE, FORM.

    Block elements in HTML 4.0; P, H1, H2, H3, H4, H5, H6, UL, OL, PRE, DL, DIV, NOSCRIPT, BLOCKQUOTE, FORM, HR, TABLE, FIELDSET, ADDRESS, (INS, DEL).

    Block elements in HTML 4.0 Transitional; P, H1, H2, H3, H4, H5, H6, UL, OL, DIR, MENU, PRE, DL, DIV, CENTER, NOSCRIPT, NOFRAMES, BLOCKQUOTE, FORM, ISINDEX, HR, TABLE, FIELDSET, ADDRESS, (INS, DEL).

    Parents and children
    elements which either contain (parents) or are in the content of (children) other elements, e.g. <P>text<STRONG>text</STRONG>text</P>. P is a parent of STRONG. STRONG is a child of P. If not specified otherwise, children will inherit parent's properties. NOTE: not all properties are inherited. For more information, see INHERITANCE.
    Replaced
    elements which content is replaced. For example content of the IMG element is replaced with an image, content of the INPUT element is replace with a field.
    Floating
    elements which follow the flow of a parent - inline elements.

    Go to top

  33. How does inheritance work?

    HTML documents are structured hierarchically. There is an ancestor, the toplevel element, the HTML element, from which all other elements (children)are descended. As in any other family also children of the HTML family caninherit their parents, e.g. color or size.

    By letting the children inherit their parents a default style can be createdfor top level elements and their children. (Note: not all properties can be inherited).  The inheritance starts at the oldest ancestor and is passed on to its children and then their children and the children's children and so on.

    Inherited style can be overridden by declaring specific style to childelement.  For example if the EM element is not to inherit its parent P thenown style must be declared to it. For example:

    BODY {font-size: 10pt}
    All text will be displayed in a 10 point font

    BODY {font-size: 10pt}
    H1 {font-size: 14pt} or H1 {font-size: 180%}

    All text except for the level 1 headings will be displayed in a 10 point font. H1 will be displayed in a 14 point font (or in a font that is 80% larger than the one set to BODY). If the element H1 contains other elements, e.g. EM then the EM element will also be displayed in a 14 point font (or 180%) it will inherit the property of the parent H1. If the EM element is to be displayed in some other font then own font properties must be declared to it, e.g:

    BODY {font-size: 10pt}
    H1 {font-size: 14pt} or H1 {font-size: 180%}
    EM {font-size: 15pt} or EM {font-size: 110%}

    The EM element will be displayed in a 15 point font or will be 10% larger than H1. NOTE: EM is, in this example, inside H1 therefore will inherit H1's properties and not BODY's.

    The above declaration will display all EM elements in 15 point font or font that is 10% larger than font declared to the parent element. If this specific font is to apply to EM elements but only if they are inside H1 and not every occurrence of EM then EM must take a form of a contextual selector.

    H1 EM {font-size: 15pt} or H1 EM {font-size: 110%}

    In the example above EM is a contextual selector. It will be displayed in specified font only if it will be found in the context of H1.

    Not all properties are inherited. One such property is background. However, since it's initial value is transparent the background of the parent element will shine through by default unless it is explicitly set.

    Go to top

  34. Are Style Sheets case sensitive?

    No. Style sheets are case insensitive. Whatever is case insensitive in HTML is also case insensitive in CSS. However, parts that are not under control of CSS like font family names and URLs can be case sensitive - IMAGE.gif and image.gif is not the same file.

    Go to top

  35. Which characters can CSS-names contain?

    The CSS-names; names of selectors, classes and IDs can contain characters a-z, A-Z, digits 0-9, period, hyphen, escaped characters, Unicode characters 161-255, as well as any Unicode character as a numeric code. The names cannot start with a dash or a digit. (Note: in HTML the value of the CLASS attribute can contain more characters).

    Go to top

  36. Can I include comments in my Style Sheet?

    Yes. Comments can be written anywhere where whitespace is allowed and are treated as white space themselves. Anything written between /* and */ is treated as a comment (white space). NOTE: Comments cannot be nested.

    /* This is a CSS-comment */

    Go to top

  37. Can Style Sheets and HTML stylistic elements be used in the same document?

    Yes. Style Sheets will be ignored in browsers without CSS-support and HTML stylistic elements used.

    Go to top

  38. Which set of definitions, HTML attributes or CSS properties, take precedence?

    CSS properties take precedence over HTML attributes. If both are specified, HTML attributes will be displayed in browsers without CSS support but won't have any effect in browsers with CSS support.

    Go to top

  39. Can CSS be used with other than HTML documents?

    Yes. CSS can be used with any ny structured document format. e.g. XML, however, the method of linking CSS with other document types has not been decided yet (Feb 98).

    Go to top

What’s the Difference Between HTML and XHTML


I just got an email from a budding web-designer friend that said:

What the hell is the difference between XHTML and HTML?!?!? Am I just stupid? Is
it hard to switch over? I don’t even know what the hay I’m talking about.

I replied back with a quick off-the-cuff comparison between the two - the
differences and the reasons the two standards exist - and thought other people
might benefit. So, I’m posting it here.

Now, I don’t profess to be an expert, and I’m sure there are plenty of nuances
I’ve overlooked. Feel free to contribute in the comments, but keep in mind that
I’m purposely making this explanation as simple as possible and leaving out the
gory details.

It’s a tricky question to answer, but here goes . . .

There’s a computer language (not programming language!) called XML. It stands
for extensible markup language. Basically, it’s just a standard way of storing
data (such as a payroll report, a web page, the number of cows in your barn,
anything) that can be easily read by people and parsed (understood) by
computers. Take a look at this XML code I’m making up on the spot . . .





10



1

2







8

30





Basically, that says I have a farm with a Barn and a Field. The barn has 10
horses and some tools (a hammer and two shovels). There’s also a field with 8
cows and 30 pigs.

That make sense? It’s a very basic example, but it shows how you can use XML to
store any sort of data you want. It just has to follow the proper structure.

Ok, so that’s XML in a (very small) nutshell.

HTML was created in the early 90’s by Tim Berners-Lee (before XML was invented)
as a way to describe web pages. It worked great. It was easy to read, easy to
write, and computers could understand it, too.

(Ok, I’m totally gonna botch this next part.) People thought, hey, if HTML works
great for describing web pages, lets “formalize” the language so that we can use
that style to store any type of data we want. And so, XML was born. Basically,
they took the look of HTML and added a few rules about how you were supposed to
write it. That’s why HTML looks like XML and vice versa.

Ok, now for XHTML. A few years after XML caught on (at this time people were
still building websites with HTML), the W3C (the governing body of HTML) decided
they should make a “new” version of HTML that was “proper” XML. In other words,
even though HTML looked like XML, there were still places in HTML were it didn’t
follow the newly defined XML specification. (And that makes sense since HTML
came before XML.)

So, XHTML is a new version of HTML that is valid XML code. There’s not a huge
difference between XHTML and HTML. The biggest thing is that all your tags must
have a matching closing tag. For example, you know that when you create a link
the opening <a> has a matching </a>. But what about the <br> tag? You don’t
write <br>some text</br> do you? But according to the rules of XML, each tag has
to have a matching closing tag. So, in XHTML you now have to write your <br>
tags like <br/>. That’s called a “self-closing” tag.

In addition to requiring every tag to have a closing tag (or be self-closing),
they also got rid of some old tags and attributes that had been replaced by CSS.
For example, the <font> tag is no longer allowed. And the width attributes in
tags, like <table width=”50%”>, is gone, too. There are too many little changes
like this to name here, but I think that should give you an idea.

So, which one do you use? HTML or XHTML? It doesn’t really matter since there’s
a competing HTML5 standard on it’s way that will compete with XHTML (yes, our
lives just got more complicated). Here’s what I do . . .

Declare your pages to be XHTML1. That way, the browser knows how to read them.
Then, run your pages through the W3C XHTML Validator2 to make sure you code is
correct. Also, make sure you adhere to proper “modern” web design principles
like you’re doing already:

  • Use CSS for formatting and layout
  • Don’t use tables for layout
  • No old/deprecated tags like <font>, etc.


Do all that and you’ll be fine. As for the upcoming HTML5, well, that’s
something all web designers will have to worry about together when it happens.

HTML

What is HTML?


HTML is a computer language devised to allow website creation. These websites can then be viewed by anyone else connected to the Internet. It is relatively easy to learn, with the basics being accessible to most people in one sitting; and quite powerful in what it allows you to create. It is constantly undergoing revision and evolution to meet the demands and requirements of the growing Internet audience under the direction of the » W3C, the organisation charged with designing and maintaining the language.

The definition of HTML is HyperText Markup Language.

  • HyperText is the method by which you move around on the web — by clicking on special text called hyperlinks which bring you to the next page. The fact that it is hyper just means it is not linear — i.e. you can go to any place on the Internet whenever you want by clicking on links — there is no set order to do things in.

  • Markup is what HTML tags do to the text inside them. They mark it as a certain type of text (italicised text, for example).

  • HTML is a Language, as it has code-words and syntax like any other language.




How does it work?



HTML consists of a series of short codes typed into a text-file by the site author — these are the tags. The text is then saved as a html file, and viewed through a browser, like Internet Explorer or Netscape Navigator. This browser reads the file and translates the text into a visible form, hopefully rendering the page as the author had intended. Writing your own HTML entails using tags correctly to create your vision. You can use anything from a rudimentary text-editor to a powerful graphical editor to create HTML pages.


What are the tags up to?


The tags are what separate normal text from HTML code. You might know them as the words between the . They allow all the cool stuff like images and tables and stuff, just by telling your browser what to render on the page. Different tags will perform different functions. The tags themselves don’t appear when you view your page through a browser, but their effects do. The simplest tags do nothing more than apply formatting to some text, like this:

These words will be bold, and these will not.

In the example above, the tags were wrapped around some text, and their effect will be that the contained text will be bolded when viewed through an ordinary web browser.

If you want to see a list of a load of tags to see what’s ahead of you, look at this tag reference. Learning the tags themselves is dealt with in the next section of this website, My First Site.


Is this going to take long?


Well, it depends on what you want from it. Knowing HTML will take only a few days of reading and learning the codes for what you want. You can have the basics down in an hour. Once you know the tags you can create HTML pages.

However, using HTML and designing good websites is a different story, which is why I try to do more than just teach you code here at HTMLSource — I like to add in as much advice as possible too. Good website design is half skill and half talent, I reckon. Learning techniques and correct use of your tag knowledge will improve your work immensely, and a good understanding of general design and the audience you’re trying to reach will improve your website’s chances of success. Luckily, these things can be researched and understood, as long as you’re willing to work at it so you can output better websites.

The range of skills you will learn as a result of running your own website is impressive. You’ll learn about aspects of graphic design, typography and computer programming. Your efficiency with computers in general increases.You’ll also learn about promotion and your writing will probably improve too, as you adapt to write for certain audiences.


Do I have to be online all the time?


Not at all. You can code your entire website offline, storing it all on your own computer, and then just transfer all the files onto the web. Then whenever you have new content, you just add that to the existing online version of your site. It’s really quite simple.


Is there anything HTML can’t do?


Of course, but since making websites became more popular and needs increased many other supporting languages have been created to allow new stuff to happen, plus HTML is modified every few years to make way for improvements.

Cascading Stylesheets are used to control how your pages are presented, and make pages more accessible. Basic special effects and interaction is provided by JavaScript, which adds a lot of power to basic HTML. Most of this advanced stuff is for later down the road, but when using all of these technologies together, you have a lot of power at your disposal.