Home > Enterprise >  Second <div> tag failing to show up with color
Second <div> tag failing to show up with color

Time:02-11

I'm trying to create an animation with 2 div tags, but the child is not appearing in the parent. How do I get the child to show up with background color?

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Rotate</title>
  </head>
  <body>
    <style>
      .parent {
        width: 400px;
        height: 400px;
        background-color: hsla(200,100%,20%);
      };

      .child {
        width: 50%;
        height: 50%;
        background-color: red;
        z-index: 2;
      };
    </style>
    <div class = "parent">
      <div class = "child"></div>
    </div>
  </body>
</html>```

CodePudding user response:

The main culprit is the use of a semicolon after the .parent css class declaration. That semicolon causes subsequent declarations (.child in this case) to be ignored:

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Rotate</title>
  </head>
  <body>
    <style>
      .parent {
        width: 400px;
        height: 400px;
        background-color: hsla(200,100%,20%);
      }     /* <------ Removed semicolon */

      .child {
        width: 50%;
        height: 50%;
        background-color: red;
      }
    </style>
    <div class = "parent">
      <div class = "child"></div>
    </div>
  </body>
</html>

I also removed z-index: 2; from .child since it is not needed in this example.

  • Related