CSS Layout How to - Center an element whose width is beyond the viewport








Question

We would like to know how to center an element whose width is beyond the viewport.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
body {<!-- ww  w  .ja  v a 2  s  . com-->
  width: 400px;
  border: 1px solid black;
  margin: 0 auto;
}

#foo {
  width: 600px;
  height: 100px;
  border: 1px solid green;
  margin-left: -100px;
  margin-right: -100px;
}
</style>
</head>
<body>
  <div id="foo"></div>
</body>
</html>

The code above is rendered as follows: