Winter Bootcamp in ML and IoT in Jaipur
 Course content (For Bootcamp & Winter Training):- Machine Learning (ML) || Internet of Things (IoT) || Register for winter bootcamp
0 like 0 dislike
in Tutorial & Interview questions by (18.8k points)

1 Answer

0 like 0 dislike
by (18.8k points)
Best answer

Parameter                                      Details

class                                Indicates the Class of the element (non-unique)

id                                      Indicates the ID of the element (unique in the same context)

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.

Giving an element a class

Classes are identifiers for the elements that they are assigned to. Use the class attribute to assign a class to an element.

<div class="example-class"></div>

To assign multiple classes to an element, separate the class names with spaces.

<div class="class1 class2"></div>

Using classes in CSS

Classes can be used for styling certain elements without changing all elements of that kind. For example, these two span elements can have completely different stylings:


<span class="special"></span>

Classes of the same name can be given to any number of elements on a page and they will all receive the styling associated with that class. This will always be true unless you specify the element within the CSS.

For example, we have two elements, both with the class highlight:

<div class="highlight">Lorem ipsum</div>

<span class="highlight">Lorem ipsum</span>

If our CSS is as below, then the color green will be applied to the text within both elements:

.highlight { color: green; }

However, if we only want to target div's with the class highlight then we can add specificity like below:

div.highlight { color: green; }

Nevertheless, when styling with CSS, it is generally recommended that only classes (e.g. .highlight) be used rather than elements with classes (e.g. div.highlight).

As with any other selector, classes can can be nested:

.main .highlight { color: red; } /* Descendant combinator */

.footer > .highlight { color: blue; } /* Child combinator */

You can also chain the class selector to only select elements that have a combination of several classes. For

example, if this is our HTML:

<div class="special left menu">This text will be pink</div>

And we want to colour this specific piece of text pink, we can do the following in our CSS: { color: pink; }

Giving an element an ID

The ID attribute of an element is an identifier which must be unique in the whole document. Its purpose is to uniquely identify the element when linking (using an anchor), scripting, or styling (with CSS).

<div id="example-id"></div>

You should not have two elements with the same ID in the same document, even if the attributes are attached to two different kinds of elements. For example, the following code is incorrect:

<div id="example-id"></div>

<span id="example-id"></span>

Browsers will do their best to render this code, but unexpected behavior may occur when styling with CSS or adding functionality with JavaScript.

To reference elements by their ID in CSS, prefix the ID with #.

#example-id { color: green; }

To jump to an element with an ID on a given page, append # with the element name in the URL.

This feature is supported in most browsers and does not require additional JavaScript or CSS to work.

Acceptable Values

For an ID

Version ≥ 5

The only restrictions on the value of an id are:

  1. it must be unique in the document
  2.  it must not contain any space characters
  3.  it must contain at least one character

So the value can be all digits, just one digit, just punctuation characters, include special characters, whatever. Just no whitespace.

So these are valid:

<div id="container"> ... </div>

<div id="999"> ... </div>

<div id="#%LV-||"> ... </div>

<div id="____V"> ... </div>

<div id="⌘⌥"> ... </div>

<div id="♥"> ... </div>

<div id="{}"> ... </div>

<div id="©"> ... </div>

<div id="♤₩¤☆€~¥"> ... </div>

This is invalid:

<div id=" "> ... </div>

This is also invalid, when included in the same document:

<div id="results"> ... </div>

<div id="results"> ... </div>

Version ≤ 4.01

An id value must begin with a letter, which can then be followed only by:

letters (A-Z/a-z)

digits (0-9)

hyphens ("-")

underscores ("_")

colons (":")

periods (".")

Referring to the first group of examples in the HTML5 section above, only one is valid:

<div id="container"> ... </div>

These are also valid:

<div id="sampletext"> ... </div>

<div id="sample-text"> ... </div>

<div id="sample_text"> ... </div>

<div id="sample:text"> ... </div>

<div id="sample.text"> ... </div>

Again, if it doesn't start with a letter (uppercase or lowercase), it's not valid.

Winter 10 Days bootcamp classes(7 HRS Daily) will start from 5 & 20 December 2019 in:
1) Internet of things(IoT) Using RASPBERRY-PI
2) Machine Learning (ML)

70% OFF| Fee-INR 3,000/-

Limited seats!! Hurry up!!

[[ CALL - 07976731765 ]]

Some Study Resources are compiled from original Stack Overflow Documentation, the content is developed by the different experts at Stack Overflow. Study resources are released under Creative Commons BY-SA. Images may be copyright of their respective owners. This website is for self-learning and not affiliated with Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners. Please send feedback and corrections to

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer