display: contents is enabled by default in Blink
and it will be probably shipped in Chrome 65 and Safari 11.1.
These browsers will join Firefox that is shipping it since version 37,
which makes Edge the only one missing the feature (you can vote for it!).
You might (or not) remember a blog post from early 2016
where I was talking about the Igalia Coding Experience program and some ideas
of tasks to be done as part of the Web Platform team.
One of the them was
display: contents which is finally happening.
This new value for the
display property allows you to somehow remove
an element from the box tree but still keep its contents.
The proper definition from the spec:
The element itself does not generate any boxes, but its children and pseudo-elements still generate boxes and text runs as normal. For the purposes of box generation and layout, the element must be treated as if it had been replaced in the element tree by its contents (including both its source-document children and its pseudo-elements, such as
::afterpseudo-elements, which are generated before/after the element’s children as normal).
A simple example will help to understand it properly:
display: contents makes that the
div doesn’t generate any box,
so its background, border and padding are not renderer.
However the inherited properties like
font have effect
on the child (
span element) as expected.
For this example, the final result would be something like:
If you want more details Rachel Andrew has a nice blog post about this topic.
CSS Grid Layout &
As you could expect from a post from myself this is somehow related
to CSS Grid Layout. 😎
display: contents can be used as a replacement of subgrids
(which are not supported by any browser at this point) in some use cases.
However subgrids are still needed for other scenarios.
The canonical example for Grid Layout auto-placement is a simple form like:
However this is not the typical HTML of a form, as you usually want to use a list inside, so people using screen readers will know how many fields they have to fill in your form beforehand. So the HTML looks more like this:
display: contents you’ll be able to have the same layout
than in the first case with a similar CSS:
So this is really nice, now when you convert your website to start using CSS Grid Layout, you would need less changes on your HTML and you won’t need to remove some HTML that is really useful, like the list in the previous example.
As I said in the introduction, Firefox shipped
three years ago,
however Chromium didn’t have any implementation for it.
Igalia as CSS Grid Layout implementor was interested in having support
for the feature as it’s a handy solution for several Grid Layout use cases.
The proposal for the Igalia Coding Experience was
the implementation of
display: contents on Blink
as the main task.
Emilio did an awesome job and managed to implement most of it,
reporting issues to CSS Working Group and other browsers as needed,
and writing tests for the
to ensure interoperability between the implementations.
Once the Coding Experience was over there were still
a few missing things to be able to enable
Rune Lillesveen (Google and previously Opera)
who was helping during the whole process with the reviews,
finished the work and shipped it past week.
WebKit already had an initial support for
that was only used internally by Shadow DOM implementation
and not exposed to the end users, neither supported by the rest of the code.
We reactivated the work there too, he didn’t have time to finish the whole thing but later Antti Koivisto (Apple) completed the work and enabled it by default on trunk by November 2017.
Igalia is one of the top external contributors
on the open web platform projects.
This put us on a position that allows us to implement new features
in the different open source projects, thanks to our community involvement
and internal knowledge after several years of experience on the field.
display: contents implementation,
this feature probably wouldn’t be available today in Chromium and WebKit
without Igalia’s support, in this particular case through a Coding Experience.
We’re really happy about the results of the Coding Experience and we’re looking forward to repeat the success story in the future.
Of course, all the merit goes to Emilio, who is an impressive engineer and did a great job during the Coding Experience. As part of this process he got committer privileges in both Chromium and WebKit projects. Kudos!
Last, but not least, thanks to Antti and Rune for finishing the work
display: contents available to WebKit and Chromium users.