This is a W3C Working Draft for review by W3C members and other interested parties. It is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to use W3C Working Drafts as reference material or to cite them as other than "work in progress". A list of current W3C technical reports can be found at http://www.w3.org/pub/WWW/TR. Since working drafts are subject to frequent change, you are advised to reference the latter URL, rather than the URL for this working draft.
This document specifies level 1 of the Cascading Style Sheet mechanism (CSS1). CSS1 is a simple style sheet mechanism that allows authors and readers to attach style (e.g. fonts, colors and spacing) to HTML documents. The CSS1 language is human readable and writable, and expresses style in common desktop publishing terminology.
One of the fundamental features of CSS is that style sheets cascade; authors can attach a preferred style sheet, while the reader may have a personal style sheet to adjust for human or technological handicaps. The rules for resolving conflicts between different style sheets are defined in this specification.
Comments to this draft can be sent to the www-style@w3.org mailing list, or directly to the authors. For background material, see the resource page on web style sheets [1]. We encourage browser vendors to start implementing CSS1.
Designing simple style sheets is easy. One only needs to know a little HTML and some basic desktop publishing terminology. E.g., to set the text color of 'H1' elements to blue, one can say:
H1 { color: blue }The example above is a simple CSS rule. A rule consists of two main parts: selector ('H1') and declaration ('color: blue'). The declaration has two parts: property ('color') and value ('blue'). While the example above only tries to influence one of the properties needed for rendering an HTML document, it qualifies as a style sheet on its own. Combined with other style sheets (one fundamental feature of CSS is that style sheets are combined) it will determine the final presentation of the document.
The selector is the link between the HTML document and the style, and all HTML tags are possible selectors. HTML tags are defined in the HTML specification [2], and the CSS1 specification defines a syntax for how to address them.
The 'color' property is one of around 35 properties that determine the presentation of an HTML document. The list of properties and their possible values is defined in this specification.
HTML authors only need to write style sheets if they want to suggest a specific style for their documents. Each User Agent (UA, often a "web browser" or "web client") will have a default style sheet that presents documents in a reasonable - but arguably mundane - manner. Appendix A contains a sample style sheet to present HTML documents as suggested in the HTML 2.0 specification.
In order for the style sheets to influence the presentation, the UA must be aware of their existence. Another W3C working draft, HTML3 and Style Sheets [4], describes how to link HTML with style sheets:
<HTML> <HEAD> <TITLE>title</TITLE> <LINK REL=STYLESHEET TYPE="text/css" HREF="http://style.com/cool" TITLE="Cool"> <STYLE TYPE="text/css"> @import url(http://style.com/basic); H1 { color: blue } </STYLE> </HEAD> <BODY> <H1>Headline is blue</H1> <P STYLE="color: green">While the paragraph is green. </BODY> </HTML>The example shows four ways to combine style and HTML: using the 'LINK' element to link an external style sheet, a 'STYLE' element inside the 'HEAD' element, an imported style sheet using the CSS '@import ...' notation, and a 'STYLE' attribute on an element inside 'BODY'. The latter option mixes style with content and one loses the corresponding advantages of traditional style sheets.
The 'LINK' element references alternative style sheets that the reader can select, while imported style sheets are automatically merged with the rest of the style sheet.
Traditionally, UAs have silently ignored unknown tags. As as result, old UAs will ignore the 'STYLE' element, but its content will be treated as part of the document body, and rendered as such. During a transition phase, 'STYLE' element content may be hidden using SGML comments:
<STYLE> <!-- H1 { color: green } --> </STYLE>Note that conformant SGML parsers normally will remove comments.
H1, H2, H3 { font-family: helvetica }Similarly, declarations can be grouped:
H1 { font-weight: bold; font-size: 12pt; line-height: 14pt; font-family: helvetica; font-style: normal }In addition, some properties have their own grouping syntax:
H1 { font: bold 12pt/14pt helvetica }which is equivalent to the previous example.
<H1>The headline <EM>is</EM> important!</H1>If no color has been assigned to 'EM', the emphasized "is" will inherit the color of the parent element, i.e. it will also appear in blue. Other style properties are likewise inherited, e.g. 'font-family' and 'font-size'.
Inheritance starts at the oldest ancestor, i.e. the top-level element. In HTML, this is the 'HTML' element which is followed by the 'BODY' element. In order to set a "default" style property, one can use 'BODY' as selector:
BODY { color: black; background: url(texture.gif) white; }
This will work even if the author has omitted the 'BODY' tag (which is legal) since the parser will infer the missing tag. The example above sets the text color to be black and the background to be an image. The background will be white if the image is not available. (See the description of the 'background' property for more on this.)
Some style properties are not inherited from the parent element to the child element. Most often it is intuitive why this is not the case. E.g., the 'background' property does not inherit, but the parent element's background will shine through by default.
Often, the value of a property is a percentage that refers to another property:
P { font-size: 10pt } P { line-height: 120% } /* relative to 'font-size', i.e. 12pt */
For each property that allows percentage values, it is defined what property it refers to. Children elements of 'P' will inherit the computed value of 'line-height' (i.e. 12pt), not the percentage.
<HTML> <HEAD> <TITLE>Title</TITLE> <STYLE TYPE="text/css"> H1.punk { color: #00FF00 } </STYLE> </HEAD> <BODY> <H1 CLASS=punk>Way too green</H1> </BODY> </HTML>
The normal inheritance rules apply to classed elements; they inherit values from their ancestors in the document structure.
One can address all elements of the same class by omitting the tag name in the selector:
.punk { color: green } /* all elements with CLASS green */Only one class can be specified per selector. 'P.punk.rap' is therefore an invalid selector in CSS1. (Context-sensitive selectors, described below, can have one class per simple selector)
#z098y { letter-spacing: 0.3em } <P ID=z098y>Wide text</P>
Inheritance saves CSS designers typing. Instead of setting all style properties, one can create defaults and then list the exceptions. To give 'EM' elements within 'H1' a different color, one may specify:
H1 { color: blue } EM { color: red }When this style sheet is in effect, all emphasized sections, within or outside 'H1' will turn red. Probably, one only wanted 'EM' elements within 'H1' to turn red and this can be specified with:
H1 EM { color: red }The selector is now a search pattern on the stack of open elements, and this type of selector is referred to as a "context-sensitive selector". Context-sensitive selectors consist of several simple selectors separated by white space (all selectors described up to now have been simple selectors). Only elements that match the last simple selector (in this case the 'EM' element) are addressed, and only so if the search pattern matches. Context-sensitive selectors in CSS1 look for ancestor relationships, but other relationships (e.g. parent-child) may be introduced in later revisions. In the example above, the search pattern matches if 'EM' is a descendant of 'H1', i.e. if 'EM' is inside an 'H1' element.
UL LI { font-size: small } UL UL LI { font-size: x-small }Here, the first selector matches 'LI' elements with at least one 'UL' ancestor. The second selector matches a subset of the first, i.e. 'LI' elements with at least two 'UL' ancestors. The conflict is resolved by the second selector being more specific due to the longer search pattern. See the cascading order (section 3.2) for more on this.
Context-sensitive selectors can look for tags, classes or ids:
P.reddish .punk { color: red } #x78y CODE { background: blue }
This first selector matches all elements with class 'punk' that have an ancestor of element 'P' with class 'reddish'. The second selector matches all 'CODE' elements that are descendants of the element with 'ID=x78y'. Several context-sensitive selectors can be grouped together:
H1 B, H2 B, H1 EM, H2 EM { color: red }Which is equivalent to:
H1 B { color: red } H2 B { color: red } H1 EM { color: red } H2 EM { color: red }
Textual comments in CSS style sheets are similar to those in the C programming language:
EM { color: red } /* red, really red!! */Comments cannot be nested.
Pseudo-classes and pseudo-elements do not exist in the HTML source. Rather, they are inserted by the UA under certain conditions to be used for addressing in style sheets. They are referred to as "classes" and "elements" since this is a convenient way of describing their behavior, but UAs may choose to implement them differently.
Pseudo-elements are used to address sub-parts of elements, while pseudo-classes allow style sheets to differentiate between different element types.
User agents commonly display newly visited anchors differently from older ones. In CSS1, this is handled through pseudo-classes on the 'A' element:
A:link { color: red } /* unvisited link */ A:visited { color: blue } /* visited links */ A:active { color: orange } /* active links */All 'A' elements with an 'HREF' attribute will be put into one of these groups (i.e. target anchors are not affected). UAs may choose to move an element from 'visited' to 'link' after a certain time.
The formatting of an anchor pseudo-class is exactly as if the class had been inserted manually.
Pseudo-classes can be used in context-sensitive selectors:
A:link IMG { border: solid blue }Also, pseudo-classes can be combined with normal classes:
A.external:visited { color: blue } <A CLASS=external HREF="http://out.side/">external link</A>
If the link in the above example has been visited, it will be rendered in blue. Note that normal class names precede pseudo-classes in the selector.
The 'first-line' pseudo-element is used to apply special styles to the first line as formatted on the canvas:
<STYLE TYPE="text/css"> P:first-line { font-style: small-caps } </STYLE> <P>The first line of an article in Newsweek.On an text-based UA, this could be formatted as:
THE FIRST LINE OF AN article in Newsweek.
(In the above example, the UA chose to replace small-caps text with capital letters since small-caps fonts were not available. Note that this specification does not describe how UAs should render documents when the necessary resources, e.g. colors and fonts, are not available.)
The fictional tag sequence in the above example is: "<P> <P:first-line> The first line of an </P:first-line> article in Newsweek.</P>". The 'first-line' end tag is inserted at the end of the first line as formatted on the canvas.
The 'first-line' pseudo-element can only be attached to a block-level element.
The 'first-letter' pseudo-element is used for "initial caps" and "drop caps" which are common typographic effects. All normal properties 'first-letter' elements and the formatting should be accordingly. This is how you could make a dropcap initial letter span two lines:
<HTML> <HEAD> <TITLE>Title</TITLE> <STYLE TYPE="text/css"> P { font-size: 12pt } P:first-letter { font-size: 200%; vertical-align: -100%; float: left } SPAN { text-transform: uppercase } </STYLE </HEAD> <BODY> <P><SPAN>The first</SPAN> few words of an article in The Economist.</P> </BODY> </HTML>
(The 'SPAN' element is being proposed as a new character-level element for HTML3)
If an text-based UA supports the 'first-letter' pseudo-element (we do not expect them to), the above could be formatted as:
___ | HE FIRST few words |of an article in the Economist..
The fictional tag sequence is: "<P> <SPAN> <P:first-letter> T </P:first-letter> he first </SPAN> few words of an article in the Economist. </P>". Note that the 'first-letter' pseudo-element tags abut the content (i.e. the initial character), while the 'first-line' pseudo-element start tag is inserted right after the start tag of the element they are attached to.
The UA defines what characters are inside the 'first-letter' element, e.g. if preceding punctuation is to be included, or what characters are to be defined as letters.
The 'first-letter' pseudo-element can only be attached to a block-level element.
In a context-sensitive selector, pseudo-elements are only allowed in the last simple selector:
BODY P:first-letter { color: purple }
Pseudo-elements can be combined with classes in selectors:
P.initial:first-letter { color: red } <P CLASS=initial>First paragraph</A>
The above example would make the first letter of all 'P' elements with 'CLASS=initial' red. When combined with classes or pseudo-classes, pseudo-elements should be specified at the end of the selector. Only one pseudo-element can be specified per selector.
P { color: red; font-size: 12pt } P:first-letter { color: green; font-size: 200% } P:first-line { color: blue }In this example, the first letter of each 'P' element would be green with a font size of 24pt. The rest of the first line (as formatted on the canvas) would be blue while the rest of the paragraph would be red. The fictional tag sequence is: "<P> <P:first-line> <P:first-letter> ... </P:first-letter> ... <P:/first-line> </P>".
Note that the 'first-letter' element is inside the 'first-line' element. Properties set on 'first-line' will be inherited by 'first-element', but are overridden if the same property is set on 'first-letter'.
Possibly, pseudo-elements influencing the same content can be set on different elements:
BODY:first-letter { color: red } P:first-letter { font-size: 200% } <BODY><P>some text ...The fictional tag sequence of the above example is "<BODY> <BODY:first-letter> <P> <P:first-letter> some text ..."
@import url(http://www.style.org/punk); @import url(http://www.style.org/funk); H1 { color: red } /* override imported sheets */
Sometimes conflicts will arise between the style sheets that influence the presentation. Conflict resolution is based on each style rule having a weight. By default, the weights of the reader's rules are less than the weights of rules in the author's documents. I.e., if there are conflicts between the style sheets of an incoming document and the reader's personal sheets, the author's rules will be used. Both reader and author rules override UA's default values.
Style sheet designers can increase the weights of their rules:
H1 { color: red ! important } P { font-size: 12pt ! important }
An important reader rule will override an author rule with normal weight. An important author rule will override an important reader rule.
Conflicting rules are intrinsic to the CSS mechanism. To find the value for an element/property combination, the following algorithm should be followed:
LI {...} /* a=0 b=0 c=1 -> specificity = 1 */ UL LI {...} /* a=0 b=0 c=2 -> specificity = 2 */ UL OL LI {...} /* a=0 b=0 c=3 -> specificity = 3 */ LI.red {...} /* a=0 b=1 c=1 -> specificity = 11 */ UL OL LI.red {...} /* a=0 b=1 c=3 -> specificity = 13 */ #x34y {...} /* a=1 b=0 c=0 -> specificity = 100 */
The search for the property value can be terminated whenever one rule has a higher weight than the other rules that apply to the same element/property combination.
This strategy gives author's style sheets considerably higher weight than those of the reader. It is therefore important that the reader has the ability to turn off the influence of a certain style sheet, e.g. through a pull-down menu.
A 'STYLE' attribute on an element (see section 1.1 for an example) should be considered as if an ID attribute had been specified at the end of the style sheet.
The UA may choose to honor other stylistic attributes (e.g. 'ALIGN') as if a 'STYLE' attribute had been used. When in conflict with other stylistic attributes, the 'STYLE' attribute should win.
_______________________________________ | | | margin (transparent) | | _________________________________ | | | | | | | border | | | | ___________________________ | | | | | | | | | | | padding | | | | | | _____________________ | | | | | | | | | | | | | | | content | | | | | | | |_____________________| | | | | | |___________________________| | | | |_________________________________| | |_______________________________________| | element width | | box width |The size of the margin, border and padding are set with the 'margin', 'border' and 'padding' properties respectively. The padding area uses the same background as the element itself (set with the 'background' property). The color and style for the border is set with the 'border' property. The margins are always transparent, so the parent element will shine through.
The following example shows how margins and padding format a 'UL' element with two children. To simplify the diagram there are no borders.
<STYLE TYPE="text/css"> UL { background: red; margin: A B C D; /* let's pretend we have constants in CSS1 */ padding: E F G H; /* " */ } LI { color: white; background: blue; /* so text is white on blue */ margin: a b c d; /* let's pretend we have constants in CSS1 */ padding: e f g h; /* " */ } </STYLE> .. <UL> <LI>1st element of list <LI>2nd element of list </UL>
_______________________________________________________ | | | A UL margin (transparent) | | _______________________________________________ | | D | | B | | | E UL padding (red) | | | | _______________________________________ | | | | H | | F | | | | | a LI margin (transparent, | | | | | | so red shines through) | | | | | | _______________________________ | | | | | | d | | b | | | | | | | e LI padding (blue) | | | | | | | | | | | | | | | | h 1st element of list f | | | | | | | | | | | | | | | | g | | | | | | | |_______________________________| | | | | | | | | | | | | max(a, c) | | | <- note the max | | | | | | | | | _______________________________ | | | | | | | | | | | | | | d | e LI padding (blue) | | | | | | | | | | | | | | | | h 2nd element of list f | | | | | | | | | | | | | | | | g | | | | | | | |_______________________________| | | | | | | | | | | | | c LI margin (transparent, | | | | | | so red shines through) | | | | | |_______________________________________| | | | | | | | | G | | | |_______________________________________________| | | | | C | |_______________________________________________________|
Technically, padding and margin properties are not inherited. But, as the example shows, the placement of an element is relative to ancestors and siblings.
If the border width had been set (the default value is '0'), the border would have appeared between the padding and the margins.
The width of the margins specify the minimum distance to the edges of surrounding boxes. In the example above, the margins between the two 'LI' elements are collapsed by taking the maximum of the two. Similarly, if the padding between the 'UL' and the first 'LI' element is zero (the "E" constant), the top elements' top margins should be collapsed.
Elements with a 'display' property value of 'list-item' are preceded by a label. The type of label is determined by the 'list-style' property. The label is not considered to be a part of the content, and will be placed outside the content. The rendering of the label should be based on the font and color properties of the element it belongs to.
The first issue is harder to resolve and has caused the authors of this specification much pain. There are two ideas: either the unfilled area is filled with the default background resource of the window system, or the background is "stolen" from a structural element. HTML extensions have taken the latter path: attributes on the 'BODY' element set the background of the whole canvas. To support designers' expectations, CSS1 introduces a special rule to find the canvas background:
If the 'background' value of the 'HTML' element is different from 'transparent' then use it, else use the 'background' value of the 'BODY' element. If the resulting value is 'transparent', the rendering is undefined.
This rule allows:
<HTML STYLE="background: url(http://style.com/marble.png)"> <BODY STYLE="background: red">In the example above, the canvas will be covered with "marble". The background of the 'BODY' element (which may or may not fully cover the canvas will be red.
Until other means of addressing the canvas become available, we recommend setting canvas properties on the 'BODY' element.
Style sheets influence the presentation of documents by assigning values to style properties. This section lists the defined style properties, and their corresponding list of possible values, of CSS1.
The list of CSS1 properties has been kept to a minimum, while making sure commonly used styles can be expressed. Depending on the formatting model and the presentation medium, some properties can prove hard to incorporate into existing UA implementations. According to the conformance rules, UAs should make efforts to format documents according to the style sheets, but full support for all properties cannot be expected. E.g., a text-based browser is not able to honor margins accurately, but should approximate.
In the text below, the allowed values for each property are listed with a syntax like the following:
Value: N | NW | NE
Value: [ <length> | thick | thin ]{1,4}
Value: <url>? <color> [ / <color> ]?
Value: <url> || <color>
The words between "<" and ">" give a type of value. The most common types are <length>, <percentage>, <url>, <number> and <color>; these are described in the section on units. The more specialized types (e.g. <font-family> and <border-style>) are described under the property where they appear.
Other words are keywords that must appear literally. The slash (/) is also considered a keyword.
Several things juxtaposed mean that all of them must occur, in the given order. A bar (|) separates alternatives: one of them must occur. A double bar (A || B) means that either A or B or both must occur, in any order. Brackets ([]) are for grouping. Juxtaposition is stronger than the double bar, and the double bar is stronger than the bar. Thus "a b | c || d e" is equivalent to "[ a b ] | [ c || [ d e ]]".
Every type, keyword, or bracketed group may be followed by one of the following modifiers:
A font is assumed to come with a certain encoding, i.e., a table that maps characters to glyphs. CSS1 assumes that a suitable table exists; it has no provisions for changing that table. A "character" in this respect is an abstract notion, it is not the number or bit-pattern that is found in the document, nor is it the glyph that appears on the screen. It is something in between. In many documents the byte with number 65 will represent the character "A", which will then, subject to the character-to-glyph mapping, be represented as some "A"-like shape. In non-western languages the relation between bytes and the characters they represent is much more complex. So, CSS deals with characters, and whether a particular implementation represents an "A" internally by the number 65 or something completely different is of no concern here. The character-to-glyph table for each font has one entry for each possible character in an HTML document. Each entry is either empty, meaning that the font doesn't have a glyph for that character, or it points to a glyph.
Many fonts will only have glyphs for a few hundred characters. For example, fonts for western languages usually have some 200 glyphs. The fact that most encoding tables will be quite sparse has important consequences:
<EM STYLE="font-family: Symbol">abcd</EM>
The example above has no effect whatsoever, since the four characters 'abcd' have no glyphs in the 'Symbol' font. Older browsers that work with 8-bit characters internally often fail to check the font encoding. Typically, the above fragment will show four Greek letters. Authors should not rely on this behavior.
A keyword value is an index to a table of font sizes kept by the UA. On a computer screen a scaling factor of 1.5 is suggested between adjacent indexes; if the 'medium' font is 10pt, the 'large' font could be 15pt. Different media may need different scaling factors. Also, the UA should take the quality and availability of fonts into account when computing the table. The table may be different from one font family to another.
If the value is a number, it is interpreted relative to the table of font sizes and the font size of the parent element. For example, if the parent element has a font size of 'medium', a value of '-2' will make the font size of the current element be 'x-small'. If the parent element's size is not close to a table entry, the UA is free to interpolate between table entries or round off to the closest one. The UA may have to extrapolate table values if the numerical value goes beyond the keywords.
Length and percentage values should not take the font size table into account when calculating the font size of the element.
For most properties, length values refer to the font size of the current element. On the 'font-size' property' length units (e.g. 'em' and 'ex'), refer to the font size of the parent element.
Note that an application may reinterpret an explicit size, depending on the context. E.g., inside a VR scene a font may get a different size because of perspective distortion.
Examples:
P { font-size: 12pt; } /* absolute size: be careful! */ BLOCKQUOTE { font-size: -1 } EM { font-size: +1 } /* the '+' is optional */ EM { font-size: 150% } EM { font-size: 1.5em }If the suggested scaling factor of 1.5 is used, the latter three rules are identical.
The value is a prioritized list of font family names and/or generic family names. Unlike most other CSS1 properties, values are separated by a comma to indicate that they are alternatives:
BODY { font-family: gill, helvetica, sans-serif }There are two types of list values:
BODY { font-family: "new century schoolbook", serif } <BODY STYLE="font-family: 'My own font', fantasy">
If quoting is omitted, any white space characters before and after the font name is ignored and any sequence of white space characters inside the font name is converted to a single space.
Ideally, the style sheet designer should specify only one font, and the font manager should return the best alternative (perhaps by taking visual similarity, visual quality and performance into account). Unfortunately, current rendering environments do not offer this level of service, and it is beyond the style sheet mechanism to do so. Therefore, a prioritized list of alternative families can be supplied. This practice poses one problem: the UA must be able to determine if a font selection has been successful or not to know how far it should proceed in the list. One example: if the style sheet asks for 'univers' and the window system is smart enough to suggest 'helvetica' (which looks almost identical) as a replacement, is this a success or failure? This specification leaves the answer undefined for now.
The keywords can either be absolute or relative:
P { font-weight: medium } STRONG { font-weight: bolder }In the example above, 'STRONG' elements should be 'bolder' than their parent. If the parent is 'medium', a 'STRONG' element will have the value of 'bold'. I.e., a relative value indicates a change of two positions relative to the list of absolute values. Child elements inherit the resultant weight, not the keyword value.
If the desired font weight is not available, the UA selects an approximation strategy.
Legal combinations of the values are:
If the preferred font style cannot be accomplished, the UA should make best efforts to find acceptable substitutions. Often, an 'oblique' font can be substituted by an 'italic' font. If 'small-caps' are not available, capital letters of a smaller font size can be used to render small characters if the resolution of the output medium is appropriate for this.
H1, H2, H3 { font-style: small-caps italic } H1 EM { font-style: italic }In the example above, emphasized text within 'H1' will appear in normal lower-case italic.
The property sets the the distance between two adjacent lines' baselines. It only applies to block-level elements.
When a numerical value is specified, the line height is given by the font size of the current element multiplied with the numerical value. This differs from the precentage unit in the way it inherits: when a numerical value is specified, child elements will inherit the factor itself, not the resultant value (as is the case with percentage and other units).
Negative values are not allowed.
The three rules in the example below have the same resultant line height:
DIV { line-height: 1.2; font-size: 10pt } /* number */ DIV { line-height: 1.2em; font-size: 10pt } /* length */ DIV { line-height: 120%; font-size: 10pt } /* percentage */
It is suggested that UAs set the initial value to be a number in the range of 1.0 to 1.2.
This syntax of this property is based on a traditional typographic shorthand notation to set multiple properties related to fonts: 'font-style', 'font-weight', 'font-size', 'line-height' and 'font-family'. For a definition of allowed and initial values, see the previously defined properties. Setting the 'font' property is equivalent to including separate declarations at the same point in the style sheet. Properties left out of a 'font' specification are set to their initial value.
P { font: bold 12pt/14pt sans-serif } P { font: 80% sans-serif } P { font: bold italic x-large/110% "new century schoolbook", serif }In the second rule, the font size percentage value ('80%') refers to the font size of the parent element. In the last rule, the line height percentage refers to the font size of the element itself.
This property describes the text color of an element, i.e. the "foreground" color. There are different ways to specify red:
EM { color: red } /* natural language */ EM { color: rgb(255,0,0) } /* RGB rage 0-255 */
See section 6.3 for a complete description of possible color values.
This property describes the background of an element, i.e. the surface onto which the content (e.g. text) is rendered. The background can be one of:
P { background: transparent } /* transparent */ BODY { background: red } /* one color */ H1 { background: blue / red } /* blend two colors */ BODY { background: url(chess.png) 50% repeat fixed } /* img + controls */
This property does not inherit, but the parent element's background will shine through by default due to the initial transparent value. If neither an image or a color is found, a value of 'transparent' is assumed.
If an image is found through the URL, it will be overlaid on top of any color specified. The color (or color combination) is used:
TABLE { background: blue/green NW }
In the example above, the direction is set to 'NW' which gives a table that is blue in the bottom right corner and blending smoothly into green in the top left corner.
If <scroll> (described below) has a value of 'fixed', the colors will be blended over the canvas, not the element:
BODY { background: blue/green fixed }Here, the canvas would be blue at the top and green at the bottom.
If only one background color is specified, <blend-direction> is ignored.
BODY { background: url(marble.png) repeat-x }In the example above, the image will only be repeated horizontally. Similarly, a value of 'repeat-y' will repeat the image vertically.
If a background image has been specified, the value of <position> specifies its initial position. Legal values are [<percentage> | left | center | right [ <percentage> | top | middle | bottom]?]. If no value is specified, '0% 0%' is assumed.
With a value pair of '0% 0%', the upper left corner of the image is placed in the upper left corner of the element. A value pair of '100% 100%' places the lower right corner of the image in the lower right corner of the element. With a value pair of '14% 84%', the point 14% across and 84% down the image is to be placed at the point 14% across and 84% down the element.
If only one value is given, it sets both the horizontal and vertical offset of the background image. If two values are given, the horizontal position comes first.
If the background image is fixed with regard to the canvas (see the <scroll> value above), the image is placed relative to the canvas instead of the element. E.g.:
BODY { background: url(logo.png) fixed 100% 100%; }
In the example above, the image is placed in the lower right corner of the canvas.
The keyword values are defined as follows:
left = 0% center = 50% right = 100% top = 0% middle = 50% bottom = 100%
The length unit indicates an addition to the default space between words. Values can be negative, but there may be implementation-specific limits. The UA is free to select the exact spacing algorithm. The word spacing may also be influenced by justification (which is a value of the 'align' property).
H1 { word-spacing: 1em }Here, the word-spacing between each word in 'H1' elements would be increased with '1em'.
The length unit indicates an addition to the default space between characters. Values can be negative, but there may be implementation-specific limits. The UA is free to select the exact spacing algorithm. The letter spacing may also be influenced by justification (which is a value of the 'align' property).
BLOCKQUOTE { letter-spacing: 0.1em }Here, the word-spacing between each character in 'BLOCKQUOTE' elements would be increased with '0.1em'.
This property describes decorations that are added to the text of an element. If the element has no text (e.g. the IMG element in HTML) or is an empty element (e.g. "<EM></EM>"), this property has no effect.
The color(s) required for the text decoration should be derived from the 'color' property value.
This property is not inherited, but children elements should match their ancestor. E.g., if an element is underlined, the line should span the child elements. The color of the underlining will remain the same even if descendant elements have different 'color' values.
A:link, A:visited, A:active { text-decoration: underline }
The example above would underline the text of all active links.
We expect UA vendors to propose several new values on this property. Formatters should treat unknown values as 'underline'.
The property affects the vertical positioning of the element. One set of keywords is relative to the parent element:
Using the 'top' and 'bottom' alignment, unsolvable situations can occur where element dependencies form a loop.
Percentage values refer to the 'line-height' of the element itself. E.g., a value of '-100%' will lower the element to where the baseline of the next line should have been.
H1 { text-transform: uppercase }
The example above would put 'H1' elements in uppercase text.
This property describes how text is aligned within the element. The actual justification algorithm used is UA and human language dependent.
Example:
DIV.center { text-align: center }Since this property inherit, all block-level elements inside the 'DIV' element with 'CLASS=center' will be centered. Note that alignments are relative to the width of the element, not the canvas. If 'justify' is not supported, the UA will supply a replacement. Typically, this will be 'left' for western languages.
The property specifies indent that appears before the first formatted line. 'text-indent' may be negative. An indent is not inserted in the middle of an element that was broken by another (such as 'BR' in HTML).
Example:
P { text-indent: 3em }
These properties set the margin of an element: the 'margin' property sets the border for all four sides while the other properties only set their respective side.
For the 'margin' property, the four lengths apply to top, right, bottom and left respectively. If there is only one value, it applies to all sides, if there are two or three, the missing values are taken from the opposite side.
BODY { margin: 2em } /* all margins set to 2em */ BODY { margin: 1em 2em } /* top & bottom = 1em, right & left = 2em */ BODY { margin: 1em 2em 3em } /* top=1em, right=2em, bottom=3em, left=2em */
The 'margin' property is shorthand for setting 'margin-top', 'margin-right' 'margin-bottom' and 'margin-left' at the same place in the style sheet. These properties allow only one value. The last rule of the example above is equivalent to the example below:
BODY { margin-top: 1em; margin-right: 2em; margin-bottom: 3em; margin-left: 2em; /* copied from opposite side (right) */ }
The margins express the minimal distance between the borders of two adjacent elements. See the formatting model (section 4) for an example.
When margin properties are applied to replaced elements (e.g. IMG), they express the minimal distance from the replaced element to any of the content of the parent element.
Negative margin values will enable interesting visual effects such as overlapping text. However, they will also complicate the formatting model, and we would like to gain more implementation experience before specifying one way or the other.
The property describes how much space to insert between the border and the content (e.g. text or image). The order is top, right, bottom, left. If there is only one value, it applies to all sides, if there are two or three, the missing values are taken from the opposite side.
The surface of the padding area is set with the 'background' property.
H1 { background: white; padding: 1em }
The example above sets a 1em padding on all sides. The 'em' unit is relative to the element's font.
Padding values cannot be negative. See the formatting model (section 4) for more on these properties.
These properties set the border of an element: the 'border' property sets the border for all four sides while the other properties only set their respective side.
The border is drawn in the image pointed to by the URL. If no URL is specified, or when the image is not available, the color value is used.
If an image is found through the URL, it will be used as a texture (i.e. repeated throughout the border) to draw the border. The <color> value, if specified, is used:
P { color: black; background: white; border: solid; }In the above example, the border will be a solid black line.
The keyword widths are constant throughout a document:
H1 { border: solid thick red } P { border: solid thick blue }In the example above, 'H1' and 'P' elements will have the same border width regardless of font size. To do relative width, the 'em' unit can be used:
H1 { border: solid 0.5em }
The border styles mean:
This property can be applied to text elements, but it is most useful with inline images and similar insertions. The width is to be enforced by scaling the image if necessary. When scaling, the aspect ratio of the image should be preserved if the 'height' property is 'auto'.
Example:
IMG.icon { width: 100px }
See the formatting model (section 4) for a description of the relationship between this property and the margin and padding.
This property can be applied to text, but it is most useful with inline images and similar insertions. The height is to be enforced by scaling the image if necessary. When scaling, the aspect ratio of the image should be preserved if the 'width' property is 'auto'.
Example:
IMG.icon { height: 100px }If applied to a textual element, the height can be enforced with e.g. a scrollbar.
This property is most often used with inline images.
With the value 'none', the element will be displayed where it appears in the text. With a value of 'left' ('right') the margin properties will decide the horizontal positioning of the image and the text will float on the right (left) side of the image. With a a value of 'left' or 'right', the element is treated as block-level (i.e. the 'display' property is ignored).
IMG.icon { float: left; margin-left: 0; }The above example will place all IMG elements with 'CLASS=icon' along the left side of the parent element.
This property specifies if elements allow floating elements (normally images) to the left or right. With 'clear' set to 'left', an element will be moved below any floating element on the left side. Example:
H1 { clear: left }
This property indicates if an element is inline (e.g. 'EM' in HTML), block-level (e.g. 'H1' in HTML), or a block-level list item (e.g. 'LI' in HTML). For HTML documents, the initial value will be taken from the HTML specification.
A value of 'none' turns the display of the element, including children elements and the surrounding box, off.
P { display: block } EM { display: inline } LI { display: list-item } IMG { display: none }
The last rule turns the display of images off.
Note that HTML defines what elements are block-level (called "Block Structuring Elements" in [2]) and inline (called "Phrase Markup" in [2]), and this may be hardcoded into some UA implementations.
This property can be set on any element, and it will inherit normally down the tree. However, the 'list-style' will only be displayed on elements with a 'display' value of 'list-item'. In HTML this is typically the case for the 'LI' element.
UL { list-style: disc } UL UL { list-style: circle } LI.square { list-style: square } OL { list-style: decimal } /* 1 2 3 4 5 etc. */ OL { list-style: lower-roman } /* a b c d e etc. */A URL value can be combined with any other value:
UL { list-style: url(http://png.com/ellipse.png) disc }In the example above, the 'disc' will be used when the image is unavailable.
Declares how white space inside the element should be handled: the 'normal' way (where white space is collapsed) or as the 'PRE' element in HTML. This property may be hardcoded into UA implementations, and style sheet designers should be careful changing the initial values:
PRE { white-space: pre } /* initial value */
There are three types of length units: relative, pixel and absolute. Relative units specify a length relative to another length property. Style sheets that use relative units will more easily scale from one medium to another (e.g. from a computer display to a laser printer). Percentage units (described below) and keyword values (e.g. 'x-large') offer similar advantages.
Child elements inherit the computed value, not the relative value:
BODY { font-size: 12pt; text-indent: 3em; /* i.e. 36pt */ } H1 { font-size: 15pt }
In the example above, the 'text-indent' value of 'H1' elements will be 36pt, not 45pt.
These relative units are supported:
H1 { margin: 0.5em } /* ems, the height of the element's font */ H1 { margin: 1ex } /* x-height, ~ the height of the letter 'x' */ P { font-size: 12px } /* pixels, relative to canvas */
Pixel units, as used in the last rule, are relative to the resolution of the canvas, i.e. most often a computer display. If the pixel density of the output device is very different from that of a typical computer display, the UA should rescale pixel values. The suggested "reference pixel" is the visual angle of one pixel on a device with a pixel density of 90dpi and a distance from the reader of an arm's length.
Absolute length units are only useful when the physical properties of the output medium is known. These absolute units are supported:
H1 { margin: 0.5in } /* inches, 1in = 2.54cm */ H2 { line-height: 3cm } /* centimeters */ H3 { word-spacing: 4mm } /* millimeters */ H4 { font-size: 12pt } /* points, 1pt = 1/72 in */ H4 { font-size: 1pc } /* picas, 1pc = 12pt */
The format of a length value is a number (with or without a decimal point) immediately followed by '%'. Percentage values are always relative to a length unit. Each property that allows percentage units also define what length unit they refer to. Most often this is the font size of the element itself:
P { line-height: 120% } /* 120% of the element's 'font-size' */
Child elements inherit the resultant value, not the percentage value.
A color is a either a color name or a numerical RGB specification. For a suggested list of color names, see Appendix B.
The RGB color model is being used in numerical color specifications. There are different ways to specify red:
EM { color: #F00 } /* #RGB */ EM { color: #FF0000 } /* #RRGGBB */ EM { color: rgb(255,0,0) /* integer range 0 - 255 */ EM { color: rgb(255,0,0) /* integer range 0 - 255 */ EM { color: rgb(100%, 0%, 0%) /* float range 0.0% - 100.0% */
Note that three-digit RGB notation (#RGB) is converted into six-digit form (#RRGGBB) by replicating digits, not by adding zeros. For example, #fb0 expands ro #ffbb00.
Values outside the numerical ranges should be rounded off. The three rules below are therefore equivalent:
EM { color: rgb(255,0,0) /* integer range 0 - 255 */ EM { color: rgb(300,0,0) /* rounded off to 255 */ EM { color: rgb(110%, 0%, 0%) /* rounded off to 100% */
To ensure similar colors on different devices, numerical color values are assumed to have been gamma-corrected for a display gamma value of 2.2. This is suitable for most PCs and unix workstations. Systems with different gamma compensation (e.g. Apple MacIntosh and Silicon Graphics) must adjust the RGB values accordingly.
A Uniform Resource Locator (URL) is identified with a functional notation:
BODY { background: url(http://www.bg.com/pinkish.gif) }Partial URLs are interpreted relative to the source of the style sheet, not relative to the document:
BODY { background: url(yellow) }In absolute URLs the functional notation is optional:
TABLE { background: url(http://www.bg.com/funkish) }
Note that this section is still under discussion and the model suggested is only one of several proposed.
It is possible to consider the rules as rules on the edge of something (cell, column, table, etc.), as rules between things, or as borders around things. Sometimes one model seems easier than another, but mixing models may not be the best solution.
In the HTML table model, there are 6 types of objects: table (table), row group (thead, tbody, tfoot), row (tr), column group (colgroup), column (col), cell (td, th). Only 3 of them correspond to well defined areas of the table (i.e., areas that you can draw a border around): table, row group, cell.
The others may or may not correspond to a visible area. The column and the row may contain cells that span other columns and rows. The same is true of column groups.
The three "regular" table elements (table, row group, cell) can have borders. Since these elements also nest inside each other, they could even have each their own border (and a margin/padding in between). However, it is not very intuitive to view tables as a three level nesting. It seems much easier to view the edge of the table as being the same edge as the edge of the cell at that point, so this is the model adopted in CSS1.
The most common case seems to be that the rules on the edges of "larger" groupings are not interrupted. ("Larger" in the sense of being ancestor in the nesting.) In other words: edges of the table or the row groups stretch uninterrupted from one table edge to another. The main exception is the case where the table itself has no visible border: that invisible border can be replaced by a visible border around a cell or a group.
Another way of looking at this is to note that it is usually the "heavier" borders that are uninterrupted. "Heavier" relates to the thickness of the rule or the style or maybe a combination of both: thick single lines seem to override thin single lines, double lines seem to override single lines, etc.
However, it's our impression that the rule about "heavier" edges is more often broken than the one about "larger" objects.
The observation that "larger" objects usually have uninterrupted borders, leads to a basic disambiguating rule that the border of a table is always drawn, the border of a row group only where it doesn't coincide with the table's border, and the cell borders are only drawn when they don't coincide with any row group's. The exception is the 'none' style, which is always overridden by any visible border.
Extending the rule to columns, column groups and rows can be as simple as stating that their borders are only drawn if they fall on edges with a 'none' style.
The observation that heavier rules are usually uninterrupted leads to a precedence based on border width and style.
The two methods can be combined: in case of a conflict between elements of the same type, the widest border wins. If the borders are the same width, "heavier" styles win.
The margin properties work normally for the table element, but they don't apply to row groups, rows, column groups, columns, and cells.
Padding applies normally to cells, but doesn't apply to any other table element.
The difference between styles 'none' and 'blank' is that 'none' can be overridden by "smaller" (child) elements, while 'blank' can't. Otherwise they look the same. They have no width, even if one is specified explicitly.
The <border-style> may be accompanied by the keyword 'override', in which case this style overrides a style set on a "larger" element (unless that also has the keyword 'override').
The number gives the position of the element relative to its parent in the z-direction (out of the screen, towards the viewer). The elevation of an element will only be visual if the element has a 3D border (i.e. 'bevel').
The number has no unit, but the effect is that an elevation of 1 results in a shadow/slope of 1 pixel.
The number is not inherited, but since it is a relative number, child elements will "inherit" the absolute elevation of their parent.
In tables, the interpretation is changed slightly. The elevation of the table elements col and colgroup is ignored. The elevations of the table, thead, tbody, tfoot and tr elements are used to compute the absolute elevation of the cells inside them, but are never visualized on their own. The elevation of the table element is used, however, to specify the elevation of the (3D) borders around cells.
Note: This means that it is not possible to get borders with different elevations in one table.
Note: If the border's width is insufficient for the amount of shadow, the border-width is silently increased.
Note that this grammar is ambiguous. Whether a block
contains declarations or a nested style sheet cannot be determined
lexically. In an actual grammar, there should be different rules for
each AT_KEYWORD
.
stylesheet : [ at-rule | rule ]*; rule : token+ block; at-rule : AT_KEYWORD token* [ ';' | block ]; block : '{' [ declarations | stylesheet ] '}'; declarations : declaration [ ';' declaration ]*; declaration : IDENT ':' token*; token : IDENT | '[' token* ']' | '(' token* ')' | SYMBOL | ':' | STRING | NUMBER;
With the following terminals (using a Lex fragment):
nmstrt [A-Za-z]|\\. nmchar [A-Za-z0-9-]|\\. ident {nmstrt}{nmchar}* d [0-9] dstring \"([^"]|\\.)*\" sstring \'([^']|\\.)*\' w [ \t\n]* %x COMMENT %% "/*" BEGIN(COMMENT); <COMMENT>"*/" BEGIN(INITIAL); <COMMENT>. /* ignore */ {ident} return IDENT; \@{ident} return AT_KEYWORD; {d}+|{d}*\.{d}+ return NUMBER; {dstring}|{sstring} return STRING; \!|\#|\,|\%|\+|\-|\/ | \=|\.|\: return SYMBOL; {w} /* ignore */
Note that the list of SYMBOLs is actually open-ended.
The goal of the work on CSS1 has been to create a simple style sheet mechanism for HTML documents. The current specification is a balance between the simplicity needed to realize style sheets on the web, and pressure from authors for richer visual control. CSS1 offers:
<H1>H<FONT SIZE=-1>EADLINE</FONT></H1>with the style sheet:
H1 { font-style: small-caps } <H1>Headline</H1>
[1] The W3C resource page on web style sheets (http://www.w3.org/pub/WWW/Style)
[2] RFC 1866: Hypertext Markup Language - 2.0, (ftp://ds.internic.net/rfc/rfc1866.txt). The specification is also available in hypertext form (http://www.w3.org/pub/WWW/MarkUp/html-spec/html-spec_toc.html)
[3] ISO 8879. Information Processing - Text and Office Systems - Standard Generalized Markup Language (SGML), 1986.
[4] HTML3 and Style Sheets (http://www.w3.org/pub/WWW/TR/WD-style.html)
[5] HyperText Markup Language Specification Version 3.0 (http://www.w3.org/pub/WWW/MarkUp/html3/CoverPage.html) The draft has now expired
[6] The HTML3 table model (http://www.w3.org/pub/WWW/TR/WD-tables.html)
[7] T Berk, L Brownston, A Kaufman: A New Color-Naming System for Graphics Languages, IEEE CG&A, May 1982
[8] DSSSL (http://occam.sjf.novell.com:8080/dsssl/dsssl96) is a tree transformation and style language from the SGML community. Bert Bos has written some notes on Translating CSS to DSSSL (http://www.w3.org/pub/WWW/Style/css/css2dsssl.html)
[9] Frame-based layout via Style Sheets (http://www.w3.org/pub/WWW/TR/WD-layout.html)
[10] PNG (Portable Network Graphics) Specification (http://www.w3.org/pub/WWW/TR/WD-png.html)
During the short life of HTML, there have been several style sheet proposals to which this proposal is indebted. Especially the proposals from Robert Raisch, Joe English and Pei Wei were influential.
Through "www-style@w3.org" and other electronic media, these people have contributed: Kevin Hughes, William Perry, Benjamin Sittler, Tony Jebson, Paul Prescod, Evan Kirshenbaum, Scott Bigham, Glenn Adams, Scott Preece, Greg Watkins, Jon Smirl, Chris Lilley, Mandira Virmani, Jenny Raggett and Michael Seaton.
Also, thanks to Tim Berners-Lee, Vincent Quint, Cécile Roisin, Irène Vatton, Phill Hallam-Baker, Yves Lafon, Yves Bertot, Colas Nahaboo, Philippe Kaplan, Gilles Kahn, Terry Allen, Daniel Connolly, Roy Fielding, Henrik Frystyk Nielsen, James Clark, David Siegel, Thomas Reardon, Chris Wilson, Lydja Williams, Robert Cailliau and Stephen Zilles for constructive discussions.
Our special thanks goes to Dave Raggett for his encouragement and work on HTML3, including tables. Steven Pemberton has contributed extensively, and was also chair of the W3C Style Sheets workshop where many issues were resolved.
The following style sheet is written according to the suggested rendering in the HTML 2.0 specification. Some styles, e.g. colors, have been added for completeness. It is suggested that a style sheet similar to the one below is used as a UA default.
BODY { margin: 1em; font-family: serif; line-height: 1.1; background: white; color: black; border: black; /* used by the HR element */ } H1, H2, H3, H4, H5, H6, P, UL, OL, DIR, MENU, DIV, DT, DD, ADDRESS, BLOCKQUOTE, PRE, BR, HR { display: block } B, STRONG, I, EM, CITE, VAR, TT, CODE, KBD, SAMP, IMG, SPAN { display: inline } LI { display: list-item } H1, H2, H3, H4 { margin-top: 1em; margin-bottom: 1em } H5, H6 { margin-top: 1em } H1 { text-align: center } H1, H2, H4, H6 { font-weight: bold } H3, H5 { font-style: italic } H1 { font-size: +3 } H2 { font-size: +2 } H3 { font-size: +1 } B, STRONG { font-weight: bolder } /* relative to the parent */ I, CITE, EM, VAR, ADDRESS, BLOCKQUOTE { font-style: italic } PRE, TT, CODE, KBD, SAMP { font-family: monospace } PRE { white-space: pre } ADDRESS { margin-left: 3em } BLOCKQUOTE { margin-left: 3em; margin-right: 3em } UL, DIR { list-style: disc } OL { list-style: decimal } MENU { margin: 0 0 } /* tight formatting */ LI { margin-left: 3em } DT { margin-bottom: 0 } DD { margin-top: 0; margin-left: 5em } HR { border-top: solid } /* 'border-bottom' could also have been used */ A:link { color: blue } /* unvisited link */ A:visited { color: red } /* visited links */ A:active { color: orange } /* active links */ /* setting the anchor border around IMG elements requires context-sensitive selectors */ A:link IMG { border: 2px solid blue } A:visited IMG { border: 2px solid red } A:active IMG { border: 2px solid orange }
This table lists color names supported by several current UAs. To ensure similar colors on different devices, numerical color values are assumed to have been gamma-corrected for a display gamma value of 2.2. This is suitable for most PCs and unix workstations. Systems with different gamma compensation (e.g. Apple MacIntosh and Silicon Graphics) must adjust the RGB values accordingly.
The corresponding RGB values for linear light and for Macs are also given for convenience.
For further technical details see the Gamma Tutorial Appendix in the PNG specification [10].
linear light | PC | Mac | |||||||
---|---|---|---|---|---|---|---|---|---|
R | G | B | R | G | B | R | G | B | |
aliceblue | 223 | 240 | 255 | 240 | 248 | 255 | 233 | 245 | 255 |
antiquewhite | 244 | 213 | 175 | 250 | 235 | 215 | 248 | 226 | 199 |
aqua | 0 | 255 | 255 | 0 | 255 | 255 | 0 | 255 | 255 |
aquamarine | 55 | 255 | 170 | 127 | 255 | 212 | 92 | 255 | 194 |
azure | 223 | 255 | 255 | 240 | 255 | 255 | 233 | 255 | 255 |
beige | 234 | 234 | 184 | 245 | 245 | 220 | 240 | 240 | 205 |
bisque | 255 | 199 | 143 | 255 | 228 | 196 | 255 | 216 | 173 |
black | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
blanchedalmond | 255 | 213 | 158 | 255 | 235 | 205 | 255 | 226 | 185 |
blue | 0 | 0 | 255 | 0 | 0 | 255 | 0 | 0 | 255 |
blueviolet | 66 | 5 | 196 | 138 | 43 | 226 | 104 | 19 | 214 |
brown | 98 | 5 | 5 | 165 | 42 | 42 | 135 | 18 | 18 |
burlywood | 188 | 124 | 63 | 222 | 184 | 135 | 208 | 158 | 100 |
cadetblue | 29 | 89 | 91 | 95 | 158 | 160 | 60 | 126 | 129 |
chartreuse | 55 | 255 | 0 | 127 | 255 | 0 | 92 | 255 | 0 |
chocolate | 166 | 36 | 2 | 210 | 105 | 30 | 192 | 69 | 11 |
coral | 255 | 55 | 20 | 255 | 127 | 80 | 255 | 92 | 47 |
cornflowerblue | 33 | 78 | 217 | 100 | 149 | 237 | 65 | 116 | 229 |
cornsilk | 255 | 240 | 184 | 255 | 248 | 220 | 255 | 245 | 205 |
crimson | 184 | 1 | 11 | 220 | 20 | 60 | 205 | 6 | 31 |
cyan | 0 | 255 | 255 | 0 | 255 | 255 | 0 | 255 | 255 |
darkblue | 0 | 0 | 67 | 0 | 0 | 139 | 0 | 0 | 105 |
darkcyan | 0 | 67 | 67 | 0 | 139 | 139 | 0 | 105 | 105 |
darkgoldenrod | 124 | 62 | 0 | 184 | 134 | 11 | 158 | 99 | 3 |
darkgray | 103 | 103 | 103 | 169 | 169 | 169 | 139 | 139 | 139 |
darkgreen | 0 | 33 | 0 | 0 | 100 | 0 | 0 | 65 | 0 |
darkkhaki | 132 | 123 | 38 | 189 | 183 | 107 | 164 | 157 | 71 |
darkmagenta | 67 | 0 | 67 | 139 | 0 | 139 | 105 | 0 | 105 |
darkolivegreen | 23 | 38 | 6 | 85 | 107 | 47 | 51 | 71 | 21 |
darkorange | 255 | 68 | 0 | 255 | 140 | 0 | 255 | 106 | 0 |
darkorchid | 83 | 7 | 156 | 153 | 50 | 204 | 121 | 23 | 184 |
darkred | 67 | 0 | 0 | 139 | 0 | 0 | 105 | 0 | 0 |
darksalmon | 209 | 79 | 50 | 233 | 150 | 122 | 223 | 117 | 86 |
darkseagreen | 71 | 130 | 71 | 143 | 188 | 143 | 109 | 163 | 109 |
darkslateblue | 16 | 11 | 67 | 72 | 61 | 139 | 40 | 31 | 105 |
darkslategray | 6 | 19 | 19 | 47 | 79 | 79 | 21 | 46 | 46 |
darkturquoise | 0 | 159 | 165 | 0 | 206 | 209 | 0 | 186 | 190 |
darkviolet | 77 | 0 | 168 | 148 | 0 | 211 | 115 | 0 | 193 |
deeppink | 255 | 1 | 76 | 255 | 20 | 147 | 255 | 6 | 114 |
deepskyblue | 0 | 135 | 255 | 0 | 191 | 255 | 0 | 167 | 255 |
dimgray | 36 | 36 | 36 | 105 | 105 | 105 | 69 | 69 | 69 |
dodgerblue | 2 | 73 | 255 | 30 | 144 | 255 | 11 | 110 | 255 |
firebrick | 116 | 3 | 3 | 178 | 34 | 34 | 150 | 13 | 13 |
floralwhite | 255 | 244 | 223 | 255 | 250 | 240 | 255 | 248 | 233 |
forestgreen | 3 | 67 | 3 | 34 | 139 | 34 | 13 | 105 | 13 |
fuchsia | 255 | 0 | 255 | 255 | 0 | 255 | 255 | 0 | 255 |
gainsboro | 184 | 184 | 184 | 220 | 220 | 220 | 205 | 205 | 205 |
ghostwhite | 240 | 240 | 255 | 248 | 248 | 255 | 245 | 245 | 255 |
gold | 255 | 175 | 0 | 255 | 215 | 0 | 255 | 199 | 0 |
goldenrod | 181 | 98 | 3 | 218 | 165 | 32 | 203 | 135 | 12 |
gray | 56 | 56 | 56 | 128 | 128 | 128 | 93 | 93 | 93 |
green | 0 | 56 | 0 | 0 | 128 | 0 | 0 | 93 | 0 |
greenyellow | 109 | 255 | 6 | 173 | 255 | 47 | 144 | 255 | 21 |
honeydew | 223 | 255 | 223 | 240 | 255 | 240 | 233 | 255 | 233 |
hotpink | 255 | 36 | 119 | 255 | 105 | 180 | 255 | 69 | 153 |
indianred | 158 | 27 | 27 | 205 | 92 | 92 | 185 | 57 | 57 |
indigo | 17 | 0 | 58 | 75 | 0 | 130 | 42 | 0 | 95 |
ivory | 255 | 255 | 223 | 255 | 255 | 240 | 255 | 255 | 233 |
khaki | 223 | 203 | 68 | 240 | 230 | 140 | 233 | 219 | 106 |
lavender | 203 | 203 | 244 | 230 | 230 | 250 | 219 | 219 | 248 |
lavenderblush | 255 | 223 | 234 | 255 | 240 | 245 | 255 | 233 | 240 |
lawngreen | 52 | 248 | 0 | 124 | 252 | 0 | 89 | 251 | 0 |
lemonchiffon | 255 | 244 | 158 | 255 | 250 | 205 | 255 | 248 | 185 |
lightblue | 109 | 177 | 203 | 173 | 216 | 230 | 144 | 200 | 219 |
lightcoral | 223 | 56 | 56 | 240 | 128 | 128 | 233 | 93 | 93 |
lightcyan | 192 | 255 | 255 | 224 | 255 | 255 | 211 | 255 | 255 |
lightgoldenrodyellow | 244 | 244 | 166 | 250 | 250 | 210 | 248 | 248 | 192 |
lightgreen | 73 | 219 | 73 | 144 | 238 | 144 | 110 | 230 | 110 |
lightgrey | 168 | 168 | 168 | 211 | 211 | 211 | 193 | 193 | 193 |
lightpink | 255 | 121 | 138 | 255 | 182 | 193 | 255 | 155 | 169 |
lightsalmon | 255 | 91 | 50 | 255 | 160 | 122 | 255 | 129 | 86 |
lightseagreen | 3 | 116 | 105 | 32 | 178 | 170 | 12 | 150 | 141 |
lightskyblue | 63 | 159 | 244 | 135 | 206 | 250 | 100 | 186 | 248 |
lightslategray | 48 | 64 | 83 | 119 | 136 | 153 | 83 | 101 | 121 |
lightsteelblue | 113 | 143 | 188 | 176 | 196 | 222 | 148 | 173 | 208 |
lightyellow | 255 | 255 | 192 | 255 | 255 | 224 | 255 | 255 | 211 |
lime | 0 | 255 | 0 | 0 | 255 | 0 | 0 | 255 | 0 |
limegreen | 7 | 158 | 7 | 50 | 205 | 50 | 23 | 185 | 23 |
linen | 244 | 223 | 203 | 250 | 240 | 230 | 248 | 233 | 219 |
magenta | 255 | 0 | 255 | 255 | 0 | 255 | 255 | 0 | 255 |
maroon | 56 | 0 | 0 | 128 | 0 | 0 | 93 | 0 | 0 |
mediumaquamarine | 34 | 158 | 105 | 102 | 205 | 170 | 66 | 185 | 141 |
mediumblue | 0 | 0 | 158 | 0 | 0 | 205 | 0 | 0 | 185 |
mediumorchid | 127 | 23 | 168 | 186 | 85 | 211 | 160 | 51 | 193 |
mediumpurple | 76 | 42 | 182 | 147 | 112 | 219 | 114 | 76 | 204 |
mediumseagreen | 11 | 117 | 43 | 60 | 179 | 113 | 31 | 152 | 77 |
mediumslateblue | 51 | 35 | 219 | 123 | 104 | 238 | 87 | 68 | 230 |
mediumspringgreen | 0 | 244 | 84 | 0 | 250 | 154 | 0 | 248 | 122 |
mediumturquoise | 16 | 165 | 156 | 72 | 209 | 204 | 40 | 190 | 184 |
mediumvioletred | 148 | 1 | 61 | 199 | 21 | 133 | 177 | 7 | 98 |
midnightblue | 2 | 2 | 42 | 25 | 25 | 112 | 8 | 8 | 76 |
mintcream | 234 | 255 | 244 | 245 | 255 | 250 | 240 | 255 | 248 |
mistyrose | 255 | 199 | 194 | 255 | 228 | 225 | 255 | 216 | 212 |
moccasin | 255 | 199 | 120 | 255 | 228 | 181 | 255 | 216 | 154 |
navajowhite | 255 | 188 | 109 | 255 | 222 | 173 | 255 | 208 | 144 |
navy | 0 | 0 | 56 | 0 | 0 | 128 | 0 | 0 | 93 |
oldlace | 251 | 234 | 203 | 253 | 245 | 230 | 252 | 240 | 219 |
olive | 56 | 56 | 0 | 128 | 128 | 0 | 93 | 93 | 0 |
olivedrab | 38 | 70 | 3 | 107 | 142 | 35 | 71 | 108 | 14 |
orange | 255 | 98 | 0 | 255 | 165 | 0 | 255 | 135 | 0 |
orangered | 255 | 14 | 0 | 255 | 69 | 0 | 255 | 37 | 0 |
orchid | 181 | 42 | 173 | 218 | 112 | 214 | 203 | 76 | 197 |
palegoldenrod | 219 | 207 | 105 | 238 | 232 | 170 | 230 | 222 | 141 |
palegreen | 82 | 246 | 82 | 152 | 251 | 152 | 119 | 249 | 119 |
paleturquoise | 111 | 219 | 219 | 175 | 238 | 238 | 147 | 230 | 230 |
palevioletred | 182 | 42 | 76 | 219 | 112 | 147 | 204 | 76 | 114 |
papayawhip | 255 | 221 | 172 | 255 | 239 | 213 | 255 | 232 | 196 |
peachpuff | 255 | 181 | 126 | 255 | 218 | 185 | 255 | 203 | 159 |
peru | 158 | 61 | 12 | 205 | 133 | 63 | 185 | 98 | 33 |
pink | 255 | 137 | 154 | 255 | 192 | 203 | 255 | 168 | 182 |
plum | 186 | 91 | 186 | 221 | 160 | 221 | 207 | 129 | 207 |
powderblue | 113 | 192 | 203 | 176 | 224 | 230 | 148 | 211 | 219 |
purple | 56 | 0 | 56 | 128 | 0 | 128 | 93 | 0 | 93 |
red | 255 | 0 | 0 | 255 | 0 | 0 | 255 | 0 | 0 |
rosybrown | 130 | 71 | 71 | 188 | 143 | 143 | 163 | 109 | 109 |
royalblue | 13 | 36 | 194 | 65 | 105 | 225 | 34 | 69 | 212 |
saddlebrown | 67 | 14 | 1 | 139 | 69 | 19 | 105 | 37 | 6 |
salmon | 244 | 56 | 43 | 250 | 128 | 114 | 248 | 93 | 78 |
sandybrown | 231 | 97 | 30 | 244 | 164 | 96 | 239 | 133 | 61 |
seagreen | 6 | 67 | 24 | 46 | 139 | 87 | 21 | 105 | 53 |
seashell | 255 | 234 | 219 | 255 | 245 | 238 | 255 | 240 | 230 |
sienna | 91 | 21 | 6 | 160 | 82 | 45 | 129 | 48 | 20 |
silver | 137 | 137 | 137 | 192 | 192 | 192 | 168 | 168 | 168 |
skyblue | 63 | 159 | 213 | 135 | 206 | 235 | 100 | 186 | 226 |
slateblue | 37 | 26 | 158 | 106 | 90 | 205 | 70 | 55 | 185 |
slategray | 42 | 56 | 73 | 112 | 128 | 144 | 76 | 93 | 110 |
snow | 255 | 244 | 244 | 255 | 250 | 250 | 255 | 248 | 248 |
springgreen | 0 | 255 | 55 | 0 | 255 | 127 | 0 | 255 | 92 |
steelblue | 15 | 58 | 119 | 70 | 130 | 180 | 38 | 95 | 153 |
tan | 166 | 119 | 68 | 210 | 180 | 140 | 192 | 153 | 106 |
teal | 0 | 56 | 56 | 0 | 128 | 128 | 0 | 93 | 93 |
thistle | 177 | 135 | 177 | 216 | 191 | 216 | 200 | 167 | 200 |
tomato | 255 | 32 | 15 | 255 | 99 | 71 | 255 | 64 | 39 |
turquoise | 12 | 192 | 163 | 64 | 224 | 208 | 34 | 211 | 189 |
violet | 219 | 58 | 219 | 238 | 130 | 238 | 230 | 95 | 230 |
wheat | 234 | 188 | 117 | 245 | 222 | 179 | 240 | 208 | 152 |
white | 255 | 255 | 255 | 255 | 255 | 255 | 255 | 255 | 255 |
whitesmoke | 234 | 234 | 234 | 245 | 245 | 245 | 240 | 240 | 240 |
yellow | 255 | 255 | 0 | 255 | 255 | 0 | 255 | 255 | 0 |
yellowgreen | 84 | 158 | 7 | 154 | 205 | 50 | 122 | 185 | 23 |
The minimal CSS grammar that all implementations need to support is defined in section 8. However, that grammar is not very helpful when implementing CSS1. Hopefully, the yacc/lex grammar below better serves that purpose.
The format of the productions is optimized for human consumption and some shorthand notation beyond yacc is used:
* : 0 or more + : 1 or more ? : 0 or 1 | : separates alternatives [] : grouping
The productions are:
stylesheet : at_rule* import* at_rule* rule* at_rule* ; import : IMPORT_SYM URL ';' /* E.g., @import url(fun.css); */ ; unary_operator : '-' | '+' ; /* * The only operators in level 1 are slash, space and comma. * An expression `a b c, d e f' stands for a list * [[a,b,c],[d,e,f]]. Note that `a,b,c' is the list * [a,b,c], *not* [[a],[b],[c]]. */ operator : '/' | ',' | /* empty */ ; property : IDENT ; rule : selector [ ',' selector ]* '{' declaration [ ';' declaration ]* '}' ; selector : simple_selector+ [ ':' pseudo_element ]? ; /* * A simple_selector is something like H1, PRE.FOO, * .FOO, etc., or it is an ID: #p004 * * DOT_WO_WS is a `.' without preceding whitespace. * DOT_W_WS is a `.' with preceding whitespace. */ simple_selector : element_name pseudo_class? [ DOT_WO_WS class ]? | DOT_W_WS class | id_selector ; element_name : IDENT ; pseudo_class : LINK_PSCLASS | VISITED_PSCLASS | ACTIVE_PSCLASS ; class : IDENT ; pseudo_element : IDENT ; id_selector : '#' IDENT ; declaration : property ':' expr prio? | /* empty */ /* Prevents syntax errors... */ ; prio : IMPORTANT_SYM /* !important" */ | EXCL token* /* Reserved for future use */ ; expr : term [ operator term ]* ; term : unary_operator? [ NUMBER | STRING | PERCENTAGE | LENGTH | EMS | EXS | IDENT | HEXCOLOR | URL | RGB ] ; at_rule /* Reserved for future use */ : AT_KEYWORD token* [ block | ';' ] ; block /* Reserved for future use */ : '{' [ token | block | ';' ]* '}' ; token : IMPORT_SYM | URL | '+' | '-' | '/' | ',' | IDENT | ':' | DOT_WO_WS | DOT_W_WS | LINK_PSCLASS | VISITED_PSCLASS | ACTIVE_PSCLASS | '#' | IMPORTANT_SYM | EXCL | NUMBER | STRING | PERCENTAGE | LENGTH | EMS | EXS | HEXCOLOR | URL | RGB | '(' token* ')' | '[' token* ']' ;
The following is the input to a lex/flex scanner:
%{ #include "constants.h" /* The constants include definitions similar to the following: #define INCH (25.4 * MM) #define CM (10 * MM) #define MM 1 #define PICA (12 * INCH/72 * MM) #define POINT (INCH/72 * MM) */ %} %a 3000 %o 4000 urlchar [a-zA-Z0-9:/_%~!@#$?*+{};.,|=`'-] d [0-9] notnm [^-a-zA-Z0-9] nmchar [-a-zA-Z0-9]|\\. nmstrt [a-zA-Z]|\\. w [ \t\n]* num {d}+|{d}*\.{d}+ h [0-9a-fA-F] h3 {h}{h}{h} h6 {h3}{h3} h9 {h3}{h3}{h3} ident {nmstrt}{nmchar}* %x COMMENT %% "/*" {BEGIN(COMMENT);} <COMMENT>"*/" {BEGIN(INITIAL);} <COMMENT>\n {/* ignore */} <COMMENT>. {/* ignore */} @import return IMPORT_SYM; @class return CLASS_SYM; @define return DEFINE_SYM; @{ident} return AT_KEYWORD; "!"{w}important return IMPORTANT_SYM; "!"{w}{ident} return EXCL; {ident} {yylval.sym = str2Symbol(yytext); return IDENT;} \"([^"\n]|\\.)*\" | \'([^'\n]|\\.)*\' {yylval.str = noquotes(yytext); return STRING;} {num} {yylval.num = atof(yytext); return NUMBER;} {num}"%" {yylval.num = atof(yytext); return PERCENTAGE;} {num}pt/{notnm} {yylval.num = atof(yytext) * POINT; return LENGTH;} {num}mm/{notnm} {yylval.num = atof(yytext); return LENGTH;} {num}cm/{notnm} {yylval.num = atof(yytext) * CM; return LENGTH;} {num}pc/{notnm} {yylval.num = atof(yytext) * PICA; return LENGTH;} {num}inch/{notnm} {yylval.num = atof(yytext) * INCH; return LENGTH;} {num}px/{notnm} {yylval.num = atof(yytext) * pixelwd; return LENGTH;} {num}em/{notnm} {yylval.num = atof(yytext); return EMS;} {num}ex/{notnm} {yylval.num = atof(yytext); return EXS;} ":"link {return LINK_PSCLASS;} ":"visited {return VISITED_PSCLASS;} ":"active {return ACTIVE_PSCLASS;} "#"{h9} | "#"{h6} | "#"{h3} {yylval.str = yytext; return HEXCOLOR;} "url("[^\n)]+")" {yylval.str = noquotes(yytext+3); return URL;} [a-z]+":"[a-zA-Z~/.%0-9?#@=*_+,;!-]+ {yylval.str = yytext; return URL;} "rgb("{num}%?,{num}%?,{num}%?")" {yylval.str = yytext; return RGB;} ":" return ':'; ^"."|[ \t]+"." return DOT_W_WS; "." return DOT_WO_WS; "/" return '/'; "+" return '+'; "-" return '-'; "{" return '{'; "}" return '}'; ";" return ';'; "," return ','; "#" return '#'; [ \t]+ {/* ignore whitespace */} \n {/* ignore whitespace */} . {yyerror("Illegal character");}