You can use vertical-padding
to set a ratio to a box. It helps to draw a square or any rectangle that will be able to resize and preserve ratio in the mean time.
basicly with a pseudo element (floatting or inline-block) to draw a square
:before {
content:'';
padding-top:100%;/* equals width of tag */
float:left; /* or inline-block; */
}
Show & Run snippet below to see a square, a 16/9 and a 4/3 boxe.
.ratio {
display:inline-block;
width:25%;
vertical-align:middle;
background:lightgray;
}
body {
text-align:center;
}
.ratio:before {
content:'';
display:inline-block;
padding-top:100%;
vertical-align:middle;
}
.b:before {
padding-top:56.2%;
}
.c:before {
padding-top:75%;
}
<div class="ratio "> square</div>
<div class="ratio b"> 16/9 </div>
<div class="ratio c"> 4/3 </div>
adapt vertical padding for the ratio you look for.
i explained it here once How to make a div's height proportional to its width?
you may use either flex or table to draw to center X,Y your square or rectangle
flex: demo based on this template using css below :
html {
height:100%;
display:flex;
}
body {
margin:auto;
}
table: example
html {
display:table;
height:100%;
width:100%;
}
body {
display:table;
vertical-align:middle;/* will set vertically content in center */
}
#wrapper {
width:xx; /* whatever you need */
margin:auto; /* horizontal centering*/
}