CSS3 - The Present and the Future

(use arrow keys or click on viewport edges to navigate)

About Me

  • Samuli Hakoniemi ( http://samuli.hakoniemi.net )
  • 32 year old Front-end Developer, who's in love with HTML, CSS and JavaScript
  • 12 years of experience in User Interface Development and Design

Table of Contents

Table of Contents

This presentation is split in two categories: "The Present" and "The Future".

The Present

The Present

Well-known Properties

Well-known Properties

  • border-radius
  • border-image
  • box-shadow
  • box-sizing
  • text-shadow*
  • text-stroke
  • text-overflow

* Originally proposed in CSS2.

Well-known Properties

Border-radius

Example Code

#rounded {
  border-radius: 10px;
}

#mixedRounded {
  border-radius: 20px 0px 5px 10px;
}

#mixedRounded2 {
  border-radius: 20px 0px 5px 10px / 20px;
}

Well-known Properties

Border-Image

The border-image property allows using images as border. Also border-corner-image property exists, which can be used for setting corners of the element.

The border-fit property determine how the border-image(s) will tile. Following values are possible:clip, repeat, scale, stretch, overwrite, overflow and space.

Example Code

#borderImage {
  border-width: 32px;
  border-image: url(star.png) 32 32 repeat stretch;
}      
Border-image

Well-known Properties

Box-shadow

The box-shadow property allows implementing both outer and inner drop-shadows on box elements.

Example Code

#withShadow {
  box-shadow: inset 1px 1px 10px rgba(255,255,255,0.75),
              2px 2px 5px rgba(32,32,32,0.5);
}

Well-known Properties

Box-sizing

The box-sizing property allows changing the behavior of how browser is calculating the element dimensions. It allows three values: content-box (default), padding-box (removed from spec?) and border-box.

Example Code

#contentBox {
  box-sizing: content-box;
}

#paddingBox {
  box-sizing: padding-box;
}

#borderBox {
  box-sizing: border-box;
}
content-box
padding-box
border-box

Well-known Properties

Text-shadow

The text-shadow property is similar to box-shadow, but it's applied to text elements.

Example Code

#redTextShadow {
  text-shadow: 2px 2px 5px rgba(192,32,32,0.5);
} 

I have red text-shadow applied.

Text-stroke

The text-stroke property adds an outline around the characters in text element.

Example Code

#textStroke {
    color: #6699CC;
    text-stroke: 1px #202020;

I have black text-stroke applied.

Well-known Properties

Text-overflow

The text-overflow property allows two values: clip and ellipsis. In order to work, text-overflow also requires properties overflow and white-space set to 'hidden' and 'nowrap'.

Example Code

#textOverflowClip {
    width:600px;
    overflow:hidden;
    white-space:nowrap;
    text-overflow: clip;
}

I am very long text paragraph, which is clipped after 600px.

#textOverflowClip {
  [...]
  text-overflow: ellipsis;
}

I am very long text paragraph, which has ellipsis after 600px.

More Experimental

More Experimental

  • appearance
  • calc()
  • hyphenation
  • masks
  • pointer-events

More Experimental

appearance

The appearance property can be used for displaying an element using platform-native styling. Property allows several possible values, including all the form elements used in HTML5.

Example Code

  div#appearAsButton {
      appearance: button;
  } 
I am a DIV element

More Experimental

calc()

The function calc() can be used as any value of a property where length is required. Currently only Firefox supports this function properly.

Example Code

#brickInTheWall {
  width: 150px;
  height: 150px;
  float: left;
  background-color:#C02020;
}

#anotherBrickInTheWall {
  width: calc(100% - 150px - 20px);
  margin-left:20px;
  [...]
}

More Experimental

Hyphenation

The hyphens property makes possible splitting words with hyphenation. It allows three values: none, manual and auto, where 'manual' requires splitting words with entity ­.

Example Code

#hyphenation {
  hyphens: auto;
}

This is a long paragraph with extraordinary long words that are hyphenated whenever there is need for hyphenation.

More Experimental

masks

The mask-image property can be used for setting a mask for element, similary way as Photoshop's masks work. Also using gradients instead of images are allowed.

Currently masks are only supported in WebKit browsers, where properties are eg. -webkit-mask-image and -webkit-mask-box-image.

Example Syntax

#withMask {
  -webkit-mask: [<mask-image> || <mask-repeat> || <mask-attachment>
                || <mask-position> || <mask-origin> || <mask-clip>
}

More Experimental

pointer-events

The pointer-events property allows to control under what circumstances an element can become the target of mouse events. It allows two values: auto (default) and none.

Example Code

