Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive design: centralize a full-screen square in any screen size

The problem is simple:

  • The main part of my page is a square that should be shown at the center of the screen in all screen sizes and orientations
  • The square should use the screen efficiently and be as big as possible without the need to scroll
  • No scrollbars, no fixed sizing, no overflow-hidden, no Javascript.
  • Flexbox is encouraged.

This is how the page should look like in a landscape or portrait browser:

The square should be at the center in all conditions

Here is a CodePen as a starting point.

<div class="body">
  <div class="square">
    Square
  </div>
</div>
like image 696
AlexStack Avatar asked Sep 15 '14 06:09

AlexStack


1 Answers

Here's my attempt to achieve the end goal.

The key point is to use vmin viewport percentage length for both width and height properties of the square box:

Example Here

.body, .square {
  display: flex;
  align-items: center;
  justify-content: center;
}

.body {
  min-height: 100vh;
}

.square {
  width: 100vmin;
  height: 100vmin;
}

(vendor prefixes omitted for brevity, check the "Compiled View" in the demo).

like image 189
Hashem Qolami Avatar answered Oct 01 '22 01:10

Hashem Qolami