The A List Apart Blog Presents:

Ten CSS One-Liners to Replace Native Apps

Article Continues Below

Håkon Wium Lie is the father of CSS, the CTO of Opera, and a pioneer advocate for web standards. Earlier this year, we published his blog post, “CSS Regions Considered Harmful.” When Håkon speaks, whether we always agree or not, we listen. Today, Håkon introduces CSS Figures and argues their case.

Tablets and mobile devices require us to rethink web design. Moused scrollbars will be replaced by paged gestures, and figures will float in multi-column layouts. Can this be expressed in CSS?

Paged designs, floating figures, and multi-column layout are widely used on mobile devices today. For some examples, see Flipboard, the Our Choice ebook, or Facebook Paper. These are all native apps. If we want the web to win on these devices (we do), it’s vital that designers can build these kinds of presentations using web standards. If web standards cannot express this, authors will be justified in making native apps.

Over the past years, I’ve been editing two specifications that, when combined, provide this kind of functionality: CSS Multi-column Layout and CSS Figures. I believe they are important to make sure the web remains a compelling environment for content providers.

In this article, I will demonstrate how simple it is to write CSS code with these specs. I will do so through 10 one-liners. Real stylesheets will be slightly longer, but still compact, readable, and reusable. Here are some screenshots to give you a visual indication of what we are aiming for:

Three views of a web page demonstrating different numbers of columns for different window sizes

Building a page

The starting point for my code examples is an article with a title, text, and some images. In a traditional browser, the article will be shown in one column, with a scrollbar on the right. Using CSS Multi-column Layout, you can give the article two columns instead of one:

  article { columns: 2 }

That’s a powerful one-liner, but we can do better; we can make the number of columns depend on the available space, so that a narrow screen will have one column, a wider screen will have two columns, etc. This is all it takes to specify that the optimal line length is 15em and for the number of columns to be calculated accordingly:

  article { columns: 15em }

To me, this is where CSS code morphs into poetry: one succinct line of code scales from the narrowest phone to the widest TV, from the small print to text for the visually impaired. There is no JavaScript, media queries, or expensive authoring tool involved. There is simply one highly responsive line of code. That line is used to great effect to produce the screenshots above. And it works in current browsers (which is not yet the case for the following examples).

The screenshots above show paged presentations, as opposed to scrolled presentations. This is easily expressed with:

  article { overflow: paged-x }

The above code says that the article should be laid out as pages, stacked along the x-axis (which, in English, is toward the right). Browsers that support this feature must provide an interface for navigating in these pages. For example, the user may reach the next page by making a swiping gesture or tilting the device. A visual indication of which page you are reading may also be provided, just like scrollbars provide a visual indication in scrolled environments. On a tablet or mobile phone, swiping to the next page or document will be easier than scrolling.

Images

Adding images to the article creates some challenges. Lines of text can easily be poured into several columns, but if figures are interspersed with text, the result will be uneven; because images are unbreakable, they will cause unused whitespace if they appear at a column break. To avoid this, traditional paper-based layouts place images at the top or bottom of columns, thereby allowing other text to fill the whitespace. This can naturally be expressed in CSS by adding top and bottom to the float property. For example:

  img { float: bottom }

The bluish harbor images in the screenshots above have been floated to the bottom of the page with this one-liner. CSS is used to express something that HTML cannot say; it is impossible to know how much textual content will fit on a screen in advance of formatting. Therefore, an author cannot know where to insert the image in the source code in order for it to appear at the bottom of the column. Being able to float figures to the top and bottom (in addition to the already existing left and right) is a natural extension to the float property.

Spanning columns

Another trick from traditional layout is for figures to span several columns. Consider this newspaper clipping:

A newspaper clipping showing text in four columns and images in the lower-left, lower-right and upper-right corners
Used with permission from the Bristol Observer

In the newspaper article, the image on the left spans two columns and is floated to the bottom of the columns. The code to achieve this in CSS is simple:

  figure { float: bottom; column-span: 2 }

HTML5’s figure element is perfect for holding both an image and the caption underneath it:

  <figure>
    <img src=cats.jpg>
    <figcaption>Isabel loves the fluffy felines</figcaption>
  </figure>

The newspaper article also has a figure that spans three columns, and is floated to the top right corner. In a previous version of the CSS Figures specification, this was achieved by setting float: top-corner. However, after discussions with implementers, it became clear that they were able to float content to more places than just corners. Therefore, CSS Figures introduces new properties to express that content should be deferred to a later column, page, or line.

Deferring figures

