Applying style to pages Contents
A D V E R T I S E M E N T
Declarations
Declarations are used to apply style to elements, for example, to set an
element to have a border.
The basic syntax of a declaration is a property (such as color)
followed by a colon (:) and a value. Thus color: red is a
declaration. Declarations are directly used only in inline style.
Declaration groupingIn order to specify several styles for an element, declarations can be
separated by semi-colons. For example, <P style="color: red; font-size: 16px;">.
It is optional to have a semi-colon after the final declaration - <P
style="color: red; font-size: 16px"> is also valid.
Selectors
Selectors are used to associate style declarations with an element or
elements.
This is done by placing the declarations within a block (enclosed in {}) and
preceding it with a selector. For example, to associate the color: red
declaration with all P elements, you would place that declaration in a block and
add a selector that matches P:
P {color: red}
The example below would make all 'DIV' elements red and 16 pixels high:
DIV {color: red;
font-size: 16px}
At its most basic, a selector consists of an element name (for example, <P>)
(without the < and >). Selectors such as P or A:link are called simple.
SelectorsThe type selector
BODY
The CSS selector involves taking the HTML element, removing the < and >. It
is used, as previously explained, in an example such as BODY {color: red}.
Class selectors
These allow you to group elements in a class. For example, (if you've got CSS
in your browser) you will see all the code exerpts have a gray background with
black border. This is due to a class.
The HTML code for a class looks like this <TAG class="classname">. For
example:
<P class="introductoryparagraph"> .... </P>
These are distinguished in the style sheet by the use of a period followed by
the class name:
P.introductoryparagraph {color: blue}
This applies to P elements with a class of introductorypargraph.
.introductoryparagraph {color: blue}
This would apply to any element with a class of introductoryparagraph
(the other one would only apply to P elements).
Class names should describe the content of the element - instead of
class="italic" you should say why you want it to be italic, e.g.
class="copyrightnotice". That way your style sheet will be self-documenting, and
you can easily change appearance according to element content.
Classes may not start with a number or hyphen, and should be in lowercase
(because although all browsers should distinguish between lower and upper case,
most do not). They may not contain spaces in their names. They may contain any
letter of the alphabet as well as numbers and hyphens (but not at the start of
the class).
An element may have more than one class, e.g., <P class="green quote
new"> matches, e.g., P.quote.green, P.new or P.quote.new.green, but not
P.new.old. However, few browsers support this use.
Pseudo-class selectors
These are so called because the behavior is as if the element was specially
marked as that class.
Note that pseudo-class selectors that cause the document to reflow may be
ignored by web browsers. An example of this would be a different font size on a
visited link to an unvisited one.
:link, :visited
These apply to unvisited, and visited links respectively. A link cannot be
both visited and unvisited. E.g.:
A:link {color: red} /* :link would be the same as A:link,
because there aren't any other elements to which :link applies */
A:visited {color: purple}
:hover and :focus selectors
Hover applies when a mouse is over an element, and focus when the element has
the focus; e.g., a value being typed in a form field. For example, P:hover
{text-decoration: underline}, or A:focus. Hover and focus are not mutually
exclusive with each other or with :link and :visited.
:active selectors
This applies to an element being activated. For example, INPUT:active {color:
red} would make INPUT elements in the process of being activated red.
Links can be active as well as linked or visited. In
addition, :active applies to all elements - not just links.
Pseudo-classes on links
Consider:
A:active {background-color: red}
A:hover {background-color: yellow}
A:link {background-color: green}
Presumably the intention would be to have unvisited links green, except when
a mouse is hovering over it, when the desired background color is yellow, and
when it is being activated, when the background color should be red.
However, this would not happen, because the states are not mutually
exclusive, and since A:link comes last, it overrides the previous ones.
In order to specify more about an element, you can use multiple
pseudo-classes; for example, A:link:hover. Thus to specify that you want hovered
over unvisited links with the focus to be red, you would say A:link:hover:focus
{color: red}. Note that only Netscape 6, and not Windows Internet Explorer,
supports this usage.
Contextual selectors
These provide that if ELEMENT2 is a descendant of ELEMENT1, then the given
properties apply to those ELEMENT2s. E.g.:
STRONG EM {text-transform: uppercase} /* EM inside STRONG
will be uppercase */
This is a very important concept indeed. To take an example, the links in the
contents tables at the top of this page are enclosed in <DIV
class="contents"></DIV>. That means that to make the links a different color you
can just type DIV.contents A {color: blue}, which is equivalent to specifying
that you want all links (all A elements) inside the DIV to be blue.
Selector groupingIn order to associate a block with several different selectors, and therefore
save typing and download time, one can separate the selectors by commas.
BODY P, H1 {color: red}
is the same as:
BODY P {color: red}
H1 {color: red}
|