HTML Guide
<video> elements accept a height attribute to specify the width in CSS pixels. This value can only be an integer, it should not contain units or %. If you need to specify a percentage width, you can do that with CSS.
Here’s an example of setting width and height on a video element.
<video controls width="640" height="480">
<source src="/media/cc0-videos/flower.webm" type="video/webm">
</video>
Last reviewed: March 23, 2023
Related W3C validator issues
The height attribute on the <video> element must be a non-negative integer representing the height in CSS pixels. The value “auto” is not a valid value for this attribute. To resolve the issue, set the height attribute to a specific numeric value or adjust the height using CSS instead.
Example with a numeric height value:
<video width="640" height="360" controls>
<source src="movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
Alternatively, control the height using CSS:
<video width="640" controls style="height: auto;">
<source src="movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
In the CSS approach, “auto” can be used, but it should not be part of the HTML attributes.
The controlslist attribute on the <video> element is experimental, and not yet valid in the current HTML5 standard, so you should use it with caution as it may not be supported by all browsers.
The <table> element does not accept a height attribute. Use CSS instead.
The W3C HTML Validator issue you encountered indicates that the value of the height attribute for an <img> element is set to “100%”, which is not allowed in HTML. The height attribute must be a specific digit that represents the height in pixels (e.g., “150”), rather than a percentage or any other unit.
How to Fix the Issue
To resolve this issue, you need to specify a numeric value for the height attribute instead. If you want the image to occupy a certain percentage of the container, use CSS instead of the height attribute.
Example of Incorrect HTML
Here is an example that causes the validation issue:
<img src="image.jpg" height="100%" alt="Example Image">
Corrected HTML Using Pixel Values
To correct the issue, replace the percentage with a pixel value:
<img src="image.jpg" height="200" alt="Example Image">
Alternative: Using CSS for Responsive Design
If you want the image to scale responsively and occupy 100% of the width of its container, consider using CSS:
<style>
.responsive-img {
width: 100%;
height: auto; /* Maintains aspect ratio */
}
</style>
<img src="image.jpg" class="responsive-img" alt="Example Image">
Summary
- Do not use percentage values for the height attribute of <img> tags.
- Use pixel values for fixed dimensions.
- For responsive design, use CSS to set the dimensions instead.
The width and height attributes on <img> elements expect a digit to specify the dimension in pixels. It should not contain units, letters or percent signs.
You can achieve this using CSS instead, for example:
<!-- Invalid syntax, the height attribute expects only digits -->
<img src="photo.jpg" alt="cat" height="auto" />
<!-- Valid syntax using CSS -->
<img src="photo.jpg" alt="cat" style="height: auto" />
The error message is indicating that the width attribute of the <video> element has an invalid value. According to the HTML specification, the width attribute expects a non-negative integer value, representing the pixel width of the video.
The value "auto" is not valid for the width attribute. Instead, specify a full number that indicates the pixel width of the video. If you want the video to be responsive without specifying a fixed width, you can use CSS to achieve that.
Here are two ways to resolve this:
-
Specify a valid pixel value for width:
<video width="640" height="360" controls> <source src="video.mp4" type="video/mp4"> Your browser does not support the video tag. </video> -
Use CSS for a responsive video player:
Instead of using the width attribute, use CSS to set the width of the video element. This allows the video to adjust its size according to the container or viewport.
<style> .responsive-video { width: 100%; height: auto; } </style> <video class="responsive-video" controls> <source src="video.mp4" type="video/mp4"> Your browser does not support the video tag. </video>
In the second example, the video will scale based on its containing element, maintaining its aspect ratio due to the height: auto; CSS rule. This approach offers more flexibility for responsive design.
An empty value for the poster attribute on a video element is not valid; the attribute must contain a non-empty URL.
The poster attribute specifies an image to show before the user plays the video. According to the HTML living standard and W3C specifications, if the poster attribute is present, it must have a non-empty value that is a valid URL to an image resource. Using poster="" is invalid and triggers validator errors.
If you do not want to show any poster image, simply omit the poster attribute altogether. If you want to show an image, provide a valid image URL as the value.
Valid examples:
Video without a poster (omit the attribute):
<video controls>
<source src="movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
Video with a poster image:
<video controls poster="thumbnail.jpg">
<source src="movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
Invalid (causes error):
<video controls poster="">
<source src="movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
Always either use a valid URL in the poster attribute or omit it entirely to ensure HTML validity.
The attributes width and height of <iframe> elements expect a non-negative integer, so an empty string is not allowed. Either define the correct dimension, or remove this attribute.
The HTML specification requires that the width and height attributes on <img> elements, when present, contain a string representing a non-negative integer — that is, a sequence of one or more ASCII digits like "0", "150", or "1920". An empty string ("") does not satisfy this requirement, so the W3C validator flags it as an error.
This issue commonly arises when:
- A CMS or templating engine outputs width="" or height="" because no dimension value was configured.
- JavaScript dynamically sets img.setAttribute("width", "") instead of removing the attribute.
- A developer adds the attributes as placeholders intending to fill them in later but forgets to do so.
Why it matters
Providing valid width and height attributes is one of the most effective ways to prevent Cumulative Layout Shift (CLS). Browsers use these values to calculate the image’s aspect ratio and reserve the correct amount of space before the image loads. When the values are empty strings, the browser cannot determine the aspect ratio, so no space is reserved — leading to layout shifts as images load in, which hurts both user experience and Core Web Vitals scores.
Beyond performance, invalid attribute values can cause unpredictable rendering behavior across browsers. Some browsers may ignore the attribute, others may interpret the empty string as 0, collapsing the image to zero pixels in that dimension. Standards-compliant HTML also improves accessibility by ensuring assistive technologies can parse the document reliably.
Examples
❌ Invalid: empty string values
<img src="photo.jpg" alt="A sunset over the ocean" width="" height="">
Both width and height are set to empty strings, which is not valid.
✅ Fixed: provide actual dimensions
<img src="photo.jpg" alt="A sunset over the ocean" width="800" height="600">
Replace the empty strings with the image’s actual pixel dimensions. These values should reflect the image’s intrinsic (natural) size. CSS can still be used to scale the image visually — the browser will use the width and height ratio to reserve the correct space.
✅ Fixed: remove the attributes entirely
<img src="photo.jpg" alt="A sunset over the ocean">
If you don’t know the dimensions or prefer to handle sizing purely through CSS, remove the attributes altogether. An absent attribute is valid; an empty one is not.
❌ Invalid: only one attribute is empty
<img src="banner.jpg" alt="Promotional banner" width="1200" height="">
Even if only one attribute has an empty value, the validation error will be triggered for that attribute.
✅ Fixed: both attributes with valid values
<img src="banner.jpg" alt="Promotional banner" width="1200" height="400">
Fixing dynamic/template-generated markup
If a template language is outputting empty attributes, use a conditional to omit them when no value is available. For example, in a template:
<!-- Instead of always outputting the attributes: -->
<img src="photo.jpg" alt="Description" width="" height="">
<!-- Conditionally include them only when values exist: -->
<img src="photo.jpg" alt="Description" width="800" height="600">
If you’re setting dimensions via JavaScript, remove the attribute rather than setting it to an empty string:
// ❌ Don't do this
img.setAttribute("width", "");
// ✅ Do this instead
img.removeAttribute("width");
// ✅ Or set a valid value
img.setAttribute("width", "800");
A note on values
The width and height attributes only accept non-negative integers — whole numbers without units, decimals, or percentage signs. Values like "100px", "50%", or "3.5" are also invalid. Use plain integers like "100" or "600". If you need responsive sizing with percentages or other CSS units, apply those through CSS styles instead.
Remove the unit; height on embed expects a non-negative integer (pixels) or a valid CSS length only when set via CSS, not the HTML attribute.
Detailed explanation
The embed element supports the presentational attributes width and height as unsigned integers representing CSS pixels. In HTML, the height attribute must be a number without a unit, for example 650. Supplying 650px violates the attribute’s value syntax and triggers the validator error.
If you want to use units or other CSS lengths (e.g., px, em, %, vh), set them with CSS via the style attribute or a stylesheet using the height property, not the HTML attribute.
HTML examples
Example reproducing the issue
<!DOCTYPE html>
<html lang="en">
<head>
<title>Embed height invalid</title>
</head>
<body>
<embed src="file.pdf" type="application/pdf" width="800" height="650px">
</body>
</html>
Corrected example (HTML attribute as integer)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Embed height fixed</title>
</head>
<body>
<embed src="file.pdf" type="application/pdf" width="800" height="650">
</body>
</html>
Alternative corrected example (use CSS units)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Embed height via CSS</title>
<style>
.viewer { width: 800px; height: 650px; }
</style>
</head>
<body>
<embed class="viewer" src="file.pdf" type="application/pdf">
</body>
</html>