a#noPointer {
  pointer-events: none;
}
cursor:
crosshair
a#noPointer

CSS3 Selectors

CSS3 Selectors

  • E[foo^="bar"]
  • E[foo$="bar"]
  • E[foo*="bar"]
  • E:nth-child(n) and E:nth-last-child(n)
  • E:only-child and E:last-child
  • E:nth-of-type(n) and E:nth-last-of-type(n)
  • E:first-of-type, E:only-of-type and E:last-of-type
  • E:empty
  • E:target
  • E ~ F

CSS3 Selectors

Attribute Selectors

Selectors allow the representation of an element's attributes. When a selector is used as an expression to match against an element, attribute selectors must be considered to match an element if that element has an attribute that matches the attribute represented by the attribute selector.

E[foo^="bar"]>

An E element whose "foo" attribute value begins exactly with the string "bar"

E[foo$="bar"]>

An E element whose "foo" attribute value ends exactly with the string "bar"

E[foo*="bar"]>

An E element whose "foo" attribute value contains the substring "bar"

CSS3 Selectors

Structural pseudo-classes

Selectors introduces the concept of structural pseudo-classes to permit selection based on extra information that lies in the document tree but cannot be represented by other simple selectors or combinators.

E:nth-child(n) and E:nth-last-child(n)

Selector E:nth-child(n) is the n-th child of it's parent element, and E:nth-last-child(n) is the n-th child of it's parent, counting from the last element.

E:only-child and E:last-child

Selector E:only-child is an E element which is an only child of its parent, and E:last-child is an E element which is a last child of its parent.

CSS3 Selectors

Structural pseudo-classes

E:nth-of-type(n) and E:nth-last-of-type(n)

Selector E:nth-of-type(n) is an E element, the n-th sibling of its type, and E:nth-last-of-type(n) is an E element, the n-th sibling of its type, counting from the last element.

E:first-of-type, E:only-of-type and E:last-of-type

Selector E:first-of-type is an E element, first sibling of its type, E:only-of-type is an E element, only sibling of its type, and E:last-of-type is an E element, last sibling of its type.

E:empty

An E element that has no children (including text nodes).

CSS3 Selectors

The target pseudo-class - E:target

Some URIs refer to a location within a resource. This kind of URI ends with "#", followed by an anchor identifier.

Example Code

#highlight:target {
  background-color: yellow;
}

<div id='highlight'>This element is highlighted with yellow
when anchor identifier is 'highlight'.</div>

General Sibling Combinator - E ~ F

An F element preceded by an E element. Notice that element F doesn't have to precede element E immediately.

Media Queries

Media Queries

CSS3 allows defining stylesheets with different conditional expressions. For example, you can have one stylesheet for large screens and a different stylesheet for smaller screens.

Media Queries also allow detecting pixel-ratio and orientation of a device. This makes media queries especially useful when designing for mobile devices.

How to implement

  • <link rel="stylesheet" media="screen and (min-width:480px)" href="smallScreen.css" />
  • @import url(smallScreen.css) screen and (min-width:480px);
  • @media screen and (min-width:480px) { [ ... CSS Selectors ...] }

Media Queries

Resources

Web Fonts

Web Fonts

This space is reserved for Web Fonts (content not finished yet).

Backgrounds

Backgrounds

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position
  • background-clip*
  • background-origin*
  • background-size*

* Proposed in CSS3.

Backgrounds

background-clip

The background-clip property determines the background painting area. Possible values are same as in box-sizing: content-box, padding-box and border-box (default).

background-origin

The background-origin property determines the background positioning area. Possible values are same as in background-clip. The default value is padding-box.

background-size

The background-size property determines how background-image is scaled to the content. Possible values are: auto (default), contain, cover and length values.

Value contain will scale the image while preserving its aspect ratio, so background can fit in element area. Value cover will scale the image to cover the whole element area. Length values can be defined without restrictions.

Gradients

Gradients

A gradient is an image that smoothly fades from one color to another. These are commonly used for subtle shading in background images, buttons, and many other things.

Gradients are a type of image, and can be used anywhere an image can, such as in the background-image or list-style-image properties.

In CSS3 there are two types of gradients: linear gradients and radial gradients.

Gradients

Linear Gradients

A linear gradient is created by specifying a gradient-line and then several colors placed along that line.

The syntax for linear gradient is following: linear-gradient([ [ <angle> | to [left | right] || [top | bottom]> ] ,]? <color-stop>[, <color-stop>]+).

Example Code

#linearGradient {
  background-image: linear-gradient(left top,
                    rgb(55,85,150) 1%,
                    rgb(72,130,224) 50%,
                    rgb(255,255,255) 90%);
}    

