Advertisement
  1. Web Design
  2. HTML/CSS
  3. CSS

CSS Exclusions: Making Boring Layouts Less Boring

Scroll to top

In this tutorial, we are going to explore CSS Exclusions. At the first glance, CSS Exclusions may look similar to CSS Shapes in that they wrap content around an element. Technically, however, they serve a different purpose.

The CSS Shapes Module defines an element’s real shape, but content will not wrap around that shape until the element is floated. The CSS Exclusions Module, on the other hand, is specifically designed for this; allowing inline content to wrap around an element without floating it, regardless of how the element is positioned—absoluterelative, or fixed.

Properties

The CSS Exclusions Module introduces a couple of new properties and values, namely:

  • wrap-flow: sets the exclusion area, as well as how the content should wrap around it.
  • wrap-margin: is pretty self-explanatory, setting the margin or offset surrounding the exclusion area.

Browser Support

It is worth noting that CSS Exclusions currently only works in Internet Explorer 10 and up, and Edge, once again demonstrating how Microsoft are pushing web browser frontiers (we have them to thank for the development of CSS Grid too). For the time being, we have to prefix the new properties with -ms- when being used.

can i use css exclusionscan i use css exclusionscan i use css exclusions
And the sea of red continues to the right..

To better understand how CSS Exclusions works, we’ve prepared a simple starting page comprising a few lines of content and an avatar image at the end.

Layout without CSS Exclusions 

This is a fairly common pattern on the web, so let’s see if we can polish it up a bit using CSS Exclusions. Assuming your browser supports Excludes, we’re aiming for this result:

Using CSS Exclusions

First, let’s arrange the content into two columns and position the avatar image to the center. It doesn’t matter how you arrange the layout, you can use CSS Flexbox, CSS Grid, or the “traditional” approach using the float property.

Lovely, column-based text (not compulsory for this demo)

We can see from the above image that the avatar image has been removed from the document flow and is positioned on top of the content, obscuring it. Using CSS Exclusions, we can force the content to flow around the avatar image. 

To do so, we set the wrap-flow property to the avatar and set the value to both.

1
.avatar {
2
   -ms-wrap-flow: both; // Works in IE and Edge.
3
   wrap-flow: both; // Does't not work in any browser.
4
}

The wrap-flow property sets the .avatar as an “exclusion area”; an area where no content should pass through. As you can see below, the content now flows to the right and left of the avatar image.

Possible Values

Other acceptable values are startendmaximumminimum, and clear

The first value, start, will wrap the content around the start of the exclusion area, but leave the end of the area empty.

1
.avatar {
2
   -ms-wrap-flow: start;
3
}

Given the content on our page, the result would look as follows.

Content flows to the left of the image. 

The end value, as its name suggests, works conversely; it will wrap the content around the end of the exclusion area.

1
.avatar {
2
   -ms-wrap-flow: end;
3
}

This gives us the following outcome:

Wrapping around the right

Note: the start and the end of the exclusion area is determined by the writing direction of the content. Where scripts are written right-to-left, such as often seen with Arabic and Hebrew, the content wraps starting on the right and ends on the left of the exclusion area. 

With Japanese, when written from top-to-bottom, the content wraps starting from the top and ends at the bottom.

Content flow in different writing directions. Image by (W3C)

The third acceptable value is maximum.

1
.avatar {
2
   -ms-wrap-flow: maximum;
3
}

This will wrap the content around the exclusion area wherever it finds the wider space within the container area.

The minimum value works in the opposite way.

1
.avatar {
2
   -ms-wrap-flow: minimum;
3
}

Here, the content will flow through the narrowest space available around the exclusion area.

The last value is clear.

1
.avatar {
2
   -ms-wrap-flow: clear;
3
}

This is quite similar to the clear you’re already familiar with from floats, in that it will clear the right and the left of the exclusion area. It therefore only lets the content flow to the top and bottom of the exclusion area.

Exclusions Margin

Similarly to CSS Shapes the CSS Exclusions Module also comes with a property for defining the margin of the exclusion area, namely wrap-margin. Unlike the margin property, the value of wrap-margin must be a positive value.

1
.avatar {
2
   -ms-wrap-flow: end;
3
   -ms-wrap-margin: -10px; // Invalid.
4
   -ms-wrap-margin: 10px; // Valid.
5
}

Furthermore, the wrap-margin properties does not allow us to set the margin of each side (right, left, top, and bottom) individually. Whether this feature will be introduced in the future remains to be seen.

1
.avatar {
2
   -ms-wrap-flow: end;
3
   -ms-wrap-margin: 10px 20px 10px 30px; // Invalid.
4
   -ms-wrap-margin: 10px; // Valid.
5
}

Once set, we should see more whitespace around the exclusion area.

@supports

Given that we’ve positioned our avatar over the content, without support for CSS Excludes we’re left with a messy layout. Therefore, it’s wise to consider the fallback, and wrap the relevant styles within a @supports rule, like so:

1
.site-content .avatar {
2
  width: 180px;
3
  height: 180px;
4
  margin-right: auto;
5
  margin-left: auto;
6
  text-align: center;
7
  margin-top: 80px;
8
}
9
10
/* wrap the relevant rules in a @supports declaration, just to be safe */
11
@supports (-ms-wrap-flow: both) {
12
  .site-content .avatar {
13
    position: absolute;
14
    top: 300px;
15
    left: 50%;
16
    margin-left: -90px;
17
    -ms-wrap-margin: 50px;
18
    -ms-wrap-flow: both
19
  }
20
}

Now our CSS Exclusion styles will only be applied if supported by the browser.

Wrapping Up

CSS Exclusions, along with CSS Shapes and other bleeding edge specifications, will allow us to explore website layouts way beyond what we’ve become used to. Hopefully we will see speedy advancement in browser support, and more pushing of boundaries from the Microsoft Edge team!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.