Skip to content

C060 Media Queries

In this task, you will experiment with media queries. Using the following HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="style.css" />
    <title>Document</title>
  </head>
  <body>
    <div class="center">Lorem ipsum</div>
  </body>
</html>

And the following CSS:

div {
  width: 400px;
  height: 400px;
  font-size: 2em;
  background-color: pink;
}

.center {
  line-height: 400px;
  height: 400px;
  border: 3px solid green;
  text-align: center;
}

Your task is to ensure the square changes color based on the browser size:

  • Up to 600px = pink
  • Minimum 600px = red
  • Minimum 800px = blue
  • Minimum 1000px = yellow

You can use this as a template:

@media only screen and (min-width: [xxx]px) {
  div {
    background-color: [color];
  }
}
See a possible solution
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="style.css" />
    <title>Document</title>
</head>
<body>
    <div class="center">Lorem ipsum</div>
</body>
</html>
div {
    width: 400px;
    height: 400px;
    font-size: 2em;
    background-color: pink;
}

.center {
    line-height: 400px;
    height: 400px;
    border: 3px solid green;
    text-align: center;
}

@media only screen and (min-width: 600px) {
    div {
        background-color: red;
    }
}

@media only screen and (min-width: 800px) {
    div {
        background-color: blue;
    }
}

@media only screen and (min-width: 1000px) {
    div {
        background-color: yellow;
    }
}