Gradients

Radial Gradients

Radial gradient emerge from a single point and smoothly spread outward in a circular or elliptical shape. A radial gradient is specified by indicating the center of the gradient and the size and shape of the ending shape.

The syntax for radial gradients is following: radial-gradient([<position>,]? [<shape> || <size>,]? <stop>, <stop>[, <stop>]*);.

Example Code

#radialGradient {
  background-image: radial-gradient(50% 50%,
                    ellipse closest-corner,
                    #2977ab, #c7dfff 75%);
}    

CSS Columns

CSS Columns

  • allows content to flow from one column to another based on available space rather than document structure
  • allows the column width to be specified and the number of columns to vary depending on available space
  • At the moment applicable in all major browsers (IE10+)

CSS Columns

Columns in Practice

Example Code

    

CSS Transforms

CSS Transforms

  • allows defining operations that transform element in new geometry space
  • by using transforms, elements can be translated, rotated, skewed and scaled.
  • there are two types of transformations: 2D Transforms and 3D Transforms

CSS Transforms

2D Transforms

CSS 2D Transforms allows elements rendered by CSS to be transformed in two-dimensional space.

Example Code

#scaleRotateSkewAndTranslate {
  transform: scale(1.25, 1.25) rotate(15deg)
             skew(-10deg) translate(20px, 20px);
  transform-origin: 50% 50%;           
}

Live Example

Use mouse x-axis + shift for scale, ctrl for rotate or alt for skew.

CSS Transforms

3D Transforms

CSS 3D Transforms extends CSS Transforms to allow elements rendered by CSS to be transformed in three-dimensional space.

It's important to notice that using 3D Transforms enables hardware-acceleration on the device when applicable.

In addition to declaring transforms, it's good to declare perspective property, which enables 3D space. The greater the value, the further the distance, so the less intense the visual effect.

Example Code

#scaleRotateAndTranslate {
  transform: scale3d(1.25, 1.25, 1.0) rotate3d(0.33, 1.0, 0.0, 45deg)
             translate3d(20px, 20px, 0px);
  perspective: 800;
}             

CSS Transitions

CSS Transitions

  • allows property changes in CSS values to occur smoothly over a specified duration
  • were first time introduced in late 2007 by Safari
  • possible to define the target property, duration, timing (easing) and the delay of a transition

CSS Transitions

Creating a Transition

Example Code

#basicElement {
  [...]
  transition: transform 1200ms ease-out 600ms;
} 

#basicElement.changedState {
  transform: rotate(45deg) translate3d(0px, 0px, 0px);
}

CSS Animations

CSS Animations

  • allows modifying CSS property values over time
  • consist of two components: a style describing the animation, and a set of keyframes that indicate the start and end states

CSS Animations

Creating an Animation

Animations are created by defining keyframe rules. These rules are set with @keyframes rule, which takes timing positions as sub-selectors.

For creating animations we can declare following properties:

  • animation-name
  • animation-duration
  • animation-iteration-count
  • animation-direction
  • animation-timing-function

CSS Animations

Example

@keyframes moving {
  0% {
    transform:rotate(0deg) translate3d(0px, 0px, 0px);
  }      

  50% {
    transform:rotate(30deg) translate3d(250px, -200px, 0px);
  }      

  100% {
    transform:rotate(0deg) translate3d(0px, 0px, 0px);
  }
}

#keepMoving {
  animation: moving 2000ms infinite both ease-in-out;
}

The Future

Flexible Box Model

Flexible Box Model

  • flexible Box or "flexbox" is, according to W3C, an element with display:flexbox or display:inline-flexbox
  • offers a new box model which has been optimized for laying out user interfaces
  • flexbox has earlier been defined with values for display property, such as box and box-flex
  • but Flexbox Specification has been changed in early fall 2011.
  • no browser support yet (Chrome Canary has partial support).

Grid Layout

Grid Layout

  • Grid Layout can be used to intelligently reflow elements within a webpage,
  • It enables an author to align elements into columns and rows,
  • Element flow also be defined with templates,
  • Doesn't have content structure, and thus enables a wide variety of layouts not possible with tables,
  • No proper browser support yet (IE10 has partial support).

Grid Layout

Defining a Simple Grid

A Grid element is declared in markup by setting the display property of an element to 'grid'. Columns and rows are defined either in fixed or percental values, or in fractions.

Child elements of the Grid are termed Grid Items and may be positioned and sized by the Grid element by leveraging the following logical concepts:

  • Grid Tracks,
  • Grid Lines and
  • Grid Cells

Grid Layout

Grid Tracks

