New CSS Layout Meets the Real World
Rachel Andrew
@
An Event Apart Boston
Slide 2
“The state of the use of Cascading Style Sheet on the web is really
beginning to get boring. Why haven't designers begun exploiting
its benefits yet?
”
Slide 3
I can remember a time not too long ago when individuals were
running amok exploiting the simplest of html tags and creating
works of beauty. But now, after browser vendors have stepped
it up and given us much of the control we've been asking for, I
can't seem to find web designers that are exploiting these new
found powers.
Slide 4
I can remember a time not too long ago when individuals were
running amok exploiting the simplest of html tags and creating
works of beauty. But now, after browser vendors have stepped
it up and given us much of the control we've been asking for, I
can't seem to find web designers that are exploiting these new
found powers.
Chris Casciano
Slide 5
2nd October 2001
Chris Casciano
http://www.chunkysoup.net/opinion/boringcss/
Media Object
▸
contains an image plus content
▸
is flexible
▸
elements should stack on mobile
▸
box should clear the contents
▸
image can be to the left or the right
▸
can be nested
Slide 20
Avoid pre-empting the
need for markup as
styling hooks.
Slide 21
<div class="media">
<h2 class="title">This is my title</
h2>
<div class="img">
<img src="/assets/img/
placeholder.jpg" alt="Placeholder
">
</div>
<div class="content">
</div>
<div class="footer">
footer here
</div>
</div>
Media Object
A parent with a class of ‘media’
Four child elements:
-
title
-
img
-
content
-
footer
auto as flex-basis
Box one, two and three are nested
inside flex-example.
There is another element nested
inside box three.
Slide 37
.flex-example {
display: flex;
}
.flex-example > div {
flex: 1 1 auto;
}
auto as flex-basis
The parent becomes a flex container,
and all direct children are set to grow
and shrink from a flex-basis of
auto
.
Slide 38
no box has a width, and flex-basis is auto and so resolved from the content
Slide 39
.flex-example {
display: flex;
}
.flex-example > div {
flex: 1 1 auto;
}
.two {
width: 350px;
}
.three .inner {
width: 200px;
}
auto as flex-basis
Use auto and the flex-basis will be
taken from any width set on the item.
If there is no width, flex-basis will be
taken from the content width.
Slide 40
width 350px
nested item width 200px
no width
https://cssgrid.me/flex-auto
}
}
Media Object
The grid-template-rows property
defines the rows on the grid.
If we don’t define rows grid will
create them as part of the implicit
grid.
}
Media Object
Flipping the display by creating new
rules for the ‘media-flip’ class.
Slide 45
Slide 46
Media Object
▸
contains an image plus content
✓
▸
is flexible
✓
▸
elements should stack on mobile
✓
▸
box should clear the contents
✓
▸
image can be to the left or the right
✓
▸
can be nested
Slide 47
Slide 48
.media > .media {
grid-column: 2 / -1
;
}
Media Object
If an item with a class of media is the
direct child of an item with a class of
media, start on column line 2.
Line -1 is the final line of the grid.
align-self: start;
}
Half-border Box
The first row of the grid is a minimum
of 30px tall, a maximum of 1fr.
Slide 62
.half-border::before {
content: "";
border-left: 2px solid #000;
grid-row: 1;
grid-column: 1;
}
Half-border Box
A pseudo-element created with
generated content will also become a
grid item.
Slide 63
Slide 64
.half-border .inner {
grid-row: 1 / 3;
grid-column: 1;
margin: 10px;
}
Half-border Box
The inner starts at row line 1 and
ends at row line 3, spanning 2 row
tracks.
grid-column: 1 ;
}
Magazine Style Layout
The items are positioned using line-
based positioning.
Grid items respect z-index so we can
layer items that end up in the same
cells.
Slide 74
Slide 75
.photo-circle {
border-radius: 50%;
}
Magazine Style Layout
Setting border-radius to 50% gives us
a circle.
Slide 76
Slide 77
Fancy headers
with circles
New CSS Meets
Slide 78
Slide 79
<header class="run-header">
<h1>
4.5
Miles
in Berlin,
Germany</h1>
<div class="intro">
<p> </p>
</div>
</header>
Fancy Header
The mark-up for the article header
Slide 80
<header class="run-header">
<h1><span class="distance-wrap">
<span class="distance">4.5</span>
<span class="miles">Miles</span></
span>
<span class="location">in Berlin,
Germany</span></h1>
<div class="intro">
<p> </p>
</div>
</header>
Fancy Header
We need to add some mark-up to
identify the parts of the h1 text we
want to style.
Slide 81
Slide 82
.run-header .distance-wrap {
border-radius: 50%;
width: 5em;
height: 5em;
padding: 0;
background: linear-
gradient(rgba(0,0,0,0),
rgba(0,0,0,0.8)),url(/assets/img/flag-
germany.jpg) center center;
background-size: cover;
margin: 0 auto 1em auto;
z-index: 2;
}
Fancy Header
I use border-radius set to 50% to
make the distance part of the header
a circle.
Slide 83
Slide 84
.run-header .distance-wrap {
display: block;
border-radius: 50%;
width: 5em;
height: 5em;
padding: 0;
background: linear-
gradient(rgba(0,0,0,0),
rgba(0,0,0,0.8)),url(/assets/img/flag-
germany.jpg) center center;
background-size: cover;
margin: 0 auto 1em auto;
z-index: 2;
}
Fancy Header
Setting display to block will mean the
span becomes block level.
Slide 85
Slide 86
.run-header .distance-wrap {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
border-radius: 50%;
width: 5em;
height: 5em;
padding: 0;
background: linear-
gradient(rgba(0,0,0,0),
rgba(0,0,0,0.8)),url(/assets/img/flag-
germany.jpg) center center;
background-size: cover;
margin: 0 auto 1em auto;
z-index: 2;
}
Fancy Header
I use border-radius set to 50% to
make the distance part of the header
a circle.
padding-left: 1em;
margin-left: -1em;
}
.intro {
padding: 0;
}
}
Fancy Header
Floating the distance-wrap class left
means the location comes up
alongside it.
padding-left: 1em;
margin-left: -1em;
}
.intro {
padding: 0;
}
}
Fancy Header
We can use the shape-outside
property with a value of margin-box
to create the curved text.
padding-left: 1em;
margin-left: -1em;
}
.intro {
padding: 0;
}
}
Fancy Header
We can use the shape-outside
property with a value of margin-box
to create the curved text.
Slide 92
Slide 93
Slide 94
Slide 95
Design for the extreme
edges of the experience.
Slide 96
The middle is
constantly changing.
Slide 97
caniuse.com/#feat=css-shapes
Slide 98
CSS has backwards
compatibility built in.
Slide 99
Slide 100
Feature Queries
Slide 101
caniuse.com/#feat=css-featurequeries
Slide 102
Slide 103
@supports (shape-outside: margin-box) {
}
Fancy Header
A Feature Query looks very similar to
Media Queries. Here we test for a
property and value pair.
Slide 104
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/
▸
A component based approach helps to keep this all in check!
margin-left: 0;
}
}
Fancy Header
Outside of the feature query I add a
left margin to the intro.
Inside the feature query we add the
shape-outside property and also
remove that margin.
Slide 106
Fancy Header three ways -
https://cssgrid.me/fancy-headers
Slide 107
Slide 108
Slide 109
@supports(display: grid) {
}
Half-border Box
Using a Feature Query to check for
Grid Layout support
}
}
Magazine Style Layout
Outside the Feature Query add CSS
for non-grid browsers.
Inside write CSS to override plus the
rules for grid layout.
Slide 115
If using display: grid or display: flex
▸
Floated items that become grid or flex items lose their
fl
oat behaviour
▸
vertical-align has no effect on a grid item
▸
Items set to display: inline-block or display: block become grid items
▸
Your overrides mostly will be concerned with changing widths, margins and
padding.
▸
If grid tracks or flex-basis seem to be using a size you didn’t expect,
check
your item widths!
Media Object
The overrides are to remove margins
and widths and the now redundant
generated content for clearing.
Slide 123
Slide 124
Slide 125
<div class="media">
<div class="img">
<img src="/assets/img/placeholder.jpg"
alt="Placeholder
">
</div>
<h2 class="title">This is my title</h2>
<div class="content">
</div>
<div class="footer">
footer here
</div>
</div>
Media Object
Moving the title below the image in
the source.
Slide 126
Slide 127
.media {
display: grid;
grid-column-gap: 20px;
grid-template-areas:
"title"
"img"
"bd"
"ft";
}
Media Object
A single column grid to arrange our
elements for narrow widths.
Slide 128
Slide 129
A variety of Media Objects -
https://cssgrid.me/media-objects
Slide 130
What happened to
vendor prefixes?
Slide 131
Vendor Prefixes
▸
Vendor specific extensions to CSS
▸
Used by browsers to expose
experimental features
▸
Used by developers to use those experimental features
in production
Slide 132
Prefix Removals
▸
The only prefixed Grid is the old IE10 implementation
-ms
prefix
▸
shape-outside is
-webkit
prefixed in Safari and iOS Safari 10
▸
The
-webkit
prefix has been removed from shape-outside, so future Safari
shouldn’t need it
▸
Flexbox is now unprefixed in all browsers
Slide 133
For older browsers
▸
Check
caniuse.com
to see where you still need prefixes
▸
Autoprefixer uses the Can I Use database to add these automatically
▸
Autoprefixer will pre
fi
x very simple grids with the
-ms
version. In simple cases
this may work for you.
▸
Autoprefixer may also make a terrible mess if you have used properties not
part of the IE implementation. Be sure to test!