aboutsummaryrefslogtreecommitdiff
path: root/files/pt-pt/learn/css/howto/faq_de_css/index.html
blob: 357e271657aaad8c1f974dd04e02a62c016821c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
---
title: Perguntas Frequentes sobre CSS
slug: Learn/CSS/Howto/FAQ_de_CSS
tags:
  - CSS
  - Exemplo
  - FAQ
  - Guía
  - Web
  - questões
translation_of: Learn/CSS/Howto/CSS_FAQ
---
<h2 id="Why_doesn't_my_CSS_which_is_valid_render_correctly">Why doesn't my CSS, which is valid, render correctly?</h2>

<p>Browsers use the <code>DOCTYPE</code> declaration to choose whether to show the document using a mode that is more compatible  with Web standards or with old browser bugs. Using a correct and modern <code>DOCTYPE</code> declaration at the start of your HTML will improve browser standards compliance.</p>

<p>Modern browsers have two main rendering modes:</p>

<ul>
 <li><em>Quirks Mode</em>: 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. Documents with an incomplete, incorrect, or missing <code>DOCTYPE</code> declaration or a known <code>DOCTYPE</code> declaration in common use before 2001 will be rendered in Quirks Mode.</li>
 <li><em>Standards Mode</em>: the browser attempts to follow the W3C standards strictly. New HTML pages are expected to be designed for standards-compliant browsers, and as a result, pages with a modern <code>DOCTYPE</code> declaration will be rendered with Standards Mode.</li>
</ul>

<p>Gecko-based browsers, have a third <em><a href="/en-US/docs/Gecko's_&quot;Almost_Standards&quot;_Mode" title="Gecko's_&quot;Almost_Standards&quot;_Mode">Almost Standards Mode</a></em> that has only a few minor quirks.</p>

<p>This is a list of the most commonly used <code>DOCTYPE</code> declarations that will trigger Standards or Almost Standards mode:</p>

<pre>&lt;!DOCTYPE html&gt; /* This is the HTML5 doctype. Given that each modern browser uses an HTML5
                   parser, this is the recommended doctype */

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

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

<p>When at all possible, you should just use the HTML5 doctype.</p>

<h2 id="My_CSS_is_valid.2C_but_not_correctly_rendered" name="My_CSS_is_valid.2C_but_not_correctly_rendered">Why doesn't my CSS, which is valid, render at all?</h2>

<p>Here are some possible causes:</p>

<ul>
 <li>You've got the path to CSS file wrong.</li>
 <li>To be applied, a CSS stylesheet must be served with a <code>text/css</code> MIME type. If the Web server doesn't serve it with this type, it won't be applied.</li>
</ul>

<h2 id="Difference_between_id_and_class" name="Difference_between_id_and_class">What is the difference between <code>id</code> and <code>class</code>?</h2>

<p>HTML elements can have an <code>id</code> and/or <code>class</code> attribute. The <code>id</code> attribute assigns a name to the element it is applied to, and for valid markup, there can be only one element with that name. The <code>class</code> attribute assigns a class name to the element, and that name can be used on many elements within the page. CSS allows you to apply styles to particular <code>id</code> and/or <code>class</code> names.</p>

<ul>
 <li>Use a class-specific style when you want to apply the styling rules to many blocks and elements within the page, or when you currently only have element to style with that style, but you might want to add more later.</li>
 <li>Use an id-specific style when you need to restrict the applied styling rules to one specific block or element. This style will only be used by the element with that particular id.</li>
</ul>

<p>It is generally recommended to use classes as much as possible, and to use ids only when absolutely necessary for specific uses (like to connect label and form elements or for styling elements that must be semantically unique):</p>

<ul>
 <li>Using classes makes your styling extensible — even if you only have one element to style with a particular ruleset now, you might want to add more later.</li>
 <li>Classes allow you to style multiple elements, therefore they can lead to shorter stylesheets, rather than having to write out the same styling information in multiple rules that use id selectors. Shorter stylesheets are more performant.</li>
 <li>Class selectors have lower <a href="/en-US/docs/Learn/CSS/Introduction_to_CSS/Cascade_and_inheritance#Specificity">specificity</a> than id selectors, so are easier to override if needed.</li>
</ul>

<div class="note">
<p><strong>Note</strong>: See <a href="/en-US/docs/Learn/CSS/Introduction_to_CSS/Selectors">Selectors</a> for more information.</p>
</div>

<h2 id="Restoring_the_default_property_value" name="Restoring_the_default_property_value">How do I restore the default value of a property?</h2>