Grid Tracks are the columns and rows of the Grid defined with the grid-rows and grid-columns properties on the Grid element.

Example Code

#grid { 
  display: grid; 
  grid-columns: 150px 1fr; /* Two columns */
  grid-rows: 50px 1fr 50px; /* Three rows  */
}    

Grid Layout

Grid Items

Grid Items are placed into Grid Cells. A Grid Cell defines the available space for a Grid Item. The dimensions of the Grid Cell are specified by naming the starting and ending lines which define the columns and rows that the Grid Cell occupies.

Grid Lines

Grid Lines are the horizontal or vertical dividing lines that exist on either side of a column or row. Grid Lines may be referred to by their Grid Line number, or they may be named by the author.

Grid Cells

Grid Cells are the logical space used to lay out one or more Grid Items. Grid Cells may be defined explicitly using the grid-template property or the grid-cell pseudo-element.

Grid Layout

Grid Templates

Grid Cells can also be defined explicitly using one-letter names via the grid-template property. The grid-template property provides a visualization of the Grid element's structure while simultaneously defining the Grid Cells which can be used to layer or stack the child elements of the Grid.

Example Code

#grid {
  display: grid; 
  grid-template: "hh"
                 "nm"
                 "ff";
}     

Where 'h' stands for header, 'n' for navigation, 'm' for main content and 'f' for footer.

CSS Regions

CSS Regions

  • CSS regions allows content to flow across multiple areas,
  • similiar to CSS3 Columns, but content flow isn't as strict,
  • regions can be in any order in the document,
  • content can flow eg. as columns or in grid,
  • supported in IE10 (but there's also a polyfill)

CSS Regions

CSS Regions in Practice

Regions can be used in cases where we want to represent text content that flows from one area into another. Additionally content can be styled differently based on which region it's displayed. This is useful when creating more complex layouts.

flow-into (flow)

Defines the element that will flow into regions.

flow-from (content:from())

Defines the element regions that are used for displaying the source element's content.

content-order / region-order

Defines the order of flow.

CSS Regions

Example

This is basic example on how regions work.

Example Code

#source {
  flow-into: main-thread;
  flow: "main-thread";
  -ms-flow-into: main-thread;
}
    
.region {
  flow-from: main-thread;
  content: from(main-thread);
  -ms-flow-from: main-thread;
}    
<div id="source">
  <p>A long article text [...]</p>
</div>

<div class="region"></div>        
<div class="region"></div>        
<div class="region"></div>        

CSS Exclusions

CSS Exclusions

  • CSS Exclusions define arbitrary areas, allowing inline content to flow around it,
  • Can be positioned with any CSS positioning schemes,
  • Currently supported by Internet Explorer 10 (as "positioned floats")

CSS Exclusions

CSS Exclusions

Exclusion elements define exclusion areas that contribute to their containing block's wrapping context. As a consequence, exclusions impact the layout of their containing block's descendants.

Properties

  • 'wrap-flow',
  • 'wrap-margin',
  • 'wrap-padding',
  • 'wrap-through' and
  • 'wrap' as a shorthand

CSS Exclusions

wrap-flow ('-ms-wrap-side' in IE)

  • An element becomes an exclusion when its 'wrap-flow' property has a computed value other than 'auto',
  • Possible values are: 'both', 'start' (IE: 'left'), 'end' (IE: 'right'), 'maximum' and 'clear',

wrap-margin ('-ms-wrap-margin' in IE)

  • Specifies a margin that is used to offset the inner wrap shape from other shapes,
  • Only positive values are applied.

CSS Exclusions

wrap-padding

  • Specifies a padding that is used to offset the inline flow content wrapping on the inside of elements,
  • Only positive values are applied.

wrap-through ('-ms-flow-wrap' in IE)

  • By default, an element inherits its parent wrapping context,
  • Possible values: wrap and none.

CSS Shapes

CSS Shapes

  • CSS Shapes control the geometric shapes used for wrapping inline flow content outside or inside an element,
  • Two different types of shapes exist: 'outside' and 'inside',
  • Can be specified using SVG Basic Shapes syntax,
  • Also support for defining shapes with images is under discussion,
  • No browser support at the moment (Adobe has a prototype "browser" to test with)

CSS Shapes

What Shapes Are?

The following SVG shapes are supported by the CSS shapes module:

  • rectangle()
  • circle()
  • ellipse()
  • polygon()

CSS Shapes

Shapes in Practice

Example Code

#exclusion {
  wrap-flow: both;
  -webkit-wrap-shape-mode: around;
  shape-outside: circle(50%, 50%, 50%);
  -webkit-wrap-shape: circle(50%, 50%, 50%);
}

