flex-based page template

Flex is an added CSS property that allows you to receive pages that are automatically adapted to different screens.

This is the value of the display attribute. Therefore, CSS simply added an additional value to the existing property:

After you select flex, you can add other properties to the container.

Different properties are also added for contained elements. In particular, flex-shrink, which indicates whether an element can be reduced or not to free up space. flex-shrink: 0 means it cannot be shrunk.

Therefore, we want to use these new properties to make the web page template adaptable and use a minimum of CSS code.

To do this, you need to split the page into several sections:

  1. Fullpage is the main container, its two elements are aligned with columns .
  2. The first element, the hand, and the second, the footer, are separated by variable space depending on the content of the page. Footer, the footer is always at the bottom of the window, even if the page is almost empty.
  3. Main contains two elements: Header and middle.
  4. Header at set height, middle variable height.
  5. The middle contains two elements (default, therefore not specified). They are separated by variable space depending on the content.
  6. The first element, content, has a variable width and is placed on the left. The second, sidebar, on the right has a fixed width.

This leads to the following scheme:

Modèle de page flex

CSS code is greatly simplified:

<style>
html, body: {
  margin:0;
  padding:0;
}

#fullpage {
  display:flex;
  flex-direction:column;
  justify-content:space-between;
  margin:0;
  position:absolute;
  top:0;
  bottom:0;
  left:0;
  right:0;
}

#middle {
  display:flex;
  justify-content:space-between;
}

#header {
  height:100px;
}

#content {
  padding:32px;
}

#sidebar {
  width:200px;
}

#footer {
  min-height:64px;	
  display:flex;
  flex-direction:column;
  justify-content:center;
}
</style>

Removed from the demo code what is not used at the disposal of the elements. The full code is on the demo page.

If you want to limit the maximum page width, you can add these two lines to the # fullpage property:

max-width:1280px;
margin:auto;

Try a demo

It can be downloaded and used as a starting point for a site or application.

The flex value for the display is supported by all recent browsers. If you want to maximize your audience, you should stay in classic CSS, here is the page template for all old browsers.