What’s the Difference Between Width/Height in CSS and Width/Height HTML attributes?

  • Home
  • Blog
  • Article
  • What’s the Difference Between Width/Height in CSS and Width/Height HTML attributes?
nc efi placeholder

Some HTML elements accept width and height as attributes. Some do not. For example:


http://css-tricks.com/...




Those attributes are sometimes referred to as presentational attributes. The thing to know about them is that they are overridden by any other styling information whatsoever. That makes them ideal as a fallback.

So, if CSS loads and has a declaration like:

img {
  width: 400px;
}

…that is going to override the width="500" on the tag above. Presentational attributes are the weakest kind of styling, so they are overridden by any CSS, even selectors with very low specificity.

What might be a smidge confusing is that presentational attributes seem like they would have high specificity. These inline styles, for instance, are very strong:

http://css-tricks.com/...

Using an inline style (which works on any element, not a select few), we’ve moved from the weakest way to apply width and height to one of the strongest. Regular CSS will not override this, with a selector of any specificity strength. If we need to override them from CSS, we’ll need !important rules.

img {
  width: 400px !important;
}

To reiterate, presentational attributes on elements that accept them (e.g. ,