CSS Layout How to - Create Flexible any column layout








Question

We would like to know how to create Flexible any column layout.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
html, body {<!--from   w  ww.j a va2 s. c  om-->
  padding: 0;
  margin: 0;
}

#footer, #header {
  background: #444;
  color: #fff;
  clear: both;
}

#container {
  overflow: hidden;
  width: 100%;
  position: relative;
}

#contentwrap {
  float: left;
  width: 100%;
  margin-right: -100%;
}

#content {
  margin-left: 270px;
  padding: 20px;
  border-left: 1px solid #444;
}

#sidebar {
  float: left;
  width: 230px;
  padding: 20px;
  background: #dad;
  border-right: 1px solid #444;
}
</style>
</head>
<body>
  <div id="header">header</div>
  <div id="container">
    <div id="contentwrap">
      <div id="content">
        <h1>Content remaining width</h1>
        <p>
          <strong>Pellentesque habitant morbi tristique</strong> senectus et
          netus et malesuada fames ac turpis egestas. Vestibulum tortor quam,
          feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu
          libero sit amet quam egestas semper. <em>Aenean ultricies mi
            vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et
          sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed,
          <code>commodo vitae</code>
          , ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt
          condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac
          dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis.
          Ut felis.
        </p>
        <h2>Header Level 2</h2>
      </div>
    </div>
    <div id="sidebar">
      <p>short sidebar</p>
      <p>add more content here until this gets longer than main and the
        footer will still stay below</p>
    </div>
  </div>
  <div id="footer">footer</div>
</body>
</html>

The code above is rendered as follows: