When to use type and when to use class attribute in HTML?

Dear community!
The difference between class and type attributes is not entirely clear to me, when should I use them? Or can I use them fairly interchangeable? Would it be correct to think that type can be used only with specific elements such as link or embed and class can be applied to any element?

Thanks in advance!

Hi, there!

This is correct, type can only be used with certain elements. These elements include <button>, <input>, <source>, <a>, etc. Type is used to declare the type of content that will be contained within the element. You can find more information that here

Class, on the other hand, can be used to group common elements—typically for CSS or JavaScript purposes. So, if you had multiple elements that need to be styled or manipulated in the same way, you can give them all the same class to do so.

1 Like

To better understand ‘type’ it may be best to take a trip down memory lane and review MIME. Give search a try on that one term, and report back to us what you learn and any questions that arise.

There are multiple implementations of the type attribute in their different element settings. In other words we won’t find a blanket answer to any one question within its regard.

An input element has defined types, which we denote with that attribute. Likewise a requested resource would be expected to fall within a specified type, which would also be denoted by that attribute. The bottom line would be to fill in your knowledge relating to ALL the usage cases and intentions, and know where the necessary lookups would be for each case.