Sometimes I find it difficult to choose the right CSS selector. Consider this, for example:
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Which CSS selector</title>
<style>
[for="married"] {
outline: 1px solid red;
}
</style>
</head>
<body>
<form>
<label for="name">Name</label>
<input type="text" id="name">
<label for="married">Married</label>
<input type="checkbox" id="married">
</form>
</body>
</html>
I can get the same result with:
- label:nth-child(3)
- label:nth-of-type(2)
- #name + label
- etc.
I can even give the element an ID and get it directly. But isn't it better to keep your HTML tidier/smaller?
Bookmarks