舊文件

此處文件僅供參考,請自行考量時效性與適用程度,其他庫藏文件請參考文件頁面
我們亟需您的協助,進行共筆系統搬移、及文件整理工作,詳情請查閱參與我們

CSS 一般問題

出自 MozTW Wiki

於 2005年9月29日 (四) 16:17 由 BobChao對話 | 貢獻 所做的修訂
(比較) ←上一修訂 | 查看目前修訂 (比較) | 下一修訂→ (比較)

這份文件舉出使用 CSS 時經常會碰上的狀況,並予以解答。本文編譯自 Common CSS Questions,在 devmo 有正體中文版時可能會移過去。

編修開始 --BobChao 16:17 2005年九月29日 (CST)

My CSS is valid, but not correctly rendered

If you want most browsers to correctly render standards compliant HTML/CSS pages, you have to put a full and valid DOCTYPE at the beginning of the HTML file.

Modern browsers have two main rendering modes:

  • Quirks Mode: also called backwards-compatibility mode, allows legacy webpages to be rendered as their authors intended following the non-standard rendering rules used by older browsers.
  • Standards Mode: the browser attempts to follow the W3C standards strictly.

Gecko-based browsers have a third Almost Standards Mode that has only a few minor quirks.

If you declare an incomplete or outdated DTD, or omit it altogether, your browser will render your page in Quirks Mode.

This is a list of the most commonly used DTDs that will trigger Standards or Almost Standards mode:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Difference between id and class

HTML elements can have an id and/or a class attribute. The id attribute allows a name to be given to a certain element, and there must be only one element with that name. The class attribute allows an element to be assigned to a certain class, and in general there could be more than one element with the same class attribute. CSS allows styles to be applied to particular id and/or class.

Use an id-specific style when you want to restrict the style rules to one specific block or element. This style will be used by only one element.

Use a class-specific style when you want to share the style rules across a certain class of blocks and elements.

See CSS selectors

Restore the default property value

CSS2 does not have a way to specify the default value of a CSS property. The only way to restore the default value is to explicitly re-declare that property with the default value (you have to know it, because CSS does not provide any default keyword).

Thus, particular care should be taken when writing style rules using selectors (e.g., selectors by tag name, such as p) that you may want to override with more specific rules (such as those using ID or class selectors), because the original default value cannot be automatically restored.

Because of the cascading nature of CSS, it is good practice to define style rules as specifically as possible, in order to style only what you want to be styled.

Derived styles

CSS does not allow one style to be defined in terms of another. (See Eric Meyer's note about the Working Group's stance). However, assigning multiple classes to a single element can provide the same effect.

Assigning multiple classes

HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to seperate them.

<style type="text/css">
.firstclass { background: black; color: white; }
.secondclass { font-weight: bold; }
</style>

<div class="firstclass secondclass">
... content ...
... content ...
... content ...
</div>

If the same property is declared in both rules, the conflict is resolved first through specificity, then according to the order of the CSS declarations. The order of classes in the class attribute is not relevant.

Style rules that don't work

Correctly defined style rules can be ignored. Usually, this is the correct behaviour, according to syntax and priority rules.

These are the most frequent causes of style rules being ignored:

  • HTML elements hierarchy
  • Explicitly re-defined style rule
  • Use of a shorthand property
  • Use of the * selector
  • Specificity in CSS

You can use DOM Inspector's CSS Style Rules view to debug problems of this kind.

HTML elements hierarchy

The way CSS styles are applied to HTML elements depends also on the elements hierarchy. It is important to remember that a rule applied to a descendant overrides the style of the parent, in spite of any specificity or priority of CSS rules.

#section { font-weight: bold; }
.redtext { font-weight: normal; color: red; }

<div id="section">
   This is bold, 
   <span class="redtext"> this is normal and red,</span>
   and bold again
</div>

In case of complex HTML hierarchies, if a rule seems to be ignored, check if the element is inside another element with a different style.

Explicitly re-defined style rule

In CSS stylesheets, order is important. If you define a rule and then you re-define the same rule, the last definition is used.

#section { font-weight: bold; }
.redtext { color: red; }
/*  other rules             */
/*  other rules             */
/*  other rules             */
.redtext { font-weight: normal; }

<div id="section">
   This is bold, 
   <span class="redtext"> this is normal and red,</span>
   and bold again
</div>

To avoid this kind of error, try to define rules only once for a certain selector, and group all rules belonging to that selector.


Use of a shorthand property

Using shorthand properties for defining style rules is good because it uses a very compact syntax. Using shorthand with only some attributes is possible and correct, but it must be remembered that undeclared attributes are automatically reset to default. This means that a previous rule for a single attribute could be implicitly overridden.

#section { font-size: 12px; font-family: Verdana; font-weight: bold; }
.redtext { font: 14px Arial; color: red; }

<div id="section">
   This is bold 12px Verdana, 
   <span class="redtext">this is normal 14px Arial and red,</span>
   and bold 12px Verdana again
</div>

In the previous example the problem occurred on rules belonging to different elements, but it could happen also for the same element, because rule order is important.

#section {
   font-weight: bold;
   font: 12px Verdana;  /* font-weight is now normal */
}


Use of the * selector

The * selector referd to any element, and it has to be used with particular care.

body * { font-weight: normal; }
#section { font: 12px Verdana; }
.boldtext { font-weight: bold; }
.redtext { color: red; }

<div id="section">
   This is normal,
   <span class="boldtext">
      <span class="redtext"> this is normal and red,</span>
   </span>
   and normal again
</div>

In this example the body * selector applys the rule to all elements inside body, at any hierarchy level, including redtext. So font-weight: bold; applied to boldtext class is overridden by font-weight: normal; applied to redtext.


Specificity in CSS

When multiples rules apply to a certain element, the rule chosen depends on its style specificity . Inline styles (in HTML style attributes) come first, followed by id styles, then class styles and eventually element-name styles.

div { color: black; }
#orange { color: orange; }
.green { color: green; }

<div id="orange" class="green" style="color: red;">This is red</div>

The rules are more complicated when the selector has multiple parts. More detailed information about how selector specificity is calculated can be found in the CSS 2.1 Specification chapter 6.4.3

What do the -moz-* properties do?

Please see the Mozilla CSS Extensions page.

個人工具