CSS Layout How to - Shrink Or Expand Margins With Window Resize








Question

We would like to know how to shrink Or Expand Margins With Window Resize.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#container {<!--from  w  ww  .  ja v a 2  s.  c  o  m-->
  width: 90%;
  margin: 16px auto;
  padding: 16px;
  position: relative;
  border: solid 1px #333;
  background-color: #eee;
  min-width: 280px;
}

#redBox {
  width: 128px;
  height: 128px;
  display: inline-block;
  margin-right: 32%;
  background-color: red;
}

#greenBox {
  width: 128px;
  height: 128px;
  position: absolute;
  top: 16px;
  right: 16px;
  background-color: green;
}
</style>
</head>
<body>
  <div id="container">
    <div id="redBox"></div>
    <div id="greenBox"></div>
  </div>
</body>
</html>

The code above is rendered as follows: