7

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>
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
AlexStack
  • 16,766
  • 21
  • 72
  • 104
  • related: [Maintain aspect ratio of div but fill screen width and height in CSS?](http://stackoverflow.com/q/20590239/703717) – Danield Sep 15 '14 at 08:24

1 Answers1

13

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).

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
  • For those not interested in [CSS Flexbox Layout](http://www.w3.org/TR/css3-flexbox/), here is **[an alternative](http://jsfiddle.net/hashem/fp0n5nLe/)** which is supposed to work on IE9 as well. – Hashem Qolami Sep 15 '14 at 07:53