0

When I try to create table and try to add color, I suffered some problem.

in css selector I defined id,but selector #1 didn't work well.

Why this selector didn't work?

And How to fix it?

Thanks

td {
  transition-duration: 0.5s;
  border: solid black 1px;
  padding: 5px;
}

table {
  border-collapse: collapse;
}

#1{
background-color:aqua;}

#2{
background-color:aqua;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <td id=1>1</td>
    <td id=2>2</td>
    <td id=3>3</td>
  </tr>
  <tr>
    <td id=4>4</td>
    <td id=5>5</td>
    <td id=6>6</td>
  </tr>
  <tr>
    <td id=7>7</td>
    <td id=8>8</td>
    <td id=9>9</td>
  </tr>
</table>
Heisenberg
  • 4,787
  • 9
  • 47
  • 76

3 Answers3

2

IDs can't start with numbers. A good workaround is to add a meaningful name before the number. Also, as ellitt said, the id in the html should be in quotes.

For example:

HTML:

<text id="alert1">Some text</text>
<text id="alert2">Some text</text>

CSS:

#alert1 {
    background-color: blue;
}

#alert2 {
    background-color: green;
}
mega12345mega
  • 503
  • 1
  • 6
  • 17
0

Your id cannot be a number. try putting a string as a selector and then try.

It should work.

Krish
  • 47
  • 3
0

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. So, remove the numbers from id and replace them with string.

<td id="name1">Your Code</td>
<td id="name2">Your Code</td>

#name1 {
   background-color:aqua;
}

#name2 {
  background-color:aqua;
}
Milinda
  • 101
  • 2
  • 12