Frontend Development

HELP! DIFFERENCE BETWEEN DIV CLASS AND DIV ID

Submitted by Avocat, , Thread ID: 61633

Thread Closed

RE: HELP! DIFFERENCE BETWEEN DIV CLASS AND DIV ID

#13
You need to remember, that you can have multiple classes per element and sigle class can be re-used as much as you can, but IDs are unique per page.

Both, classes and IDs have multiple usses.
First of all you can use both for styling purposes.
in css for id you simply style it as:
#yourId {
color: red;
}

for classes you use:

.yourClass {
color: red;
}

both of them will change color of the text in the elements with corresponding classes and ids to red.

IDs also hase a single extremey useful native function. They are used for navigation.

If you have an element on your site with an id for example id="yourPageElement" you can add #yourPageElement in the end of the URL of the page and it will automatically scroll the element into view.
This is usefull for example for a button that will scroll you to the top of the page.
This can be achieved by a link like this: a href="#yourPageElement"

Users browsing this thread: 4 Guest(s)