Search code examples
htmlcssstylesinternals

Internal style does not work in this HTML code?


<html>
<head>
<style>
#1fineday {
    color:yellow;
}
</style>
</head>
<body>
<h1>Welcome</h1>
<div class="intro">
  <p id="1fineday">Hello World</p> 
</div>
</body>
</html>

Shouldn't "Hello World" be yellow? I don't understand why internal CSS styling I wrote doesn't apply. The ID is 1fineday. I must be overlooking something.


Solution

  • Because of naming convention, your id cannot start with a number

    <html>
    
    <head>
      <style>
        #fineday {
          color: yellow;
        }
      </style>
    </head>
    
    <body>
      <h1>Welcome</h1>
      <div class="intro">
        <p id="fineday">Hello World</p>
      </div>
    </body>
    
    </html>

    Remove the number 1 and it will work

    For reference:

    W3Schools: https://www.w3schools.com/css/css_syntax.asp

    CSS-Tricks: https://css-tricks.com/ids-cannot-start-with-a-number/