<p>Initially CSS didn't provide a "default" keyword and the only way to restore the default value of a property is to explicitly re-declare that property. For example:</p>

<pre class="brush: css">/* Heading default color is black */
h1 { color: red; }
h1 { color: black; }</pre>

<p>This has changed with CSS 2; the keyword <a href="/es/CSS/initial" title="initial">initial</a> is now a valid value for a CSS property. It resets it to its default value, which is defined in the CSS specification of the given property.</p>

<pre class="brush: css">/* Heading default color is black */
h1 { color: red; }
h1 { color: initial; }</pre>

<h2 id="Derived_styles" name="Derived_styles">How do I derive one style from another?</h2>

<p>CSS does not exactly allow one style to be defined in terms of another. (See <a href="http://archivist.incutio.com/viewlist/css-discuss/2685">Eric Meyer's note about the Working Group's stance</a>). However, assigning multiple classes to a single element can provide the same effect, and <a href="/en-US/docs/Web/CSS/Using_CSS_variables">CSS Variables</a> now provide a way to define style information in one place that can be reused in multiple places.</p>

<h2 id="Assigning_multiple_classes" name="Assigning_multiple_classes">How do I assign multiple classes to an element?</h2>

<p>HTML elements can be assigned multiple classes by listing the classes in the <code>class</code> attribute, with a blank space to separate them.</p>

<pre>&lt;style type="text/css"&gt;
.news { background: black; color: white; }
.today { font-weight: bold; }
&lt;/style&gt;

&lt;div class="news today"&gt;
... content of today's news ...
&lt;/div&gt;
</pre>

<p>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 <code>class</code> attribute is not relevant.</p>

<h2 id="Style_rules_that_don.27t_work" name="Style_rules_that_don.27t_work">Why don't my style rules work properly?</h2>

<p>Style rules that are syntactically correct may not apply in certain situations. You can use <a href="/en-US/docs/DOM_Inspector" title="DOM_Inspector">DOM Inspector</a>'s <em>CSS Style Rules</em> view to debug problems of this kind, but the most frequent instances of ignored style rules are listed below.</p>

<h3 id="HTML_elements_hierarchy" name="HTML_elements_hierarchy">HTML elements hierarchy</h3>

<p>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 descendent overrides the style of the parent, in spite of any specificity or priority of CSS rules.</p>

<pre>.news { color: black; }
.corpName { font-weight: bold; color: red; }

&lt;!-- news item text is black, but corporate name is red and in bold --&gt;
&lt;div class="news"&gt;
   (Reuters) &lt;span class="corpName"&gt;General Electric&lt;/span&gt; (GE.NYS) announced on Thursday...
&lt;/div&gt;
</pre>

<p>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.</p>

<h3 id="Explicitly_re-defined_style_rule" name="Explicitly_re-defined_style_rule">Explicitly re-defined style rule</h3>

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

<pre>#stockTicker { font-weight: bold; }
.stockSymbol { color: red; }
/*  other rules             */
/*  other rules             */
/*  other rules             */
.stockSymbol { font-weight: normal; }

&lt;!-- most text is in bold, except "GE", which is red and not bold --&gt;
&lt;div id="stockTicker"&gt;
   NYS: &lt;span class="stockSymbol"&gt;GE&lt;/span&gt; +1.0 ...
&lt;/div&gt;
</pre>

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

<h3 id="Use_of_a_shorthand_property" name="Use_of_a_shorthand_property">Use of a shorthand property</h3>

<p>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 their default values. This means that a previous rule for a single attribute could be implicitly overridden.</p>

<pre>#stockTicker { font-size: 12px; font-family: Verdana; font-weight: bold; }
.stockSymbol { font: 14px Arial; color: red; }

&lt;div id="stockTicker"&gt;
   NYS: &lt;span class="stockSymbol"&gt;GE&lt;/span&gt; +1.0 ...
&lt;/div&gt;
</pre>

<p>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 <strong>is</strong> important.</p>

<pre>#stockTicker {
   font-weight: bold;
   font: 12px Verdana;  /* font-weight is now set to normal */
}
</pre>

<h3 id="Use_of_the_.2A_selector" name="Use_of_the_.2A_selector">Use of the <code>*</code> selector</h3>

<p>The <code>*</code> wildcard selector refers to any element, and it has to be used with particular care.</p>

<pre>body * { font-weight: normal; }
#stockTicker { font: 12px Verdana; }
.corpName { font-weight: bold; }
.stockUp { color: red; }

&lt;div id="section"&gt;
   NYS: &lt;span class="corpName"&gt;&lt;span class="stockUp"&gt;GE&lt;/span&gt;&lt;/span&gt; +1.0 ...
