CSS Layout How to - React to media width








Question

We would like to know how to react to media width.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#yourPageContainer {<!--  ww  w.  j a  va 2 s  .com-->
  width: 1000px;
  border: 1px solid #f00;
}

#otherElements {
  background-color: #afa;
  width: 200px;
  height: 100px
}

@media screen and (max-width:500px) {
  #yourPageContainer {
    width: 500px;
  }
  #otherElements {
    background-color: #aaf;
    width: 100px;
  }
}

@media screen and (max-width:300px) {
  #yourPageContainer {
    width: 300px;
  }
  #otherElements {
    background-color: #faa;
    width: 50px;
  }
}
</style>
</head>
<body>
  <div id="yourPageContainer">
    <div id="otherElements">Other elements</div>
  </div>
</body>
</html>

The code above is rendered as follows: