Skip to main content

HTML Guide

CSS: “width”: The types are incompatible.

Using the width attribute with an invalid value or on an element where it’s not allowed causes a “types are incompatible” error.

The width attribute must only be used on certain elements, like img, canvas, input (of type image), or video. Its value should be a valid integer, representing the number of CSS pixels, without units. Using CSS units like px or % directly in the attribute, or placing the attribute on unsupported elements, will trigger this validation message.

Correct usage:

<img src="photo.jpg" width="400" alt="Sample photo">

Incorrect usage (with units):

<img src="photo.jpg" width="400px" alt="Sample photo">

Incorrect usage (on unsupported element):

<div width="400">Content</div>

To set width via CSS instead, use the style attribute or an external stylesheet:

<div style="width: 400px;">Content</div>

For styling, always use CSS when possible; reserve the width attribute for the few HTML elements that specifically support it, and ensure the value is a simple number without units.

Learn more:

Related W3C validator issues