To represent that the cat picture in the newspaper clipping should be placed at the top of the last column, spanning three columns, this code can be used:

  figure { float: top; float-defer-column: last; column-span: 3 }

This code is slightly less intuitive (compared to the abandoned top-corner keyword), but it opens up a range of options. For example, you can float an element to the second column:

  .byline { float: top; float-defer-column: 1 }

The above code defers the byline, “By Collette Jackson”, by one. That is, if the byline would naturally appear in the first column, it will instead appear in the second column (as is the case in the newspaper clipping). For this to work with HTML code, it is necessary for the byline to appear early in the article. For example, like this:

<article>
  <h1>New rescue center pampers Persians</h1>
  <p class=byline>By Collette Jackson</p>
  ...
</article>

Deferring ads

Advertisements are another type of content which is best declared early in the source code and deferred for later presentation. Here’s some sample HTML code:

<article>
  <aside id=ad1 src=ad1.png>
  <aside id=ad2 src=ad2.png>
  <h1>New rescue center pampers Persians</h1>
</article>

And here is the corresponding CSS code, with a one-liner for each advertisement:

#ad1 { float-defer-page: 1 }
#ad2 { float-defer-page: 3 }

As a result of this code, the ads would appear on page two and four. Again, this is impossible to achieve by placing ads inside the text flow, because page breaks will appear in different places on different devices.

I think both readers and advertisers will like a more page-oriented web. In paper magazines, ads rarely bother anyone. Likewise, I think ads will be less intrusive in paged, rather than scrolled, media.

Deferring pull quotes

The final example of content that can be deferred is pull quotes. A pull quote is a quote lifted from the article, and presented in larger type at some predetermined place on the page. In this example, the pull quote is shown midway down the second column:

A picture of a pull quote in a print newspaper

Here’s the CSS code to express this in CSS:

  .pullquote#first { float-defer-line: 50% }

Other types of content can also be positioned by deferring lines. For example, a photograph may be put above the fold of a newspaper by deferring a number of lines. This will also work on the foldable screens of the future.

Pull quotes, however, are an interesting use case that deserve some discussion. A pull quote has two functions. First, it presents to the reader an interesting line of text to gain attention. Second, the presentation of the article becomes visually more varied when the body text is broken up by the larger type. Typically, you want one pull quote on every page. On paper, where you know how many pages an article will take up, it is easy to supply the right number of pull quotes. On the web, however, content will be reformatted for all sorts of screens; some readers will see many small pages, other will see fewer larger pages. To ensure that each page has a pull quote, authors must provide a generous supply of pull quotes. Rather than showing the extraneous quotes at the end of the article (which would be a web browser’s instinct), they should be discarded; the content will anyway appear in the main article. This can be achieved with a one-liner:

  .pullquote { float-policy: drop-tail }

In prose, the code reads: if the pull quote is at the tail-end of the article, it should not be displayed. The same one-liner would be used to extraneous images at the end of the article; authors will often want to have one image per page, but not more than one.

Exercises

The studious reader may want to consult the CSS Multi-column Layout and CSS Figures specifications. They have more use cases and more knobs to allow designers to describe the ideal presentation of figures on the web.

The easiest way to play with CSS Figures is to download Opera 12.16 and point it to this document, which generated the screenshots in Figure 1. Based on implementation experience, the specifications have changed and not all one-liners presented in this article will work. Also, Prince and AntennaHouse have partial support for CSS Figures—these are batch formatters that output PDF documents.

I’d love to hear from those who like the approach taken in this article, and those who don’t. Do you want this added to browsers? Let me know below, or request if from your favorite browser (Firefox, Chrome, Opera, IE). If you don’t like the features, how would you express the use cases that have been discussed?

Pages and columns have been basic building blocks in typography since the Romans started cutting scrolls into pages. This is not why browsers should support them. We should do so because they help us make better, more beautiful, user experiences on mobile devices.

