When should I use attributes vs CSS styles?

css, html, xhtml

Solution

Although the border attribute is not deprecated whereas the align attribute is (to give light to the status of the attribute as of the HTML4 recommendation), it is commonly recommended to not place these attributes in your markup.

I cannot find a reference to these attributes when looking at XHTML table documentation, so I would assume these are not acceptable there.

In place of the attributes in question, using CSS eliminates mixing content and styling. XHTML seems to be completely focused on content as opposed to styling, so I would have to say that although it is acceptable, it is not the best route to use the attributes in place of CSS in an external stylesheet.

One case where I could see using attributes instead of css would be in designing a html email, as most email clients strip css unless it is declared inline using the style attribute. It may be cleaner to use the individual attributes than stuffing all of the rules into the style attribute in this case.

UPDATE

Due to information provided in a comment from another user, I see that the XHTML Spec i was looking at is outdated. Modern XHTML does recognize and support attributes such as border. The link provided is an explanation of the differences between html and xhtml as an introduction to the subject. Good stuff. The comment I am referencing is as follows :

When you discuss the XHTML documentation, you mean the aborted XHTML 2 specification. Modern XHTML is XHTML5, which has the same attribute and content model rules as regular HTML5, just a different syntax. The authors of XHTML 2 set out to remove all the crud that had built up on HTML over the years, and so took a very purist approach. The HTML5 authors took a much more pragmatic attitude, recognised the certain odd features, like border="1", were actually useful and permitted them for those narrow situations.

Problem

Sometimes in xhtml we write suppose `<table border="1">` and then again in the css we write `table{ border:2px solid black}`. I am confused when to write which. When to use attributes and when to use the css. Sometimes they get confusing.

Original source

Related problems