+ - 0:00:00
Notes for current slide
Notes for next slide

CSS from the future

Johannes Ewald
Peerigon GmbH
@jhnnns

1 / 44

There is no CSS4

2 / 44

Starting with CSS3, specification work is separated into different modules.

3 / 44
A list of all current specifications.
4 / 44

CSS Custom Properties

5 / 44

Demo

6 / 44
:root {
--grid: 4px;
}
  • Custom properties are defined like regular properties starting with a --
  • They are usually defined on the :root selector (but they don't have to)
7 / 44
div:after {
content: "Current grid is "var(--grid, "unknown");
}
  • Custom properties can be included via the var() method
  • Property values are treated as "strings" by default
  • The var() method allows to define a default value as second argument
8 / 44
div {
margin-top: calc(var(--grid) * 3);
}
  • Calculations must be wrapped with calc()
9 / 44
html {
--bg-color: rebeccapurple;
}
body {
--bg-color: hotpink;
}
div {
background-color: var(--bg-color); /* hotpink */
}
  • Custom properties cascade just like regular properties
10 / 44
const root = document.documentElement;
root.style.setProperty("--bg-color", "aliceblue")
root.style.getPropertyValue("--bg-color");
  • Custom properties can be read and modified via JavaScript
11 / 44

12 / 44

Side note

CSS2 already provided a special variable:

div {
border: 1px solid currentColor;
}

You should use it!

13 / 44

Mixins

14 / 44

Mixins = a set of properties provided by a single custom property

:root {
--toolbar-theme: {
background-color: hsl(120, 70%, 95%);
border-radius: 4px;
border: 1px solid var(--theme-color, hotpink);
};
}
.toolbar {
@apply --toolbar-theme;
}
15 / 44

Caveat

16 / 44

Nesting

17 / 44
table.colortable {
& td {
text-align: center;
&.c {
text-transform: uppercase;
}
&:first-child, &:first-child + td {
border: 1px solid black;
}
}
& th {
text-align: center;
background: black;
color :white;
}
}
18 / 44

Caveat

19 / 44

Grid

20 / 44

Demo

21 / 44
#page {
display: grid;
grid-template-columns: 200px 1fr;
grid-gap: 12px;
}
  • display: grid switches the formatting context to grid
  • grid-template-{columns,rows} defines a grid template
  • grid-gap defines the gap between columns and rows
22 / 44
#page {
display: grid;
grid-template-columns:
[sidebar-left]
200px
[sidebar-right content-left]
1fr
[content-right];
}
#sidebar {
grid-column: sidebar-left / sidebar-right;
}
#content {
grid-column: content-left / content-right;
}
  • It is also possible to name grid lines
23 / 44
#page {
display: grid;
grid-auto-flow: row dense;
}
  • The auto-placement algorithm kicks in as soon as grid items are not explicitly placed on the grid
  • grid-auto-flow defines where new grid cells are created and whether holes are allowed to be filled up
24 / 44
#page {
display: grid;
justify-content: center;
justify-items: end;
align-content: center;
align-items: end;
}
  • {justify,align}-content aligns all grid cells inside the container along the x/y axis
  • {justify,align}-items aligns the contents of each grid item
25 / 44

You might know these already...

CSS Box Alignment Spec

26 / 44

Note

This is just a small excerpt of the grid layout module.
Check out the full syntax here.

27 / 44

CSS Custom Properties + Grid Layout = 💕

Example

28 / 44

29 / 44

Why another layout mode?

Was Flexbox not good enough?

30 / 44

Flexbox is not suited for page layout:

31 / 44

Flexbox

  • Content dictates layout
  • Use it for smaller components
    like navs, toolbars, etc.

Grid

  • Grid dictates layout *
  • Use it for page layouts, forms, etc.

* with notable exceptions
jakearchibald.com/2014/dont-use-flexbox-for-page-layout

32 / 44

Houdini Drafts

33 / 44

34 / 44

36 / 44

CSS Typed Object Model

myElement.styleMap.set("opacity", new CSSNumberValue(3));
myElement.styleMap.set("margin-left", new CSSSimpleLength(20, "px"));
myElement.styleMap.get("margin-left").value; // 20
myElement.styleMap.get("margin-left").type; // "px"
  • Better performance because values don't need to be parsed/casted
  • Easier style manipulation via JavaScript
37 / 44

CSS Properties and Values

CSS.registerProperty({
name: "--stop-color",
syntax: "<color>",
inherits: false,
initialValue: "rgba(0,0,0,0)"
});
.button {
--stop-color: red;
background:
linear-gradient(var(--stop-color), black);
transition: --stop-color 1s;
}
  • Allows to define typed CSS properties with custom syntax and inheritance behavior
  • Like CSS custom properties, but more low-level
38 / 44

Worklets

  • A worklet is like a light-weight web worker
  • Provides an API to extent the rendering engine
  • Is thread-agnostic: it may or may not run on the main thread
  • Has a minimal API on the JavaScript environment
39 / 44

Paint worklet

Demo

Animation worklet

Demo

Layout worklet

No demo yet

40 / 44

Check out all the other specs:

41 / 44

Caveat

These specs are in a very early stage and the API is very likely to change.
They are far from production-ready.

42 / 44

Other new features

43 / 44

There is no CSS4

2 / 44
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
b Toggle blackout mode
f Toggle fullscreen mode
c Clone slideshow
p Toggle presenter mode
w Pause/Resume the presentation
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow