Jens Meiert

Arguments for the “style” Attribute

Jens Meiert, June 27, 2007 / February 25, 2008.

This entry is filed under Web Development.

The HTML Working Group currently argues about dropping or keeping the style attribute, the valued attribute that specifies style information for the current element.

To allow for some additional transparency and to encourage discussion outside the Working Group, here’s what I added to the discussion:

I’ve realized now that the current spec drops the style attribute for all elements except for the <font>, but I can’t understand the reasoning behind those decisions.

And neither do I, and according to my experience as well as the discussion on that subject there are several cases why we need to keep the style attribute.

  • To allow authors to set really varying formatting:

    This is probably not the best description for that argument, so let’s use an example: There is partially strong desire to avoid page load “flickering” caused by images being loaded. The usual way to address that is by setting width and height attributes which should be replaced by the style attribute (that is presentational by design).

    <img src="foo" alt="bar" style="width: 100px; height: 200px;">

    Assuming that we don’t talk about galleries where all thumbs have the same size (and may thus be styled via #gallery img {}, for example), but instead a content rich website that uses many different image or media sizes, there is no other practical option but using style for defining each measurement. (Even though we’d probably specify no image measurements at all.)

  • To enable/ensure styling of external material:

    Another suboptimal description, yet again garnished with an example – take advertising agencies that deliver ad code to commercial websites. Apart from the shadow world their markup appears to come from, we want them to adhere to web standards, too.

    But how’s that going to happen without style? Considering a really simple banner ad where the ad company only wants to make sure there appears no border around the banner, there are basically three options:

    1. Hope for each partner to remove an eventual border. (Doesn’t work.)
    2. Delivering the ad with border="0" (or worse). (Presentational.)
    3. Delivering the ad with style="border: 0;". (More kids in town.)
  • To enable (probable) scoped style sheets:

    Robert Burns already mentioned them, and even though I remember Eric (Meyer) to be unsure about their future, scoped style sheets might become a really great thing one day.

    Arguments for scoped style sheets should probably be collected on www-style, but in theory they could mean even more elegant and effective style sheets […].

Presumably we could go on with compatibility, philosophy, and other arguments … Consequently, I consider it not necessarily wise to drop the style attribute. It’s probably the wrong signal, and we should instead focus on advocating its appropriate use.

A reply to Henri Sivonen (original mail) contained additional details:

In my opinion, it is extremely silly to think that height='' and width='' are bad but style='width: …; height: …;' is good. I’d rather have height='' and width='' on all elements that are replaced elements in the normal case.

Assuming that we don’t want any presentational stuff in our documents at all, but judging the style attribute as the only attribute we would need “in case”, using style for image measurements is better than using width and height attributes, albeit it’s not “good” (nobody claimed that).

And, if these are not enough arguments, there’s yet another reply covering yet more details.

Read More

Enjoy the most popular posts, probably including:

Comments

  1. On July 2, 2007, 11:17 CEST, Laurens Holst said:

    I agree that style should be kept, however your first argument is kind of off; designating the image’s width on <img> elements is not presentation; it’s specifying meta-information of the image, which noticably improves the user experience (the flickering you mentioned).

    The only downside is that due to historical reasons, the attributes override the intrinsic dimensions of the image, which is stylistic behaviour. That’s wrong, but it’s hard to fix. Nevertheless, the attributes still fulfill their meta-information role, especially in the case that you mention.

    ~Grauw

  2. On July 4, 2007, 20:05 CEST, Jens Meiert said:

    That’s an interesting aspect that we should probably add to the style wiki page, too.

    By the way, I couldn’t hold back a slightly emotional reply to the discussion.

  3. On September 26, 2007, 17:02 CEST, Ronald Klip said:

    Another reason not to drop the style attribute is that it is widely used in scripting. Showing or hiding elements, or setting their position. Especially setting an element’s position (for example to use in a dragable div) would be impossible without style - unless you’d create a class rule for every position on the screen.

    .left1 {left: 1px;}
    [..]
    .left1920 {left: 1920px;}
    .top1 {top: 1 px;}
    [..]
    .top1200 {top: 1200px;}

    Somewhat inconvenient.

Leave a Comment

Leave a Comment

Respect the comment guidelines. XHTML allowed: <a href=""> <abbr title=""> <blockquote> <code> <em> <strong>

Found a mistake? Reward! Email me, jens@meiert.com.

You are here: meiert.com – Archive for 2007 – Arguments for the “style” Attribute

Last update: February 25, 2008. Copyright 2000-2008 Jens Meiert. Legal notice.