Top Banner
Next-level CSS @rachelandrew #conf
109

Next-level CSS

Jan 09, 2017

Download

Technology

Rachel Andrew
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Next-level CSS

Next-level CSS@rachelandrew #ffconf

Page 2: Next-level CSS

Box Alignment level 3Lining things up with

Page 3: Next-level CSS

This is the vertical-centering module.

Page 4: Next-level CSS

.box { display: flex; align-items: center; justify-content: center; }

<div class="box"> <img alt="balloon" src="square-balloon.jpg"> </div>

Flexbox

Centre the content of .box.

Page 5: Next-level CSS

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

Page 6: Next-level CSS

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

Page 7: Next-level CSS

.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; }

Flexbox

Aligning items within the flex

container

Page 8: Next-level CSS

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

Page 9: Next-level CSS

The box alignment properties in CSS are a set of 6 properties that control alignment of boxes within other boxes.

CSS BOX ALIGNMENT LEVEL 3

https://drafts.csswg.org/css-align/

Page 10: Next-level CSS

CSS BOX ALIGNMENT LEVEL 3

▸ justify-content

▸ align-content

▸ justify-self

▸ align-self

▸ justify-items

▸ align-items

Page 11: Next-level CSS

CSS Grid LayoutA proper layout system with

Page 12: Next-level CSS

.wrapper { display: grid; grid-template-columns: repeat(4, 1fr); } .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; }

CSS Grid Layout

Aligning grid items with the Box

Alignment properties.

Page 13: Next-level CSS

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

Page 14: Next-level CSS

“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.”

CSS GRID LAYOUT

https://drafts.csswg.org/css-grid/

Page 15: Next-level CSS

.cards { display:grid; grid-template-columns: 1fr 1fr 1fr; grid-gap: 10px; }

CSS Grid Layout

Defining a 3 column grid

Page 16: Next-level CSS

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

Page 17: Next-level CSS

.cards { display:flex; flex-wrap: wrap; }

.card { flex: 1 1 250px; margin: 5px; }

Flexbox

Flexbox can wrap flex items onto

multiple rows.

A new row becomes a new flex

container for the purposes of

distributing space.

Page 18: Next-level CSS

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

Page 19: Next-level CSS

.cards { display:grid; grid-template-columns: repeat(auto-fill, minmax(200px,1fr)); grid-gap: 10px; }

CSS Grid Layout

Create as many columns as will fit

into the container with a minimum

width of 200px, and a maximum of

1fr.

Page 20: Next-level CSS

minmax()

Page 21: Next-level CSS
Page 22: Next-level CSS
Page 23: Next-level CSS

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

Page 24: Next-level CSS

.home-hero { display: grid; grid-gap: 1px; grid-auto-rows: minmax(150px, auto); }

minmax()

Rows should be a minimum of 150px

and a maximum of auto

Page 25: Next-level CSS

CSS Grid auto-placement

Page 26: Next-level CSS

<ul class="colors">

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

</ul>

Grid auto-placement

I have a list with all ye olde web safe

colours in it.

Page 27: Next-level CSS

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

}

Grid auto-placement

I auto-fill columns and rows with

minmax()

Page 28: Next-level CSS

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

Page 29: Next-level CSS

.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; }

Grid auto-placement

Adding classes to some elements, by

setting the value of grid-column-end

and grid-row-end to span.

Page 30: Next-level CSS
Page 31: Next-level CSS

.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; }

Grid auto-placement

grid-auto-flow with a value of dense

Page 32: Next-level CSS
Page 33: Next-level CSS

http://gridbyexample.com/browsers/

Page 34: Next-level CSS

display: contentsVanishing boxes with

Page 35: Next-level CSS

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 pseudo-elements in the document tree.

DISPLAY: CONTENTS

https://drafts.csswg.org/css-display/#box-generation

Page 36: Next-level CSS

allow indirect children to become flex or grid items

Page 37: Next-level CSS

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

display: contents

All elements are direct children of

‘wrapper’ except for the li elements.

Page 38: Next-level CSS

.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; }

display: contents

A two column grid. The h1, p and

blockquote with a class of inset are

all direct children.

Page 39: Next-level CSS

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

Page 40: Next-level CSS

.images { display: contents; }

display: contents

The ul has a class of images. By

applying display: contents the ul is

removed and the li elements become

direct children.

Page 41: Next-level CSS

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

Page 42: Next-level CSS

https://www.chromestatus.com/feature/5663606012116992

Page 43: Next-level CSS

CSS ShapesGetting curvy with

Page 44: Next-level CSS

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.

CSS SHAPES

https://drafts.csswg.org/css-shapes/

Page 45: Next-level CSS

.balloon { float: left; width: 429px; shape-outside: circle(50%); }

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

CSS Shapes

A simple shape using the shape-outside property

Page 46: Next-level CSS

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

Page 47: Next-level CSS

.box::before { content: ""; display: block; float: left; width: 429px; height: 409px; shape-outside: circle(50%); }

CSS Shapes

Floating generated content to create

a shape

Page 48: Next-level CSS

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

Page 49: Next-level CSS

.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); }

CSS Shapes

Using clip-path to cut away part of an

image

Page 50: Next-level CSS

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

Page 51: Next-level CSS

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

Page 52: Next-level CSS

Feature QueriesCan I Use this with

Page 53: Next-level CSS

The ‘@supports’ rule is a conditional group rule whose condition tests whether the user agent supports CSS property:value pairs.

FEATURE QUERIES

https://www.w3.org/TR/css3-conditional/#at-supports

