Batuhan Keten
Batuhan Keten

Reputation: 17

Taking an element with css without using any attirbute

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Test 2</title>
    <link href="./test2.css" rel="stylesheet" />
</head>
<body>
<article>First</article>
<article>Second</article>
<article>Third</article>
<article>Fourth</article>
</body>
</html>

I need to separate articles in only css file. Without adding any attirbutes or something. Without touching to this HTML file.

article[value = 'First'] {
    color: red;
}

article:text('First'){
    color: red
}

article[text$="Second"]{
    color: red
}

I tried them and they are not working.

Upvotes: 1

Views: 37

Answers (1)

Anurag Srivastava
Anurag Srivastava

Reputation: 14413

Use the :nth-of-type selector.

article:nth-of-type(2) {
  background: red;
}
article:nth-of-type(5) {
  color: blue;
  font-weight: bold;
}
<article>First</article>
<article>Second</article>
<article>Third</article>
<article>Fourth</article>
<article>Fifth</article>
<article>Sixth</article>

Upvotes: 2

Related Questions