-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathABR-ResponsiveSVG-RWDSummit2015.html
More file actions
683 lines (657 loc) · 25.9 KB
/
Copy pathABR-ResponsiveSVG-RWDSummit2015.html
File metadata and controls
683 lines (657 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
<!DOCTYPE html>
<html><head>
<title>Scaling Up (and Down) SVG</title>
<meta charset="utf-8" />
<style media="all">
h1, h2, h3, header, footer {
text-align: center;
}
li {
margin-bottom: 0.5em;
}
li li {
margin-bottom: 0.1em;
}
ul, ol {
margin: 0.25em 0;
}
header li, footer li {
display: block;
}
header li li, footer li li{
font-size: inherit;
margin: 0.5em auto;
}
header ul, footer ul {
display: block;
padding: 0;
}
footer, section {
position: relative;
counter-increment: slide;
}
footer::before, section::before {
content: counter(slide);
display: block;
position: absolute;
top: 0;
right: 0;
padding: 0.5em;
height: 1em;
width: 1em;
color: gray;
}
</style>
<style media="print">
header, section, footer {
border-bottom: double dimgray 5px;
padding: 1em 0;
break-inside: avoid;
}
a:link, a:visited {
color: inherit;
text-decoration: none;
}
a:link::after {
content: " (" attr(href) ")";
}
a.literal::after {
content: normal;
}
</style>
<style media="screen">
body {
background-color: #666;
margin: 0; padding: 0;
font-family: Verdana, Tahoma, sans-serif;
}
header, section, footer {
display: block;
box-sizing: border-box;
width: calc(96vw - 20px);
min-height: 96vh;
padding: 1em;
border-radius: 1em;
margin: 2vh 2vw;
background-image: linear-gradient(to bottom right, lightYellow 20%, darkSeaGreen);
box-shadow: 0.2em 0.3em 0.5em 0 black;
}
ul {
padding-left: 3em;
}
@media (min-height: 40em) {
header, section, footer {
height: 96vh;
}
section > ul, footer > ul {
height: calc(100% - 5em);
overflow: auto;
}
header ul {
height: calc(100% - 10em);
overflow: auto;
}
}
@media (min-width: 30em) {
:root {
font-size: 150%;
}
li li {
font-size: 80%;
}
}
@media (min-width: 40em) AND (min-height: 40em) {
:root {
font-size: 200%;
}
}
.control {
display: block;
position: fixed;
background-color: rgba(100%, 100%, 75%, 0.5);
font-size: 80%;
padding: 0.25em 0.5em;
bottom: 0;
right: 0;
z-index: 1000;
}
.control input[type="checkbox"] {
height: 1em;
width: 1em;
border-radius: 25%;
box-sizing: border-box;
font-size: inherit;
}
:not(a):focus {
outline: none;
border-right: solid seaGreen;
}
</style>
<style id="interactive-styles" disabled="disabled" media="only none">
:focus ~ li, :focus + ul, .contains-focus ~ li, .contains-focus + ul {
opacity: 0.05;
}
li ul {
height: 0;
opacity: 0;
overflow: hidden;
transition: opacity 0.5s;
}
li:focus ul, .contains-focus > ul {
height: auto;
opacity: 1;
}
</style>
</head><body>
<header>
<h1>Scaling Up (and Down) SVG</h1>
<h2>Using Scalable Vector Graphics on Responsive Web Pages</h2>
<ul>
<li>Amelia Bellamy-Royds</li>
<li>RWD Summit 2015</li>
<li>12 March 2015</li>
<li>Follow along with demos: <a class="literal" href="http://codepen.io/collection/njbPRv/">http://codepen.io/collection/njbPRv/</a></li>
</ul>
</header>
<section>
<h2>Responsive SVG</h2>
<ul>
<li>Chris Coyier covered:
<ul>
<li><em>What</em> is SVG?</li>
<li><em>Why</em> is SVG good for RWD?</li>
</ul>
</li>
<li>This talk: <em>how</em> to use SVG in responsive sites
<ul>
<li>How to make your SVG look good at any size</li>
<li>First: SVG scaling basics</li>
<li>Then: Advanced scaling effects</li>
</ul>
</li>
<li>I <em>won't</em> cover using JavaScript to re-scale graphics
<ul>
<li>Slows down your page</li>
<li>Can't be used in SVG images</li>
</ul>
</li>
<li>I <em>will</em> talk about
<ul>
<li>Core SVG scaling options</li>
<li>Using CSS media queries</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Images vs. Documents</h2>
<ul>
<li>Graphics and text respond to screen size differently</li>
<li>Images: constant aspect ratio
<ul>
<li>shrink width = shrink height</li>
<li>expand width = expand height</li>
</ul>
</li>
<li>Text: constant area <small>(approximately)</small>
<ul>
<li>shrink width = expand height</li>
<li>Text wraps onto new lines</li>
</ul>
</li>
<li>SVG scales like an image
<ul>
<li><strong>But only if you say so!</strong></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Scaling SVG Basics: the <code>viewBox</code></h2>
<ul>
<li>Most RWD:
<ul>
<li>Browser has a certain size</li>
<li>You adjust to fit</li>
</ul>
</li>
<li>SVG <code>viewBox</code>:
<ul>
<li>You declare what size your graphic is designed for</li>
<li>The browser scales to fit</li>
<li><a href="http://codepen.io/AmeliaBR/pres/PwdVWL?editors=110">DEMO</a></li>
</ul>
</li>
<li>No <code>viewBox</code> = no scaling:
<ul>
<li>Works like CSS absolute positioning</li>
<li>Percentages scale, nothing else</li>
</ul>
</li>
<li>WT# is a <code>viewBox</code>?
<ul>
<li>Attribute on the <code><svg></code></li>
<li>Four numbers: x, y, width, height</li>
<li>x,y: top-left corner coordinates</li>
<li>width,height: # of px to fit in available space</li>
</ul>
</li>
<li>Can I haz <code>viewBox</code>?
<ul>
<li>Some graphics programs will create automatically (Inkscape won't)</li>
<li>Use px for your dimensions</li>
<li><code><svg viewBox="0,0 w,h" ></code></li>
<li>For x,y: use 0,0 (if you're using graphics program)</li>
<li>For w,h: use the width & height in px</li>
<li>LIVE DEMO: Getting a <code>viewBox</code> in Inkscape</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>SVG Images</h2>
<ul>
<li>SVG Image = SVG used as an image file
<ul>
<li>HTML <code><img></code> or <code>CSS background-image</code></li>
<li>Most layout/scaling issues work the same with HTML <code><object></code></li>
<li>LIVE DEMO: Using your Inkscape SVG in a web page</li>
</ul>
</li>
<li>Size the image with CSS, browser handles the scaling
<ul>
<li>LIVE DEMO: No viewBox = no scaling in IE</li>
<li>LIVE DEMO: No height, width, viewBox = no scaling anywhere</li>
<li>LIVE DEMO: No height, width = arbitrary default sizes</li>
</ul>
</li>
<li>What if you don't know the aspect ratio?
<ul>
<li>If aspect ratio doesn't match, browser centers and fits</li>
<li>If you only specify height <em>or</em> width, other dimension auto-scales</li>
</ul>
</li>
<li>The center/fit behavior is controlled by <code>preserveAspectRatio</code> attribute
<ul>
<li>E.g.: <code>preserveAspectRatio="xMinYMax slice"</code></li>
<li>E.g.: <code>preserveAspectRatio="none"</code></li>
<li><a href="https://css-tricks.com/scale-svg/">Find out more</a></li>
</ul>
</li>
<li>Make image responsive!
<ul>
<li>Percentages or viewport units</li>
<li>Media queries</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Inline SVG</h2>
<ul>
<li>Inline SVG = SVG markup inside your HTML file
<ul>
<li>Same document, same stylesheets</li>
<li><code><svg></code> element is both canvas and content</li>
</ul>
</li>
<li>If creating your SVG in a graphics program:
<ul>
<li>Give it a viewBox</li>
<li>Optimize it</li>
<li>PHP / other server-side includes</li>
<li>AJAX for browser caching</li>
<li>Good old copy & paste</li>
<li><a href="http://codepen.io/AmeliaBR/pres/PwdVWL?editors=110">Same Demo, edit it!</a></li>
</ul>
</li>
<li>Set <em>both</em> height and width with CSS
<ul>
<li>& make responsive!</li>
<li>no viewBox = no scaling, in any browser</li>
<li>height & width attributes are defaults only</li>
<li>LIVE DEMO: playing around with CodePen</li>
</ul>
</li>
<li>If you set height <em>or</em> width
<ul>
<li>Some browsers adjust according to viewBox</li>
<li>Others don't (IE, Safari, older Chrome, really old Firefox)</li>
<li>Use the <code>padding-bottom</code> hack to scale height according to width</li>
<li><a href="https://css-tricks.com/scale-svg/">Find out more</a></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Icons & Symbols</h2>
<ul>
<li>SVG <code><symbol></code> element = reusable graphic</li>
<li>Draw a symbol with a <code><use></code> element
<ul>
<li>The use element can be in a different SVG in the same document</li>
<li>Specs: the use element could be in a different document</li>
<li>Real world: no IE support, styles buggy in other browsers</li>
</ul>
</li>
<li>Use AJAX to get your symbols into your main document
<ul>
<li><em>If</em> they are used on many pages</li>
<li>Or, directly include in HTML markup</li>
</ul>
</li>
<li>Symbols must be within an <code><svg></code> element
<ul>
<li>Insert symbols in first SVG (the won't draw directly)</li>
<li>Put symbols in their own SVG, then hide with CSS</li>
<li>Don't use <code>display: none</code> (older Firefox bugs)</li>
</ul>
</li>
<li>The <code><symbol></code> has a <code>viewBox</code>, so your SVG doesn't need one</li>
<li>Inline markup is short & sweet
<ul>
<li><code><svg class="icon"><use xlink:href="#icon" /></svg></code></li>
<li>The symbol will stretch to fit the SVG</li>
<li><a href="http://codepen.io/AmeliaBR/pres/GgXzJY">DEMO</a></li>
</ul>
</li>
<li>Change icon size using CSS height and width on the svg.icon
<ul>
<li>Must set both height and width, in all browsers</li>
<li>Use classes if icons have different aspect ratios</li>
<li>Tip: Use <code>em</code> units for easy scaling with <code>font-size</code></li>
</ul>
</li>
<li>End of SVG Scaling Basics:
<ul>
<li>QUESTIONS?</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Media Queries and SVG</h2>
<ul>
<li>Slimming Down when you're Scaling Up</li>
<li>The viewBox scales <em>everything</em> equally
<ul>
<li>All units</li>
<li>Font size</li>
<li>Stroke (lines/outline) width</li>
</ul>
</li>
<li>Result: overbearing text, thick lines
<ul>
<li><a href="http://codepen.io/AmeliaBR/pres/NPLvPz">DEMO</a></li>
</ul>
</li>
<li>Media queries can cancel out the effect
<ul>
<li>Make <code>font-size</code> or <code>stroke-width</code> <em>smaller</em> as SVG gets <em>larger</em></li>
<li>Text and strokes will still scale up, <em>but not as much</em></li>
<li>Consider accessibility: Don't make things <em>too</em> small</li>
<li><a href="http://codepen.io/AmeliaBR/pres/NPLvPz">SAME DEMO, new options</a></li>
</ul>
</li>
<li>What is the media for SVG?
<ul>
<li>Image or object: the SVG region only</li>
<li>Inline: the entire page/frame</li>
<li>Always: the area for the entire <em>document</em></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Selective Scaling</h2>
<ul>
<li>Instead of media query breakpoints, just <em>don't scale</em> some parts</li>
<li>No viewBox = no scaling
<ul>
<li>Nested SVG elements (<em>with</em> viewBox) contain the parts you want to scale</li>
<li>But no viewBox = no auto-scaling aspect ratio for images</li>
</ul>
</li>
<li>Percentages scale position but not size
<ul>
<li>Can't do complex shapes with percentages</li>
<li>Weird for dimensions not linked to width/height</li>
<li>No aspect ratio control</li>
<li>Requires math</li>
</ul>
</li>
<li>Best for map/chart annotations
<ul>
<li><a href="http://codepen.io/AmeliaBR/pres/KwBjeB">DEMO</a></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Shrink & Simplify</h2>
<ul>
<li>Media queries remove / replace parts of graphic as you scale down</li>
<li>Large, medium, & small logos in one image file
<ul>
<li>Not an image sprite!</li>
<li>Works as an HTML image or as CSS background image</li>
</ul>
</li>
<li>Streamline shapes, simplify effects, remove text
<ul>
<li><a href="http://codepen.io/AmeliaBR/pres/jEvYXj">DEMO</a></li>
</ul>
</li>
<li>Media queries <em>inside</em> the SVG file
<ul>
<li>In a <code><style></code> block</li>
<li>Main page CSS only controls the image width/height</li>
<li><a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/91525/logo-details.svg">View source</a></li>
</ul>
</li>
<li>Media queries <em>cannot</em> change aspect ratio
<ul>
<li>viewBox is not a CSS property</li>
<li>This <em>may</em> change in SVG 2</li>
</ul>
</li>
<li>Other layout control is limited to CSS transforms
<ul>
<li>This <em>will</em> change in SVG 2</li>
</ul>
</li>
<li>To significantly change design, show/hide separate versions</li>
<li>More examples & explanation:
<ul>
<li><a href="http://tympanus.net/codrops/2014/08/19/making-svgs-responsive-with-css/">Sara Souiedan on Codrops</a></li>
<li><a href="http://www.smashingmagazine.com/2014/03/05/rethinking-responsive-svg/">Ilya Pukhalski in Smashing Magazine</a></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Complex Scaling Patterns</h2>
<ul>
<li>Inline SVG & no viewBox = CSS control of aspect ratio</li>
<li>Nested SVGs and percentages for scaling</li>
<li>Details dropped / swapped with media queries</li>
<li>Complexity is up to you
<ul>
<li>but it does get complex!</li>
<li><a href="http://codepen.io/AmeliaBR/pres/bNxYRW?editors=110">DEMO</a></li>
<li><a href="http://codepen.io/AmeliaBR/pres/adJtq?editors=110">More examples</a></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>SVG & Text Wrapping</h2>
<ul>
<li>SVG text is absolutely positioned
<ul>
<li><em>Not</em> text-in-a-box</li>
<li>Does not wrap to a new line if it runs out of room</li>
<li>This <em>will</em> change in SVG 2</li>
</ul>
</li>
<li>Need to explicitly position multi-line text
<ul>
<li><a href="http://codepen.io/AmeliaBR/pres/wBxdpM">Hypothetical Demo for line breaks controlled with media queries</a></li>
<li>Does not work in Firefox</li>
</ul>
</li>
<li>Practical solution: Two versions of text, and swap</li>
<li>For lots of text: use HTML
<ul>
<li>Absolutely positioned divs over top of SVG</li>
<li>SVG <code><foreignObject></code> element: no IE support yet, buggy in other browsers</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Summary: Basics</h2>
<ul>
<li>For most SVG, <code>viewBox</code> is all you need</li>
<li>For SVG-as-image (or object):
<ul>
<li>Use <code>viewBox</code> + <code>width</code> + <code>height</code> attributes on the root <code><svg></code> element</li>
<li>Use preserveAspectRatio to control alignment when image size doesn't match</li>
<li>Browser will auto scale height to match width or vice versa</li>
</ul>
</li>
<li>For large inline SVG:
<ul>
<li>Use <code>viewBox</code> on the <code><svg></code> element</li>
<li>Use CSS for height and width</li>
<li>Use a padding-bottom hack if necessary to get height to scale with width</li>
</ul>
</li>
<li>For inline SVG icons:
<ul>
<li>Use <code><symbol></code> element with <code>viewBox</code> attribute</li>
<li>Inline markup: <code><svg class="icon"><use xlink:href="#icon" /></svg></code></li>
<li>Use CSS classes/em units to set height and width</li>
</ul>
</li>
<li>More examples & explanation:
<ul>
<li><a class="literal" href="https://css-tricks.com/scale-svg/">https://css-tricks.com/scale-svg/</a>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Summary: Advanced</h2>
<ul>
<li>Use media queries:
<ul>
<li>To make text and strokes <em>smaller</em> (relatively) as SVG gets <em>larger</em></li>
<li>To remove extra details or swap in simplified versions</li>
<li>With breakpoints based on the <em>document</em> size</li>
</ul>
</li>
<li>Use nested SVGs:
<ul>
<li>To create complex scaling effects</li>
<li>If you want to get fancy</li>
<li>With <em>no</em> <code>viewBox</code> on outer SVG</li>
<li>With percentages for position & sizing of nested SVG</li>
<li>With percentages for position of non-scaling content</li>
</ul>
</li>
<li>Use SVG 2:
<ul>
<li>To make this all easier!</li>
<li>…eventually.</li>
</ul>
</li>
</ul>
</section>
<footer>
<h2>About Me</h2>
<ul>
<li>Amelia Bellamy-Royds</li>
<li>B.Sc., M.J., SVG Evangelist</li>
<li>amelia.bellamy.royds@gmail.com</li>
<li><a class="literal" href="http://codepen.io/AmeliaBR/">http://codepen.io/AmeliaBR/</a></li>
<li style="padding-top: 2em">I write Books!</li>
<li><a href="http://shop.oreilly.com/product/0636920032335.do"><cite>SVG Essentials, 2nd Edition</cite></a></li>
<li>Coming Soon: <cite>Using SVG with CSS3 and HTML5</cite></li>
</ul>
</footer>
<script>
(function(){
var initialized = false;
var interactiveStyles = document.getElementById("interactive-styles");
var headingSelector = "h1, h2, h3",
pointsSelector = "section > ul > li, section > ol > li, footer > ul > li";
var toggleLabel = document.createElement("label");
toggleLabel.setAttribute("class", "control");
document.body.id ="body";
toggleLabel.setAttribute("aria-controls", "body");
toggleLabel.textContent = "Tab to reveal";
var toggle = document.createElement("input");
toggle.setAttribute("type", "checkbox");
toggle.addEventListener("change", toggleTabControl);
toggleLabel.insertBefore(toggle, null);
document.body.insertBefore(toggleLabel, document.body.firstChild);
function addTabIndex(selector) {
var points = document.querySelectorAll(selector);
for (var i=0, n=points.length; i<n; i++) {
points[i].setAttribute("tabIndex", 0);
}
return points;
}
//add tabIndex to headings
//scroll slide into place whenever heading gets focus
var headings = addTabIndex(headingSelector);
for (var i=0, n=headings.length; i<n; i++){
headings[i].addEventListener("focus", scrollSlide);
}
function toggleTabControl(e){
if (!e.target.checked){
interactiveStyles.setAttribute("disabled", "disabled");
interactiveStyles.setAttribute("media", "only none");
//because "disabled" doesn't work!
document.body.insertBefore(interactiveStyles, null); //force refresh
return;
}
if (!initialized) {
//add tabIndex to bullet points
addTabIndex(pointsSelector);
//ensure that focusing links doesn't mess up focus styles
var links = document.getElementsByTagName("a");
for (var i=0, n=links.length; i<n; i++) {
links[i].addEventListener("focus", focusIn);
links[i].addEventListener("blur", focusOut);
}
initialized = true;
}
interactiveStyles.removeAttribute("disabled");
interactiveStyles.setAttribute("media", "screen");
document.body.insertBefore(interactiveStyles, null); //force refresh
}
function focusIn(e){
var p = e.target.parentElement;
while ((p.tagName.toLowerCase() != "section") &&
(p != document.documentElement)) {
p.classList.add("contains-focus");
p = p.parentElement;
}
}
function focusOut(e){
var p = e.target.parentElement;
while ((p.tagName.toLowerCase() != "section") &&
(p != document.documentElement)) {
p.classList.remove("contains-focus");
p = p.parentElement;
}
}
function scrollSlide(e){
scrollTo(window.scrollX, e.target.parentElement.offsetTop - 10);
}
})();
</script>
</body></html>