Page 54: Next-level CSS

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

Page 55: Next-level CSS

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

Page 56: Next-level CSS

@supports (display: grid) { .has-grid { /* CSS for grid browsers here */

} }

Feature Queries

Checking for support of Grid Layout

Page 57: Next-level CSS

@supports ((display: grid) and (shape-outside: circle())) { .has-grid-shapes { /* CSS for these excellent browsers here */

} }

Feature Queries

Test for more than one thing

Page 58: Next-level CSS

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/

Page 59: Next-level CSS

.balloon { border: 1px solid #999; padding: 2px; }

@supports ((shape-outside: ellipse()) and ((clip-path: ellipse()) or (-webkit-clip-path: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); } }

Feature Queries

Write CSS for browsers without

support, override that and add new

CSS inside the feature query.

Page 60: Next-level CSS

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

Page 61: Next-level CSS

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

Page 62: Next-level CSS

Websites that enhance themselves as the platform improves.

Page 63: Next-level CSS

Initial LetterFancy introductions with

Page 64: Next-level CSS

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.

INITIAL LETTER

https://drafts.csswg.org/css-inline/#initial-letter-styling

Page 65: Next-level CSS

h1+p::first-letter { initial-letter: 4 3; }

Initial Letter

Make the initial letter four lines tall,

one line above the content and 3 into

the content.

Page 66: Next-level CSS

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

Page 67: Next-level CSS

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

Page 68: Next-level CSS

Currently Safari 9+ only.

Page 69: Next-level CSS

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; }

Initial Letter

Adding additional styling to the

initial letter.

Page 70: Next-level CSS

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

Page 71: Next-level CSS

@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; } }

Initial Letter

Using feature queries to test for

support before adding rules that

style the first letter.

Page 72: Next-level CSS

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

Page 73: Next-level CSS

Writing modesUpside down and back to front with

Page 74: Next-level CSS

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

Page 75: Next-level CSS

.wrapper { display: grid; grid-template-columns: auto 1fr; grid-gap: 40px; }

h1 { writing-mode: vertical-rl; transform: rotate(180deg); text-align: right; grid-column: 1; align-self: start; justify-self: start; }

Writing Modes

Using vertical-rl then flipping the text

with a transform.

Page 76: Next-level CSS

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

Page 77: Next-level CSS

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

Page 78: Next-level CSS

Custom propertiesVariables in CSS with

Page 79: Next-level CSS

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.

CSS CUSTOM PROPERTIES (CSS VARIABLES)

https://drafts.csswg.org/css-variables/

Page 80: Next-level CSS

:root { --primary: blue; --secondary: orange; }

h1 { color: var(--primary); }

Custom Properties

Define variables then use them in

CSS

Page 81: Next-level CSS

:root { --primary: blue; --secondary: orange; }

@supports (--primary: blue) { h1 { color: var(--primary); } h2 { color: var(--secondary); } }

Custom Properties

Can be tested for using feature

queries

Page 82: Next-level CSS

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

Page 83: Next-level CSS

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

Page 84: Next-level CSS

calc()Adding things up with

Page 85: Next-level CSS

Basic mathematics in CSS

Page 86: Next-level CSS

<div class="wrapper"> <div class="box box1"> <p>…</p> <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>

calc()

Three boxes, each with a div nested

inside.

Page 87: Next-level CSS

.box2 { height: 140px; }

.box3 { height: 14em; transition: height 0.5s ease; }

.box3:hover { height: 30em; }

calc()

Two of the outer boxes have a height,

box1 is the height of the content

inside.

Box3 will grow on hover.

Page 88: Next-level CSS

.box > div { color: #fff; border-radius: 5px; position: absolute; bottom: 20px; right: 20px; width: 30%; height: calc(50% - 20px); }

calc()

In the CSS for the inner box, we

calculate the height as 50% - 20px.

Page 89: Next-level CSS

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

Page 90: Next-level CSS

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

Page 91: Next-level CSS

position: stickyStaying put with

Page 92: Next-level CSS

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.

POSITION: STICKY

https://drafts.csswg.org/css-position/#sticky-pos

Page 93: Next-level CSS

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

position: sticky

A dl with dt elements followed by

multiple dd elements.

Page 94: Next-level CSS

.authors dt { position: sticky; top: 0; }

position: sticky

Applying position: sticky to the dt

Page 95: Next-level CSS

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

Page 96: Next-level CSS

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

Page 97: Next-level CSS

Scroll snappingSnap to it with

Page 98: Next-level CSS

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

Page 99: Next-level CSS

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

Page 100: Next-level CSS

.gallery { scroll-snap-type: mandatory; scroll-snap-destination: 0 100% ; scroll-snap-points-x: repeat(100%); }

Scroll Snapping

Current Firefox implementation -

spec has since changed!

Page 101: Next-level CSS

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

Page 102: Next-level CSS

.gallery { scroll-snap-type: mandatory; scroll-snap-destination: 0 100% ; scroll-snap-points-y: repeat(100%); }

Scroll Snapping

Current Firefox implementation -

spec has since changed!

Page 103: Next-level CSS

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

Page 104: Next-level CSS

Getting our hands on all the new shiny

Page 105: Next-level CSS

Tell browser vendors you want these features

Page 106: Next-level CSS

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

Page 107: Next-level CSS

Please comment on emerging specifications

Page 108: Next-level CSS

https://github.com/w3c/csswg-drafts/issues

Page 109: Next-level CSS

Thank youhttps://cssgrid.me/ffconf