&lt;/div&gt;
</pre>

<p>In this example the <code>body *</code> selector applies the rule to all elements inside body, at any hierarchy level, including the <code>.stockUp</code> class. So <code>font-weight: bold;</code> applied to the <code>.corpName</code> class is overridden by <code>font-weight: normal;</code> applied to all elements in the body.</p>

<p>The use of the * selector should be minimized as it is a slow selector, especially when not used as the first element of a selector. Its use should be avoided as much as possible.</p>

<h3 id="Specificity_in_CSS" name="Specificity_in_CSS">Specificity in CSS</h3>

<p>When multiple rules apply to a certain element, the rule chosen depends on its style <a href="/en-US/docs/Learn/CSS/Introduction_to_CSS/Cascade_and_inheritance#Specificity" title="Specificity">specificity</a>. Inline style (in HTML <code>style</code> attributes) has the highest specificity and will override any selectors, followed by ID selectors, then class selectors, and eventually element selectors. The text color of the below {{htmlelement("div")}} will therefore be red.</p>

<pre>div { color: black; }
#orange { color: orange; }
.green { color: green; }

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

<p>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 <a href="http://www.w3.org/TR/CSS21/cascade.html#specificity">CSS 2.1 Specification chapter 6.4.3</a>.</p>

<h2 id="What_do_the_-moz-.2A_properties_do.3F" name="What_do_the_-moz-.2A_properties_do.3F">What do the -moz-*, -ms-*, -webkit-*, -o-* and -khtml-* properties do?</h2>

<p>These properties, called <em>prefixed properties</em>, are extensions to the CSS standard. They allow use of experimental and non-standard features in browsers without polluting the regular namespace, preventing future incompatibilities to arise when the standard is extended.</p>

<p>The use of such properties on production websites is not recommended — they have already created a huge web compatibility mess. For example, many developers only using the <code>-webkit-</code> prefixed version of a property when the non-prefixed version is supported across all browsers meant that a feature relying on that property would break in non-webkit-based browsers, completely needlessly. This problem got so bad that other browsers started to implement <code>-webkit-</code> prefixed aliases to improve web compatibility, as specified in the <a href="https://compat.spec.whatwg.org/">Compatibility Living Standard</a>.</p>

<p>In fact most browsers now do not use CSS prefixes when implementing experimental features, insteading implementing those features only on Nightly browser versions or similar.</p>

<p>If you need to use prefixes in your work, you are advised to write your code in a way that uses the prefixed versions first, but then includes a non-prefixed standard version afterwards so it can automatically override the prefixed versions where supported. For example:</p>

<pre class="brush: css line-numbers  language-css"><code class="language-css"><span class="property token">-ms-transform</span><span class="punctuation token">:</span> <span class="function token">rotate</span><span class="punctuation token">(</span><span class="number token">90</span>deg<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="property token">-webkit-transform</span><span class="punctuation token">:</span> <span class="function token">rotate</span><span class="punctuation token">(</span><span class="number token">90</span>deg<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="property token">transform</span><span class="punctuation token">:</span> <span class="function token">rotate</span><span class="punctuation token">(</span><span class="number token">90</span>deg<span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre>

<div class="note">
<p><strong>Note</strong>: For more information on dealing with prefixed properties, see <a href="/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/HTML_and_CSS#Handling_CSS_prefixes">Handling common HTML and CSS problems — Handling CSS prefixes</a> from our <a href="/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing">Cross-browser testing</a> module.</p>
</div>

<div class="note">
<p><strong>Note</strong>: See the <a href="/en-US/docs/CSS/CSS_Reference/Mozilla_Extensions" title="CSS Reference/Mozilla Extensions">Mozilla CSS Extensions</a> page for more information on the Mozilla-prefixed CSS properties.</p>
</div>

<h2 id="How_does_z-index_relate_to_positioning">How does z-index relate to positioning?</h2>

<p>The z-index property specifies the stack order of elements.</p>

<p>An element with a higher z-index/stack order is always rendered in front of an element with a lower z-index/stack order on the screen. Z-index will only work on elements that have a specified position (<code>position:absolute</code>, <code>position:relative</code>, or <code>position:fixed</code>).</p>

<div class="note">
<p><strong>Nota</strong>: For more information, see our <a href="/en-US/docs/Learn/CSS/CSS_layout/Positioning">Positioning</a> learning article, and in particular the <a href="/en-US/docs/Learn/CSS/CSS_layout/Positioning#Introducing_z-index">Introducing z-index</a> section.</p>
</div>