The Creative New World of CSS

A presentation at UX Immersion: Interactions in March 2018 in Newport Beach, CA, USA by Rachel Andrew

Slide 1

Slide 1

@rachelandrew UX Immersion: Interactions The Creative New World of CSS

Slide 2

Slide 2

Lining things up with Box Alignment level 3

Slide 3

Slide 3

This is the verticalcentering module.

Slide 4

Slide 4

Flexbox Centre the content of .box. .box { display: flex; align-items: center; justify-content: center; } <div class="box"> <img alt="balloon" src="squareballoon.jpg"> </div>

Slide 5

Slide 5

Flex container height http://codepen.io/rachelandrew/pen/XKaZWm

Slide 6

Slide 6

Flex container height stretch center http://codepen.io/rachelandrew/pen/RavbmN flex-start flex-end

Slide 7

Slide 7

Flexbox Aligning items within the flex container .wrapper { display: flex; } .wrapper li { min-width: 1%; flex: 1 0 25%; } .wrapper li:nth-child(2) { align-self: center; } .wrapper li:nth-child(3) { align-self: flex-start; } .wrapper li:nth-child(4) { align-self: flex-end; }

Slide 8

Slide 8

https://demos.scotch.io/visual-guide-to-css3-flexbox-flexbox-playground/demos/

Slide 9

Slide 9

http://flexboxfroggy.com/

Slide 10

Slide 10

https://flexbox.webflow.com/

Slide 11

Slide 11

Box Alignment defines how these properties work in other layout methods.

Slide 12

Slide 12

CSS BOX ALIGNMENT LEVEL 3 This module contains the features of CSS relating to the alignment of boxes within their containers in the various CSS box layout models: block layout, table layout, flex layout, and grid layout. https://drafts.csswg.org/css-align/

Slide 13

Slide 13

A proper layout system with CSS Grid Layout

Slide 14

Slide 14

