Introducing CSS Regions

Sara Vieira By Sara Vieira  |  Sep. 30, 2013

Building complex, flexible layouts has never been easy, but CSS3 has certainly taken away a lot of the headaches.

CSS3 features like Flexbox and Columns have made some complex layouts a real possibility and now they’re being joined by one of the newest additions to the web designer’s toolbox: CSS Regions.

CSS Regions allow you to flow content into a series of containers on a single page. It’s the equivalent of linking text boxes in an application like InDesign. It makes possible some layouts that were previously only possible by blurring the line between data and styles.


Browser support

As always, browser support is far from perfect.

CSS Regions is still a draft, which means it’s experimental. Initially the only browser to actually support it was Internet Explorer 10 (no, I’m not kidding); although IE10 does use an iframe as the content source.

Safari claims to offer support with a prefix.

In Chrome you can test the feature by opening your browser, typing ‘about:flags’ and activating ‘enable-experimental-webkit-features’, then restarting your browser.

Happily there is also a JavaScript polyfill created by Adobe that brings the functionality of CSS Regions to browsers that currently don’t support it, you can find this on github.


Using CSS Regions

Since the purpose of CSS Regions is to allow our text to flow across different containers, the first thing you need is some text:

<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc cursus imperdiet purus ac eleifend. Vivamus posuere pellentesque nibh vel laoreet. Donec et sem odio. Donec sit amet lorem hendrerit, faucibus libero et, mattis lacus. Suspendisse dapibus rutrum felis quis interdum. Integer tincidunt, orci at condimentum placerat, est nulla sollicitudin velit, vitae gravida nisi odio ac ligula. Sed hendrerit ac massa vel ultricies. Vestibulum commodo, orci et tincidunt laoreet, enim tellus aliquet orci, quis dapibus sapien tellus eu felis. Etiam non arcu at eros luctus consectetur vitae eget nunc. In felis ipsum, vehicula ac mauris vel, porttitor gravida neque.
Quisque orci turpis, aliquam vel tortor convallis, ullamcorper molestie nisl. Sed aliquet dignissim lorem non fringilla. In hac habitasse platea dictumst. In hac habitasse platea dictumst. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi congue erat ac hendrerit ullamcorper. Morbi facilisis urna nunc, eget pretium lectus congue vitae.

And then some containers:

<div class="containers"></div>
<div class="containers"></div>
<div class="containers"></div>

Now we have the basic content set up, we can link the containers using flow-into and flow-from, you’ll notice in the example I’ve added the -webkit- prefix.

.text {
   -webkit-flow-into: text-flow;
   padding: 0;
   margin: 0;
   color: #F2F2F2;
   font-family: helvetica, arial;
   font-size: 16px;
   line-height: 22px;
   -webkit-flow-from: text-flow;
   background: #333333;
   padding: 5px;
   margin-top: 100px;
   width: 200px;
   height: 300px;
   float: left;
   margin-right: 20px;

If you check this in a browser you’ll see that the text will just flow from one box to another. If you now position those containers, the text will continue to flow properly.


Understanding flow-into

-webkit-flow-into: text-flow;

This property accepts an identifier as the value. When you pass a value to this property it becomes part of a named flow and it will stop rendering as part of the page flow. You can cancel that by setting the property to none.

The name of the flow you use is arbitrary, just make sure you’re consistent. We can also have multiple elements with the same named flow as in our example above.

This isn’t limited to text, we can also flow images, lists, and many other kinds of HTML content.


Understanding flow-from

-webkit-flow-from: text-flow;

We use this property to specify which element(s) should receive the specified flow.

The value is the name of the flow that we specified for the flow-into property.

Bear in mind that any content styles that you apply to the original text will be maintained across the flow. So if you color the text blue, it will remain blue across all containers.


Final thoughts

To see an example of CSS Regions in action, take a look at this pen I created.

The browser support for CSS Regions is currently weak, and there’s a long way to go before we can rely on it on a daily basis. But, the flexibility that it offers is fantastic, and once full support is in place I think CSS Regions will be the go-to technique for years to come.


Are you excited by CSS Regions? How soon do you think we’ll be able to use them? Let us know in the comments.

Featured image/thumbnail, waterfall image via cuatrok77.