+ 1
Dives,classes, id
what are divs classes and id are used to in html I can't understand its uses please explain it if possible with example thx
1 Resposta
+ 9
class is used to identify a group of elements
id is used to identify a single element (unique)
the uses vary from applying CSS styles to fetching the elements by their class/id
example to applying CSS style to group of divs
<div class="group"></div>
<div class="group"></div>
<div class="group"></div>
in the CSS file:
.group{
border: 1px solid green;
}
this will give green border to any element in class group
example with id:
<div id="div1"></div>
<div id="div2"></div>
in the CSS file:
#div1{
border: 1px solid red;
}
#div2{
border: 1px solid blue;
}
this will give div with id div1 a red border and a blue border to the div with the id div2
as for getting elements with JS
document.getElementsByClassName("group")[0];
this will get the first element from the class of "group"
document.getElementById("div2");
this will get the element with id of "div2"