CSS Grid Layout Aligning grid items with the Box Alignment properties. .wrapper { display: grid; grid-template-columns: } .wrapper li { min-width: 1%; } .wrapper li:nth-child(2) align-self: center; } .wrapper li:nth-child(3) align-self: start; } .wrapper li:nth-child(4) align-self: end; } repeat(4, 1fr); { { {

Slide 15

Slide 15

Grid container height stretch center http://codepen.io/rachelandrew/pen/jqdNoL start end

Slide 16

Slide 16

CSS GRID LAYOUT “Unlike Flexbox, however, which is single-axis–oriented, Grid Layout is optimized for 2-dimensional layouts: those in which alignment of content is desired in both dimensions.” https://drafts.csswg.org/css-grid/

Slide 17

Slide 17

CSS Grid Layout Defining a 3 column grid .cards { display:grid; grid-template-columns: 1fr 1fr 1fr; grid-gap: 10px; }

Slide 18

Slide 18

http://codepen.io/rachelandrew/pen/qqdGOa

Slide 19

Slide 19

Flexbox Flexbox can wrap flex items onto multiple rows. A new row becomes a new flex container for the purposes of distributing space. .cards { display:flex; flex-wrap: wrap; } .card { flex: 1 1 250px; margin: 5px; }

Slide 20

Slide 20

https://codepen.io/rachelandrew/pen/Gqvrwz

Slide 21

Slide 21

CSS Grid Layout Create as many columns as will fit into the container with a minimum width of 200px, and a maximum of 1fr. .cards { display:grid; grid-template-columns: repeat(auto-fill, minmax(200px,1fr)); grid-gap: 10px; }

Slide 22

Slide 22

https://codepen.io/rachelandrew/pen/Gqvrwz

Slide 23

Slide 23

minmax()

Slide 24

Slide 24

Slide 25

Slide 25

Slide 26

Slide 26

http://codepen.io/rachelandrew/pen/QKwvxJ

Slide 27

Slide 27

minmax() Rows should be a minimum of 150px and a maximum of auto .home-hero { display: grid; grid-gap: 1px; grid-auto-rows: minmax(150px, auto); }

Slide 28

Slide 28

CSS Grid auto-placement

Slide 29

Slide 29

Grid auto-placement <ul class="colors"> I have a list with all ye olde web safe colours in it.

<li style="backgroundcolor:#FFFFFF;color:black" class="white">FFF FFF </li> <li style="backgroundcolor:#CCCCCC;color:black">CCC CCC </li> <li style="backgroundcolor:#999999;color:black" class="span3">999 999 </li> </ul>

Slide 30

Slide 30

Grid auto-placement I auto-fill columns and rows with minmax() .colors { display: grid; grid-template-columns: repeat(auto-fill,minmax(80px, 1fr)); grid-gap: 2px; grid-auto-rows: minmax(80px, auto); }

Slide 31

Slide 31

http://codepen.io/rachelandrew/pen/LRWPNp/

Slide 32

Slide 32

Grid auto-placement Adding classes to some elements, by setting the value of grid-column-end and grid-row-end to span. .white { grid-column: 1 / -1; grid-row: 3; } .black { grid-column: 1 / -1; grid-row: 6; } .span2 { grid-column-end: span 2; grid-row-end: span 2; } .span3 { grid-column-end: span 3; grid-row-end: span 3; } .tall4 { grid-row-end: span 4; }

Slide 33

Slide 33

Slide 34

Slide 34

Grid auto-placement grid-auto-flow with a value of dense .colors { display: grid; grid-template-columns: repeat(auto-fill,minmax(80px, 1fr)); grid-gap: 2px; grid-auto-rows: minmax(80px, auto); grid-auto-flow: dense; }

Slide 35

Slide 35

Slide 36

Slide 36

The source and display order disconnect.

Slide 37

Slide 37

https://caniuse.com/#feat=css-grid

Slide 38

Slide 38

http://cssgridgarden.com/

Slide 39

Slide 39

Slide 40

Slide 40

https://www.youtube.com/c/layoutland

Slide 41

Slide 41

https://gridbyexample.com/video/

Slide 42

Slide 42

Vanishing boxes with display: contents

Slide 43

Slide 43

DISPLAY: CONTENTS The element itself does not generate any boxes, but its children and pseudo-elements still generate boxes as normal. For the purposes of box generation and layout, the element must be treated as if it had been replaced with its children and pseudoelements in the document tree. https://drafts.csswg.org/css-display/#box-generation

Slide 44

Slide 44

allow indirect children to become flex or grid items

Slide 45

Slide 45

display: contents All elements are direct children of ‘wrapper’ except for the li elements.

<div class="wrapper"> <h1></h1> <p></p> <blockquote class="inset"></blockquote> <p></p> <ul class="images"> <li></li> <li></li> </ul> <p></p> </div>

Slide 46

Slide 46

display: contents A two column grid. The h1, p and blockquote with a class of inset are all direct children. .wrapper { max-width: 700px; margin: 40px auto; display: grid; grid-column-gap: 30px; grid-template-columns:1fr 1fr; } .wrapper h1, .wrapper p { grid-column: 1 / -1; } .inset { grid-column: 1 ; font: 1.4em/1.3 'Lora', serif; padding: 0; margin: 0; } .inset + p { grid-column: 2; }

Slide 47

Slide 47

http://codepen.io/rachelandrew/pen/gLborW

Slide 48

Slide 48

display: contents The ul has a class of images. By applying display: contents the ul is removed and the li elements become direct children. .images { display: contents; }

Slide 49

Slide 49

http://codepen.io/rachelandrew/pen/gLborW

Slide 50

Slide 50

https://caniuse.com/#feat=css-display-contents

Slide 51

Slide 51

Getting curvy with CSS Shapes

Slide 52

Slide 52

CSS SHAPES CSS Shapes describe geometric shapes for use in CSS. For Level 1, CSS Shapes can be applied to floats. A circle shape on a float will cause inline content to wrap around the circle shape instead of the float’s bounding box. https://drafts.csswg.org/css-shapes/

Slide 53

Slide 53

CSS Shapes .balloon { float: left; width: 429px; } Shapes are applied to floats. <div class="box"> <img class="balloon" src="roundballoon.png" alt="balloon"> <p>...</p> </div>

Slide 54

Slide 54

http://codepen.io/rachelandrew/pen/KrvyQq

Slide 55

Slide 55

CSS Shapes A simple shape using the shape-outside property .balloon { float: left; width: 429px; shape-outside: circle(50%); }

<div class="box"> <img class="balloon" src="roundballoon.png" alt="balloon"> <p>...</p> </div>

Slide 56

Slide 56

http://codepen.io/rachelandrew/pen/KrvyQq

Slide 57

Slide 57

CSS Shapes Floating generated content to create a shape .box::before { content: ""; display: block; float: left; width: 429px; height: 409px; shape-outside: circle(50%); }

Slide 58

Slide 58

http://codepen.io/rachelandrew/pen/mErqxy

Slide 59

Slide 59

CSS Shapes Using clip-path to cut away part of an image .balloon { float: right; width: 640px; height: 427px; shape-outside: ellipse(33% 50% at 460px); -webkit-clip-path: ellipse(28% 50% at 460px); clip-path: ellipse(28% 50% at 460px); }

Slide 60

Slide 60

http://codepen.io/rachelandrew/pen/vKJmXR

Slide 61

Slide 61

http://caniuse.com/#feat=css-shapes

Slide 62

Slide 62

https://bennettfeely.com/clippy/

Slide 63

Slide 63

Slide 64

Slide 64

Can I Use this with Feature Queries

Slide 65

Slide 65

FEATURE QUERIES The ‘@supports’ rule is a conditional group rule whose condition tests whether the user agent supports CSS property:value pairs. https://www.w3.org/TR/css3-conditional/#at-supports

Slide 66

Slide 66

http://caniuse.com/#feat=css-featurequeries

Slide 67

Slide 67

Anything new in CSS you can use feature queries to detect support.

Slide 68

Slide 68

Feature Queries @supports (display: grid) { .has-grid { Checking for support of Grid Layout /* CSS for grid browsers here */ } }

Slide 69

Slide 69

Feature Queries @supports ((display: grid) and (shapeoutside: circle())) { .has-grid-shapes { Test for more than one thing /* CSS for these excellent browsers here */ } }

Slide 70

Slide 70

Using Feature Queries ▸ Write CSS for browsers without support ▸ Override those properties inside the feature queries ▸ See https://hacks.mozilla.org/2016/08/using-feature-queries-in-css/

Slide 71

Slide 71

Feature Queries Write CSS for browsers without support, override that and add new CSS inside the feature query. .balloon { border: 1px solid #999; padding: 2px; } @supports ((shape-outside: ellipse()) and ((clip-path: ellipse()) or (-webkit-clippath:ellipse()))) { .balloon { border: none; padding: 0; float: right; width: 640px; min-width: 640px; height: 427px; shape-outside: ellipse(33% 50% at 460px); -webkit-clip-path: ellipse(28% 50% at 460px); clip-path: ellipse(28% 50% at 460px); } }

Slide 72

Slide 72

http://codepen.io/rachelandrew/pen/vKJmXR

Slide 73

Slide 73

http://codepen.io/rachelandrew/pen/vKJmXR

Slide 74

Slide 74

Websites that enhance themselves as the platform improves.

Slide 75

Slide 75

Fancy introductions with Initial Letter

Slide 76

Slide 76

INITIAL LETTER Large, decorative letters have been used to start new sections of text since before the invention of printing. In fact, their use predates lowercase letters entirely. This [initial-letter] property specifies styling for dropped, raised, and sunken initial letters. https://drafts.csswg.org/css-inline/#initial-letter-styling

Slide 77

Slide 77

Initial Letter Make the initial letter four lines tall, one line above the content and 3 into the content. h1+p::first-letter { initial-letter: 4 3; }

Slide 78

Slide 78

http://codepen.io/rachelandrew/full/WobvQq/

Slide 79

Slide 79

http://codepen.io/rachelandrew/full/WobvQq/

Slide 80

Slide 80

Currently Safari 9+ only.

Slide 81

Slide 81

Initial Letter Adding additional styling to the initial letter. h1+p::first-letter { font-weight: bold; initial-letter: 7 ; background-color: rgb(114,110,151); padding: 2em .5em; margin: 0 5px 0 0; color: #fff; border-radius: 50% ; float: left; shape-outside: margin-box; }

Slide 82

Slide 82

http://codepen.io/rachelandrew/pen/LbEpPX

Slide 83

Slide 83

Initial Letter Using feature queries to test for support before adding rules that style the first letter. @supports (initial-letter: 3) or (webkit-initial-letter: 3) { h1+p::first-letter { font-weight: bold; initial-letter: 7 ; background-color: rgb(114,110,151); padding: 2em .5em; margin: 0 5px 0 0; color: #fff; border-radius: 50% ; float: left; shape-outside: margin-box; } }

Slide 84

Slide 84

http://codepen.io/rachelandrew/pen/LbEpPX

Slide 85

Slide 85

Upside down and back to front with Writing modes

Slide 86

Slide 86

http://codepen.io/rachelandrew/pen/LbVQNW

Slide 87

Slide 87

Writing Modes .wrapper { display: grid; grid-template-columns: auto 1fr; grid-gap: 40px; } Using vertical-rl then flipping the text with a transform. h1 { writing-mode: vertical-rl; transform: rotate(180deg); text-align: right; grid-column: 1; align-self: start; justify-self: start; }

Slide 88

Slide 88

http://codepen.io/rachelandrew/pen/LbVQNW

Slide 89

Slide 89

http://caniuse.com/#feat=css-writing-mode

Slide 90

Slide 90

Variables in CSS with Custom properties

Slide 91

Slide 91

CSS CUSTOM PROPERTIES (CSS VARIABLES) This module introduces a family of custom author-defined properties known collectively as custom properties, which allow an author to assign arbitrary values to a property with an author-chosen name, and the var() function, which allow an author to then use those values in other properties elsewhere in the document. https://drafts.csswg.org/css-variables/

Slide 92

Slide 92

Custom Properties Define variables then use them in CSS :root { --primary: blue; --secondary: orange; } h1 { color: var(--primary); }

Slide 93

Slide 93

Custom Properties :root { --primary: blue; --secondary: orange; } Can be tested for using feature queries @supports (--primary: blue) { h1 { color: var(--primary); } h2 { color: var(--secondary); } }

Slide 94

Slide 94

http://codepen.io/rachelandrew/pen/mErpZA

Slide 95

Slide 95

http://caniuse.com/#feat=css-variables

Slide 96

Slide 96

Adding things up with calc()

Slide 97

Slide 97

Basic mathematics in CSS

Slide 98

Slide 98

calc() <div class="wrapper"> <div class="box box1"> <p>…</p> Three boxes, each with a div nested inside.

<div>height is defined by the flex container.</div> </div> <div class="box box2"> <div>height is 140px</div> </div> <div class="box box3"> <div>height is 14em</div> </div> </div>

Slide 99

Slide 99

calc() Two of the outer boxes have a height, box1 is the height of the content inside. Box3 will grow on hover. .box2 { height: 140px; } .box3 { height: 14em; transition: height 0.5s ease; } .box3:hover { height: 30em; }

Slide 100

Slide 100

calc() In the CSS for the inner box, we calculate the height as 50% - 20px. .box > div { color: #fff; border-radius: 5px; position: absolute; bottom: 20px; right: 20px; width: 30%; height: calc(50% - 20px); }

Slide 101

Slide 101

http://codepen.io/rachelandrew/full/VmYYqM/

Slide 102

Slide 102

http://caniuse.com/#feat=calc

Slide 103

Slide 103

Staying put with position: sticky

Slide 104

Slide 104

POSITION: STICKY A stickily positioned box is positioned similarly to a relatively positioned box, but the offset is computed with reference to the nearest ancestor with a scrolling box, or the viewport if no ancestor has a scrolling box. https://drafts.csswg.org/css-position/#sticky-pos

Slide 105

Slide 105

position: sticky A dl with dt elements followed by multiple dd elements.

<dl class="authors"> <dt>A</dt> <dd>John Allsopp</dd> <dd>Rachel Andrew</dd> <dt>B</dt> <dd>. . .</dd> </dl>

Slide 106

Slide 106

position: sticky Applying position: sticky to the dt .authors dt { position: sticky; top: 0; }

Slide 107

Slide 107

http://codepen.io/rachelandrew/pen/NbPamG

Slide 108

Slide 108

http://caniuse.com/#feat=css-sticky

Slide 109

Slide 109

Snap to it with Scroll snapping

Slide 110

Slide 110

https://drafts.csswg.org/css-scroll-snap-1/

Slide 111

Slide 111

http://caniuse.com/#feat=css-snappoints

Slide 112

Slide 112

Scroll Snapping .gallery { scroll-snap-type: x mandatory; } This currently works in Safari only. .group { scroll-snap-align: center none; }

Slide 113

Slide 113

http://codepen.io/rachelandrew/pen/NbPGYg

Slide 114

Slide 114

Scroll Snapping On the y axis, again supported only by Safari. .gallery { scroll-snap-type: y mandatory; } .group { scroll-snap-align: none start; }

Slide 115

Slide 115

http://codepen.io/rachelandrew/pen/xRbXqr

Slide 116

Slide 116

Beautiful images and text with Blend Modes & Filters

Slide 117

Slide 117

background-blend-mode normal, multiply, screen, overlay, darken, lighten, color-dodge, colorburn, hard-light, soft-light, difference, exclusion, hue, saturation, color, luminosity. .image { background-color: green; background-image: url(my-image.jpg); background-blend-mode: multiply; }

Slide 118

Slide 118

https://codepen.io/rachelandrew/pen/eVRxVW

Slide 119

Slide 119

background-blend-mode Blend more than one image, images plus a color. .image { background-image: url(image.jpg), url(circles-dark.png); background-blend-mode: multiply; }

Slide 120

Slide 120

https://codepen.io/rachelandrew/pen/EQXMjp

Slide 121

Slide 121

mix-blend-mode Blends an object or text with the background. h1 { background-color: rgb(0,0,0); color: #fff; mix-blend-mode: multiply; }

Slide 122

Slide 122

https://codepen.io/rachelandrew/pen/MQEwEK

Slide 123

Slide 123

https://codepen.io/rachelandrew/pen/MQEwEK

Slide 124

Slide 124

filter .blur img { filter: blur(2px); } Add filters to objects with a line of CSS. .brightness img { filter: brightness(0.3); } .contrast img { filter: contrast(250%); }

Slide 125

Slide 125

https://codepen.io/rachelandrew/pen/JprGYg

Slide 126

Slide 126

https://caniuse.com/#feat=css-backgroundblendmode

Slide 127

Slide 127

https://caniuse.com/#feat=css-mixblendmode

Slide 128

Slide 128

https://caniuse.com/#feat=css-filters

Slide 129

Slide 129

https://ilyashubin.github.io/FilterBlend/

Slide 130

Slide 130

Adding delight with Animation

Slide 131

Slide 131

http://animista.net/

Slide 132

Slide 132

http://valhead.com/

Slide 133

Slide 133

CSS Transforms & Animations have excellent browser support.

Slide 134

Slide 134

The Web Animations API

Slide 135

Slide 135

https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API

Slide 136

Slide 136

http://rachelnabors.com/

Slide 137

Slide 137

Many font styles one font file with Variable Fonts

Slide 138

Slide 138

Variable fonts ▸ Part of the OpenType font file specification ▸ Available as .otf or .ttf files ▸ In CSS use the font-variation-settings property ▸ CSS Fonts Level 4 Spec adds additional values to existing font properties

Slide 139

Slide 139

https://caniuse.com/#feat=variable-fonts

Slide 140

Slide 140

https://www.axis-praxis.org

Slide 141

Slide 141

Getting our hands on all the new shiny

Slide 142

Slide 142

Let browser vendors know you want these features

Slide 143

Slide 143

https://developer.microsoft.com/en-us/microsoft-edge/platform/status/shapes/?q=shapes

Slide 144

Slide 144

https://rachelandrew.co.uk/speaking/event/uxi18 Thank you