Flexbox gets new behavior for absolute-positioned children

Published on Updated on

A previous version of the CSS Flexible Box Layout specification set the static position of absolute-positioned children as though they were a flex item whose size is 0px by 0px. The latest version of the spec takes them fully out of flow and sets the static position based on align and justify properties. At the time of this writing, Edge and Opera 39 for desktop and Android already support this.

For an example, let's apply some positioning behaviors to the following HTML.

<div class="container">
<div>
<p>In Chrome 52 and later, the green box should be centered vertically and horizontally in the red box.</p>
</div>
</div>

We'll add something like this:

.container {  
display: flex;
align-items: center;
justify-content: center;
}
.container > * {
position: absolute;
}

In Chrome 52 or later, the nested <div> will be perfectly centered in the container <div>.

Chrome52 behavior.

In non-conforming browsers, the top left corner of the green box will be in the top center of the red box.

Legaci behavior.

If you want to try this for yourself in Chrome or any other browser, download our sample or visit the live demo.

Updated on Improve article

Back

Goodbye short sessions - A proposal for using service workers to improve cookie management on the web

Next

Service worker caching, PlaybackRate and Blob URLs for audio and video on Chrome for Android

This site uses cookies to deliver and enhance the quality of its services and to analyze traffic. If you agree, cookies are also used to serve advertising and to personalize the content and advertisements that you see. Learn more about our use of cookies.