Utilizing HTML attributes can achieve this:
class
, id
, data-nameOFData
for any HTML element
.class {
color: blue
}
#id {
color: green
}
div[data-test] {
color: yellow
}
<div class="class">class</div>
<div id="id">id</div>
<div data-test>data</div>
name
, type
and for
for input
elements
label[for="textInput"] {
color: aqua
}
input[type="text"] {
color: brown
}
<label for="textInput">label</label>
<br>
<input type="text" name="textInput" value="name" />
href
for anchors tags and src
for images
a {
padding: 10px;
background-color: deepskyblue
}
a[href*="google"] {
background-color: yellow
}
<a href="http://www.youtube.com">youtube</a>
<a href="http://www.google.com">google</a>
Additionally, selecting an element based on its index can be done using CSS pseudo selectors like :first-child
, :nth-child(n)
, :last-child
, :first-of-type
, :nth-of-type(n)
, :last-of-type
, :nth-of-type(even)
, :nth-child(odd)
MDN , w3Schools.
div {
width: 100px;
height: 50px;
}
div:nth-of-type(even) {
background-color: cornflowerblue
}
div:nth-child(3) {
background-color: coral
}
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>