Reputation: 27
I want to change the background color of the div tag with div element with class of ind and id of three to blue. here is my css
h1 {
text-align: center;
}
#identify {
text-align: center;
}
.container {
border-style: dotted;
width: 800px;
height: 400px;
position: absolute;
right: 550px;
}
.ind {
float: left;
position: relative;
top: 40%;
padding: 50px;
left: 200px;
}
.ident {
position: relative;
display: inline;
float: left;
text-align: center;
padding: 10px;
}
#2.ind {
background-color: blue;
}
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="project" content="hello">
<title></title>
</head>
<body>
<link rel="stylesheet" type="text/css" href="2css.css">
<h1>Set the distance!</h1>
<p id="identify">To play this game, you must be at least 18 years old. You must also fill out some information.</p>
<div class="container">
<div class="ind" id="2">2</div>
<div class="ind" id="3">3</div>
<div class="ind" id="4">4</div>
</div>
</body>
</html>
In the browser for some reason the background color of div class ind with id of 2 won't change to blue. Any suggestions?
Upvotes: 0
Views: 2761
Reputation: 1157
To give CSS effect to div element id=3, you can do it as below.
.container > .ind[id="3"]
{
background-color:lightblue;
}
<div class="container">
<div class="ind" id="2">2</div>
<div class="ind" id="3">3</div>
<div class="ind" id="4">4</div>
</div>
Upvotes: 0
Reputation: 12114
Despite the many claims otherwise, IDs in HTML5 can indeed start with a number. Care must be taken when referring to these elements in other contexts, however. First some code:
console.log(document.querySelector('#\\0032') == null);
#\0032 { background-color: black; color: white }
[id="2"] { background-color: white; color: black }
<p id="2">
Two
</p>
You do need to escape the number, using an Unicode escape sequence (I've expanded it to its four digit hexidecimal representation so that's clear). You can use an attribute selector, but that changes its specificity (see this question's answers for more information on that topic) to be lower than IDs. You can see that the second selector is not specific enough to override the ID selector. Not necessarily a bad thing, but certainly something to be aware of.
Note that despite it working, and being allowed by the relevant specifications, it is generally frowned upon because of the need for escaping in CSS. Note that also means that in something like the DOM method querySelector
, when accessed in JavaScript, the escape character must also be escaped (since you must use a JavaScript string, which uses the same escape character). I've added an example of that to the snippet as well.
Upvotes: 2
Reputation: 895
if you want to access it in css, an id cannot start with a no., thus #2 wont work.
If you want to still use id="2" try:
#\32 {
background-color: blue;
}
Upvotes: 0
Reputation: 165
CSS IDs cannot contain only number.
Two solutions:
1- change your ID in alphanumeric, i.e. id="div2"
2- use data attribute as data-id="2" and then in CSS use
.ind[data-id="2"]{background-color:blue;}
Upvotes: 1