Guías HTML para margen
Aprende a identificar y corregir errores comunes de validación HTML marcados por el W3C Validator, para que tus páginas cumplan con los estándares y se muestren correctamente en todos los navegadores. También consulta nuestras Guías de accesibilidad.
CSS length values must always pair a number with a unit — writing just px, em, %, or any other unit without a preceding number is meaningless to the browser and will be ignored. This typically happens due to a typo, a copy-paste error, or a build tool / template that outputs a unit without its corresponding numeric value (e.g., a variable that resolved to an empty string concatenated with px).
When the W3C validator encounters margin: px in an inline style attribute, it flags the error because px on its own does not match any valid CSS value for the margin property. Valid values include lengths like 10px or 2em, percentages like 5%, the keyword auto, or the number 0 (which doesn’t need a unit). Browsers will discard the invalid declaration, meaning your intended spacing won’t be applied — potentially breaking your layout in subtle ways that are hard to debug.
This issue also applies to other CSS properties that accept length values, such as padding, width, height, top, left, border-width, font-size, and many more. The fix is always the same: ensure every unit has an accompanying number.
How to Fix It
- Add the missing number before the unit: change px to something like 10px, 1.5em, or 20%.
- Use 0 without a unit if you want zero margin — writing margin: 0 is valid and preferred over margin: 0px.
- Use a keyword if appropriate, such as margin: auto for centering.
- Remove the declaration if the margin value was unintentional or unnecessary.
If the value comes from a preprocessor, template engine, or JavaScript, check that the variable being interpolated is not empty or undefined before it gets concatenated with the unit string.
Examples
Incorrect: Unit Without a Number
<div style="margin: px;">Content</div>
The value px has no number, so this is invalid CSS.
Correct: Number Paired With Unit
<div style="margin: 10px;">Content</div>
Correct: Zero Margin (No Unit Needed)
<div style="margin: 0;">Content</div>
Correct: Using a Keyword
<div style="margin: auto;">Content</div>
Incorrect in an External Stylesheet
This same error can appear in a <style> block or linked stylesheet:
<style>
.card {
margin: px;
}
</style>
Correct in an External Stylesheet
<style>
.card {
margin: 16px;
}
</style>
Watch Out for Template Variables
A common cause in templating systems is an empty variable:
<!-- If spacing is empty, this produces "margin: px;" -->
<div style="margin: {{ spacing }}px;">Content</div>
To prevent this, ensure the variable contains the full value including the number, or add a fallback:
<div style="margin: 16px;">Content</div>
The margin shorthand property sets the margin area on all four sides of an element. It accepts one to four values, where each value must be a valid CSS length (e.g., 10px, 1em, 0), a percentage, or the keyword auto. When the validator reports “Too many values or values are not recognized,” it means either more than four values were supplied, or at least one of the values is something CSS doesn’t understand — such as a misspelled unit, a missing unit on a non-zero number, or an invalid keyword.
Common causes of this error include:
- Too many values: Providing five or more values (e.g., margin: 1px 2px 3px 4px 5px). The shorthand accepts a maximum of four.
- Missing units: Writing a non-zero number without a unit (e.g., margin: 10 instead of margin: 10px). Only 0 is valid without a unit.
- Typos or invalid units: Using a misspelled or nonexistent unit like margin: 10xp or margin: 10pixels.
- Invalid keywords: Using a keyword that isn’t recognized in the margin context (e.g., margin: none). The only non-global keyword margin accepts is auto.
- Missing separators or extra characters: Including commas or other unexpected characters between values (e.g., margin: 10px, 20px). Values should be separated by spaces, not commas.
This matters because browsers may ignore or misinterpret an invalid margin declaration entirely, leading to broken or inconsistent layouts across different browsers. Writing valid CSS ensures predictable rendering and easier maintenance.
How margin shorthand values work
The number of values you provide determines how they are applied:
- 1 value: Applied to all four sides. margin: 10px → top, right, bottom, and left all get 10px.
- 2 values: First is top and bottom, second is left and right. margin: 10px 20px → top/bottom 10px, left/right 20px.
- 3 values: First is top, second is left and right, third is bottom. margin: 10px 20px 30px.
- 4 values: Applied clockwise — top, right, bottom, left. margin: 10px 20px 30px 40px.
Examples
❌ Too many values
/* Five values — invalid */
.box {
margin: 10px 20px 30px 40px 50px;
}
❌ Missing unit on a non-zero number
.box {
margin: 10 20px;
}
❌ Invalid keyword
.box {
margin: none;
}
❌ Comma-separated values
.box {
margin: 10px, 20px;
}
✅ Correct: one to four valid values
/* All four sides */
.box {
margin: 10px;
}
/* Top/bottom and left/right */
.box {
margin: 10px 20px;
}
/* Top, left/right, bottom */
.box {
margin: 10px auto 20px;
}
/* Top, right, bottom, left */
.box {
margin: 10px 20px 30px 40px;
}
✅ Correct: using auto for centering
.container {
margin: 0 auto;
}
✅ Correct: zero without a unit
.box {
margin: 0;
}
✅ Correct: using global keywords
.box {
margin: inherit;
}
If you need to set margins on more than four sides independently (which isn’t possible — elements only have four sides), you likely have a logic error. If you want fine-grained control, use the individual longhand properties (margin-top, margin-right, margin-bottom, margin-left) instead of the shorthand.
In CSS, property values such as lengths, percentages, and keywords are written without quotation marks. Quotes in CSS are reserved for specific contexts like content property strings, font family names with spaces, and url() paths. When you wrap a margin value (or any similar CSS property value) in double or single quotes, the CSS parser interprets it as a string literal rather than a set of length or keyword values. Since "0 0 1em 0" is a string and not a valid margin value, the declaration is ignored by browsers and flagged by the W3C validator.
This is a problem for several reasons. First, the style will silently fail — browsers discard CSS declarations they can’t parse, so your intended margins won’t be applied, potentially breaking your layout. Second, it indicates a misunderstanding of CSS syntax that could lead to similar errors in other properties. This mistake commonly occurs when developers confuse HTML attribute quoting rules with CSS value syntax, especially when writing inline style attributes where the attribute value itself is already quoted.
The margin property accepts one to four values, each of which can be a length (e.g., 10px, 1em), a percentage, auto, or a global keyword like inherit. None of these require quotes. The fix is straightforward: remove the quotation marks around the CSS value.
Examples
❌ Incorrect: margin value wrapped in quotes
In a <style> block:
<style>
.card {
margin: "0 0 1em 0";
}
</style>
In an inline style:
<p style="margin: '10px auto'">Hello</p>
Both of these produce the validator error because the CSS parser sees a quoted string instead of valid margin values.
✅ Correct: margin value without quotes
In a <style> block:
<style>
.card {
margin: 0 0 1em 0;
}
</style>
In an inline style:
<p style="margin: 10px auto">Hello</p>
Valid margin value formats
For reference, here are the accepted patterns for the margin property — none of which use quotes:
/* All four sides */
margin: 1em;
/* Vertical | Horizontal */
margin: 5% auto;
/* Top | Horizontal | Bottom */
margin: 1em auto 2em;
/* Top | Right | Bottom | Left */
margin: 2px 1em 0 auto;
/* Global keywords */
margin: inherit;
margin: initial;
margin: unset;
Watch out for inline style quoting confusion
A common source of this mistake is confusion about the quotes used for the HTML style attribute versus the CSS values inside it. The outer quotes delimit the attribute value for HTML — the CSS inside should not have its own quotes around property values:
<!-- ❌ Wrong: extra quotes around the CSS value -->
<div style="margin: '1em'"></div>
<!-- ✅ Correct: only the HTML attribute is quoted -->
<div style="margin: 1em"></div>
This same rule applies to other CSS properties like padding, border, font-size, color, and so on. If you see a similar validator error for any CSS property, check whether you’ve accidentally quoted the value.
¿Listo para validar tus sitios?
Comienza tu prueba gratuita hoy.