Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I know that some attributes are mandatory for some HTML elements. For example, to write W3C compliant code, img element should have alt attribute.

In order to stay away from bad habits:

  • Are there any attributes that I should use for every tag?

  • Are there any attributes I should never use?

share|improve this question

closed as off topic by World Engineer, gnat, StuperUser, Blrfl, Jim G. Oct 18 '12 at 13:38

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

3 Answers

There really isn't a good answer to this question as it depends on what you are coding, what browser you are targeting, and other factors.

But going off the link above the global attributes that you will use the most are ID and CLASS. ID is used to style and target (with jQuery and other frameworks) unique elements whereas CLASS will be used on many elements, usually with the same or similar styles.

IMPORTANT: Be sure to understand your target audience when starting a project so you know what browsers to code for. This will determine whether or not you can even leverage the latest tags/attributes and will determine much of what you can do.

share|improve this answer

Are there any attributes that I should use for every tag?

No. There is no reason for this. You should use attribute then it makes sense.

Are there any attributes you recommend I stay away from?

No. They are all valuable to some extent (varying in different cases).

P.S. For HTML/CSS and JavaScript reference I would recommend using MDN and W3 (then you need to dig really deep) resources. This is why.

share|improve this answer

The set of global attributes depends on HTML version. Browser support varies, too.

For example, according to the W3C HTML5 document (which is a draft that may change at any moment without prior notice, but still regarded as “de facto standard” by many), the set of global attributes is very large and potentially infinite (it includes any attribute that starts with data- and satisfies some general constraints).

There is no attribute that needs to be used for every tag. You use attributes because you have some use for them, or expect to have use for them in the future. For example, it is a good idea to assign an id attribute to any subheading (like h2), because people visiting the page may wish to set up links that refer to a specific section there.

Some attributes have pitfalls or even flaws. For example, accesskey, originally meant for accessibility, tends to work against it, due to wrong design and implementations. Using tabindex normally means that you should have designed the page differently. Using event handler attributes (onabort etc.) is OK in simple cases, but it is mostly better to assign event handlers to elements in JavaScript rather than in HTML markup. The title attribute is usually used for wrong purposes; it is best avoided, since its most common effect, the tooltip effect, has lousy usability and can be replaced by “CSS tooltips” (using CSS and JavaScript, or maybe CSS alone).

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.