Classes and IDs make referencing HTML elements from scripts and stylesheets easier. The class attribute can be used on one or more tags and is used by CSS for styling. IDs however are intended to refer to a single element, meaning the same ID should never be used twice. IDs are generally used with JavaScript and internal document links, and are discouraged in CSS. This topic contains helpful explanations and examples regarding proper usage of class and ID attributes in HTML.
Parameter | Details |
---|---|
class | Indicates the Class of the element (non-unique) |
id | Indicates the ID of the element (unique in the same context) |
class
and id
are global attributes, and may therefore be assigned to any HTML element.HTML5
, the class and id attributes can be used on any element. In HTML 4.0.1, they were off-limits to the <base>
, <head>
, <html>
, <meta>
, <param>
, <script>
, <style>
and <title>
tags.