64 Reader Comments

  1. Just being able to float elements vertically will be incredibly powerful. With the added control it is going to be a renaissance for good old float. 🙂

  2. @Håkon Wium Lie

    Thanks for the great article! I’ve been eagerly watching the development of flexbox, columns, figures and shapes to complement what I’ve already spent a lot of time on: using web fonts well (performance, proportion, OpenType) in the context of responsive design. What I think is important however is to not ignore the context of figures in relation to the content itself. While I agree some flexibility is great in fitting a figure to a place in the layout, sometimes it’s important that it stay closer to the content to which it’s related. That can complicate the placement of these elements within the structure of the content itself. (though for ad placement I think it’s brilliant)

    One interesting tweak that would work equally well in vertical scrolling layouts (like on a phone) would be deferring by percentage of the content itself: so rather than deferring by columns on a larger layout breakpoint (like a tablet) you might defer ads by percentage of the whole content (so they would be roughly spaced x% of the way through the article as you scroll downward through it)

    Cheers,

    Jason

  3. @Håkon Wium Lie

    It was back in, ooh, 2004/5? We were the last title in the building, which also housed two bigger newspapers, to work on Quark Xpress running on Macs. I’d arrived a few months earlier as the chief sub-editor at the Bristol Observer and set about getting the team trained up on thinking more rigorously about how they laid stuff out, and using some basic maths instead of the mark one eyeball to get it done. We needed to be able to push out a lot of neat-looking pages very quickly, so one of the first things I did was to introduce a strict horizontal and vertical grid to give us some structure. I wonder if having everything locked to the baseline grid might be what (subconsciously) caught your eye?

    As you might imagine, I get on really well with CSS 🙂

    And I’m absolutely delighted that you’re using this article in your talks and demonstrations; it’s a professional compliment of the highest order!

  4. @Brandon Gano Infinite scrolling seems to me to have one very large issue; people don’t scroll infinitely.

    It seems to me content, regardless of the medium of presentation, should have context. With infinite scrolling you’re breaking or modifying the context of your content to suit the presentation method. Paged results means there are finite and knowable expectations for your audience, natural places for them to break off and disengage. Without those, you’re depending on their attention span and available time. I’d be much less likely to abandon a 7 page article at page 5 (I’m almost done) than after 5 pages of an unknown quantity.

    If your content truly is infinite (or close enough to pass), then I think there’s a discoverability issue that a scrollbar isn’t well equipped to handle.

  5. @Jason Pamental

    I agree that text and figures sometimes need to be presented in strict order. This we can do already — it’s how

    and tags work today. We can also float images sideways (with ‘float: left/right’). However, in paged environments, we also want to float things to the top and bottom to make better use of the available space.

    I believe the use case you describe is already supported by the specification. If you say ‘float: top; float-defer-line: 50%’, the figure will end up halfway down the column. Is this what you’d like?

  6. @Henrik Helmers

    Thanks for your support. Floats have had rough days due to uneven implementations and interaction with margin collapsing. However, they are very powerful and pleasant to the eye when used — and implemented — correctly.

  7. @Håkon Wium Lie
    Great article Håkon! You made one statement that left me scratching my head:

    “Likewise, I think ads will be less intrusive in paged, rather than scrolled, media.”

    I’ve read various sports related articles that force visitors to click through 16 pages to read an entire article. Often times, the advertisements are on every fifth page or so and they take up the whole page. The ads in this paged scenario are much more intrusive to me! This is also a really bad use of pagination in my opinion.

    Can you elaborate on your belief that ads will be less intrusive in paged media?

  8. I love these CSS tricks and will definitely play with them next time I work on our internal newsletter. However I do have some questions about the deferring and pullquote techniques.

    How will assistive technology devices such as screen readers interpret the code? Typically, they read from the document and ignore the presentation layer. Using the examples above, does this mean a screen reader would be front loaded with all of the ads and pullquotes?

    And when a user tabs on their keyboard through a document, will they have to go through all of the ads first? Should developers include links to skip each of these?

  9. Hi Håkon,
    This is all great and I already have several use cases where I would use all these properties. Unfortunately, the columns property alone shows some severe problems even with the most basic setup:

    if a column breaks between ‘p’, you may end up with the margin-bottom of the ‘p’ at the top of the next column (ugly).
    If a column breaks after a heading, you got the ‘h’ at the bottom and the related text at the top of the next column (confusing).
    Images in the flow may be sliced and spread across columns.
    Getting a two columns layout vertically balanced is a chance game.

    Those are the first things that pops when I remember my trials.
    Can we expect improvements on this side?
    And how? More sophisticated UA rendering? More css properties?

    Thanks for this great article.

  10. For someone who knows only the basics of CSS, this was a great read. As others have commented, having such power in such simple statements is fantastic, and the flexibility across devices and browsers is surely what the web needs to move towards. A great read, thank you!

Got something to say?

We have turned off comments, but you can see what folks had to say before we did so.

More from ALA

I am a creative.

A List Apart founder and web design OG Zeldman ponders the moments of inspiration, the hours of plodding, and the ultimate mystery at the heart of a creative career.
Career

Discover more from A List Apart

Subscribe now to keep reading and get access to the full archive.

Continue reading