How to always center a flexible square in viewport with pure CSS? -
i know question: height equal dynamic width (css fluid layout)
but want more!! want flexible container has aspect ratio of square max-height , max-width of 100% of page (the window element) , on top of vertically , horizontally centered.
like this:
// container matches height of window // container-height = 100%; container-width = absolute-container-height -page/browser-window- - ####### - - #cont-# - - #ainer# - - ####### - --------------------- // container matches width of window // container-width = 100%; container-height = absolute-container-width --page--- - - - - -#######- -#######- -#######- -#######- - - - - ---------
is possible achieve pure css (and better cross-browser)?
edit: know there calc() css3, due poor mobile browser-support, don't want use it.
edit2: seems like, didn't make myself clear enough. need height , width of wrapper match height or width of window, depending on smaller.the square-container should never exceed smaller value of window-height/width.
this is, how done jquery:
// container/#main-wrapper has top: 50%, left: 50%, position: absolute via css $(window).resize(function () { var $ww = $(window).width(); var $wh = $(window).height(); if ($ww > $wh) { $('#main-wrapper').css({ height: $wh, width: $wh, margintop : ($wh / 2) * -1, marginleft : ($wh / 2) * -1 }); } else { $('#main-wrapper').css({ height: $ww, width: $ww, margintop : ($ww / 2) * -1, marginleft : ($ww / 2) * -1 }); } });
i figured out. magic ingredients view-port units.
given html structure:
.l-table .l-table-cell .square
you can use css (well actuall scss, idea) make work
html, body{ height: 100% } l-table{ background: orange; display: table; height: 100%; width: 100%; } .l-table-cell{ display: table-cell; vertical-align: middle; border: 2px solid black; } .square{ background: red; margin: auto; @media (orientation:landscape) { width: 70vh; height: 70vh; } @media screen , (orientation:portrait) { width: 70vw; height: 70vw; } }
http://codepen.io/johannesjo/pen/rvfxj
for need it, there polyfill.
Comments
Post a Comment