HTML Guide
The value for the height
property in your CSS must be a valid CSS length or percentage, or the keyword auto
.
height
in CSS accepts values such as px
, em
, rem
, %
, or the keyword auto
. Incompatible values, like an unrecognized string or a missing unit (e.g., just height: 100;
), will trigger this validation error. Always specify a valid unit unless using 0
, which doesn’t require a unit, or use a percentage if the parent element has a defined height.
Correct usage examples:
<style>
.box {
height: 200px;
}
.container {
height: 70%;
}
.image {
height: auto;
}
.zero {
height: 0;
}
</style>
Incorrect usage examples:
<style>
.bad {
height: 100; /* missing unit */
}
.bad2 {
height: big; /* invalid keyword */
}
</style>
Always include a valid unit like px
, %
, em
, or use auto
for the height
property.
Learn more:
Related W3C validator issues
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 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 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 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 attributes width and height of <img> elements expect a non-negative integer, so an empty string is not allowed. Either define the correct dimension, or remove this attribute.
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>
The value used in the height attribute on element iframe is not a valid integer. Remove any leading or trailing spaces from the attribute value.
Here’s an example:
<iframe width="560" height="315" src="your-video-link" frameborder="0" allowfullscreen></iframe>
<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>
The attributes width and height on an iframe expect a valid positive integer without any decimals.
Here’s an example of incorrect code where decimals are being used for dimension attributes:
<iframe src="example.html" height="602.88" width="800.2"></iframe>
Corrected code without decimals:
<iframe src="example.html" height="603" width="800"></iframe>
In the corrected code, the width and height values has been changed to a whole number, which conforms to the standard integer value expected by the W3C validator.