<div> 
    <div id="exclusion"></div> 
    This is a long paragraph [...] 
</div>

CSS Shaders

CSS Shaders

  • A set of software instructions used to calculate rendering effects,
  • Written in OpenGL ES Shading Language,
  • Proposed by Adobe (backed up by Apple and Opera),
  • A complement to the SVG Filter Effects Specification,
  • Consists the concept of vertex and fragment shaders,
  • Applied via CSS using the filter property,
  • Filter Effects has partial support, Shaders not yet applicable

CSS Shaders

Filter Effects 1.0

Works as a general syntax for defining a filter by assembling filter primitives in a graph. A filter effect consists of a series of graphics operations that are applied to a given source graphic to produce a modified graphical result.

The filter property can use a number of predefined filter functions: blur, drop-shadow, gamma, grayscale, hue-rotate, invert, opacity, saturate, sepia, and sharpen.

Example Code

#grayscaleAndBlur {
  filter: grayscale(0) blur(0px);
  transition: filter 360ms ease-in;
}

#grayscaleAndBlur:hover {
  filter: grayscale(1) blur(2px);
}                        

CSS Shaders

Vertex Shaders

Describes the properties of a vertex (the points at the corners of geometric shapes, usually triangles). These include the position and colour of the vertex, and the texture mapping.

The vertex shader will transform these vertices, such as change the position in 3D space, the colour, or the depth. This could for example transform a flat shape into something that looks like it has a 3D perspective, such as the curved page of a book. The spec includes examples such as a page curl, waving flag or wobble effect.

Example Code

#myElement {
  filter:custom(url('myVertexShader.vs'), 10 10, someParameter someValue);
}    

CSS Shaders

Fragment Shaders

Fragment shaders (also known as pixel shaders) extends the capability of Filters so that you can define your own pixel manipulations. They describe the properties of pixel, such as the colour, opacity, and bit-depth and can be used for things such as lighting effects or colour manipulation such as changing an image to grayscale or sepia tone.

CSS Filters includes a number of pre-defined ones, but fragment shaders will allow you to define your own.

Example Code

#myElement {
  filter:custom(url('myFragmentShader.fs'));
}    

Peek into CSS4

Peek into CSS4

CSS Working Group has published a first working draft of CSS4 Selectors. Work on CSS4 specifications will go on in parallel with CSS3 modules.

New Selectors, namespaces and new pseudo-classes

  • Parent selector,
  • Namespaces,
  • UI state pseudo-classes,
  • Tree-structural pseudo-classes,
  • Logical Combinations

Peek into CSS4

Parent selector

Earlier there was no option to select the parent of an element in CSS (not even in CSS3). With CSS4, the most important news for many developers in the current W3C draft is that support for the parent selector exists.

Example Code

fieldset! > input:focus {
  background-color:#F8F0B0;
}

Peek into CSS4

Namespaces

The @namespace declares a namespace prefix and associates it with a given namespace name. This namespace prefix can then be used in namespace-qualified names such as the CSS qualified names.

A CSS qualified name is a name explicitly located within (associated with) a namespace. Qualified name is a namespace prefix prepended to an element or attribute name, separated by a vertical bar.

CSS Namespace Module was introduced already as a part of CSS3.

Example Code

@namespace "http://www.example.com";
@namespace foo "http://www.example.com/foo";

h1 { font-size: 42px; } /* Belongs in example.com */
|h1 { font-size: 36px; } /* Belongs eg. in example.com/bar */
*|h1 { font-size: 32px; } /* Belongs in both namespaces */
foo|h1 { font-size: 28px; } /* Belongs in example.com/foo */

Peek into CSS4

UI state pseudo-classes

  • :enabled / :disabled *
  • :checked *
  • :indeterminate
  • :default
  • :valid / :invalid
  • :in-range / :out-of-range
  • :required / :optional
  • :read-only / :read-write

* Originally proposed in CSS3

Peek into CSS4

Tree-structural pseudo-classes

  • :root*
  • :nth-match()
  • :nth-last-match()

* Originally proposed in CSS3

Peek into CSS4

Logical Combinations

Logical Combinations stands for combining a group of selectors into one list.

Matches-any - :matches()

The matches-any pseudo-class is a functional notation taking a selector list as its argument. It represents an element that is represented by its argument.

Example Code

:matches(section, article, aside) h1 {
    font-size: 2em;
}    

Negation - :not()

The negation pseudo-class is a functional notation taking a selector list as an argument. It represents an element that is not represented by its argument. This selector was originally proposed in CSS3.

Example Code

:not(nav, aside) a {
    color: #336699;
}    

Thank you