Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector...

22
Better Themes with Scalable Vector Graphics (SVG) It‘s not just images 17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 1

Transcript of Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector...

Page 1: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

Better Themes with Scalable Vector Graphics (SVG)

It‘s not just images

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 1

Page 2: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

Who am I?

• Bernhard Kau

• Berlin, Germany

• PHP Developer

• WordPress Plugin Developer

• „From image to code“ Frontend Developer

• Organizer of the WP Meetup Berlin

• Blogger on kau-boys.de

• Twitter @2ndkauboy

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 2

Page 3: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

What is a Scalable Vector Graphic?

• It‘s a graphic … at least most of the time

• It‘s a vector format

• It‘s scalable … as large as you want … without a loss of quality

• Technically an XML file

• So it‘s code! Spooky

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 3

Page 4: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

So what does a SVG look like?

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 4

<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" width="900" height="600">

<rect fill="#fff" height="600" width="900"/><circle fill="#bc002d" cx="450" cy="300" r="180"/>

</svg>

Page 5: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

Got it? So what do we see here?

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 5

<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" width="1000" height="1000" viewBox="0 0 32 32">

<rect fill="#f00" height="32" width="32"/><rect fill="#fff" height="6" width="20" x="6" y="13"/><rect fill="#fff" height="20" width="6" x="13" y="6"/>

</svg>

Page 6: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

Anyone? Just kidding

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 6

<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" width="650" height="750" stroke="#000">

<path fill="#FFF" d="m6,6V425a319,319 0 0,0 638,0V6"/><path fill="#F00" d="m11,11v464l114-246 100,246 100-246 100,246 100-246 114,246V11"/><path fill="none" stroke-width="11" d="m6,6V425a319,319 0 0,0 638,0V6z"/>

</svg>

Page 7: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

How to use SVG images?

• Using an <img> tag

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 7

<img src="Flag_of_Japan.svg" alt="Flag of Japan" />

Page 8: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

How to use SVG images?

• Using an <object> tag

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 8

<object type="image/svg+xml" data="Flag_of_Japan.svg"><img src="Flag_of_Japan.png" alt="Flag of Japan" />

</object>

Page 9: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

How to use SVG images?

• Using a <picture> tag

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 9

<picture><source type="image/svg+xml" srcset="Flag_of_Japan.svg"><img src="Flag_of_Japan.png" alt="Flag of Japan" />

</picture>

Page 10: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

How to use SVG images?

• As a CSS background image

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 10

.flag-jp {background-image: url("Flag_of_Japan.svg");

}

Page 11: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

How to use SVG images?

• As a CSS background image with a „Data-URI“

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 11

.flag-jp-base64 {background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaH…');

}

.flag-jp-svg {background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://…');

}

. flag-jp-svg-urlencoded {background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22…');

}

Page 12: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

How to use SVG images?

• Embedding them into the HTML

Note: Do not add

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 12

<div><svg xmlns="http://www.w3.org/2000/svg" width="900" height="600">

<title>Flag of Japan</title><rect fill="#fff" height="600" width="900"/><circle fill="#bc002d" cx="450" cy="300" r="180"/>

</svg></div>

<?xml version="1.0" encoding="UTF-8"?>

Page 13: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

Why should I use SVG?

Scaled PNG at 3.0 „Pixel Density“ SVG

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 13

Page 14: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

Why should I use SVG?

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 14

<img src="Flag_of_Japan_1200x800.png" alt="Flag of Japan"width="1200" height="800"srcset="Flag_of_Japan_1200x800.png 1200w,

Flag_of_Japan_300x200.png 300w,Flag_of_Japan_768x512.png 768w,Flag_of_Japan_1024x683.png 1024w"

sizes="(max-width: 1200px) 100vw, 1200px" />

<img src="Flag_of_Japan.svg" alt="Flag of Japan" />

Page 15: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

Which browser supports SVG?

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 15

Source: http://caniuse.com/svg

Page 16: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

SVG is more than just images!

• A web font format

• A better replacement for icon web fonts

• A way to create image maps

• A container for CSS filters

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 16

Page 17: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

SVG as an icon font replacement

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 17

Icon Font SVG

Rendering May be anti-aliased Just “as is”

CSS Control Only text styles usable CSS font properties + SVG-specific CSS

Positioning Hard, due to line-height, vertical-align, letter-spacing, word-spacing

Easy, it‘s just an ordinary image

Loading Issues CORS headers, Content-Type, broken CDN Just hosted as a regular image

Other Issues Proxy browsers (Opera Mini), User overwrites fonts, blocking of custom fonts

No issue even with proxy browsers

Semantics Pseudo-Elements, <span>, <i>, this is bad An image is an image

Accessibility Text might be tried to read by screen reader Attributes like title, desc, aria-labelledby

Ease of use Not very easy to create custom font Have I mentioned, they are images?

Browser Support Even in IE6 Not in IE 8 and Android 2.3

Page 18: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

SVG as an icon font replacement

WordCamp London 2016 - Sarah Semark

http://triggersandsparks.com/talks/SVG-icons/

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 18

Page 19: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

SVG for image maps

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 19

<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg"

xmlns:xlink="http://www.w3.org/1999/xlink"x="0px" y="0px" width="100%" height="100%"viewBox="0 0 591.504 800.504" overflow="visible"enable-background="new 0 0 591.504 800.504"xml:space="preserve">

<g id="Bundesrepublik_Deutschland"><a xlink:href="/thuerigen" xlink:title="Bundesland Thüringen">

<path id="Thüringen" fill="#B1D1A3„stroke="#284566" stroke-width="0.5"d="M312.004,351.725l1.845-0.401l3.39,0.274l1.912,…"/>

</a>…

</g></svg>

Bundesland Thüringen

Page 20: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

SVG as CSS filters

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 20

.inline-filter {filter: url(#monochrome);

}.external-filters {

filter: url("filters.svg#monochrome");}

<svg xmlns="http://www.w3.org/2000/svg"><filter id="monochrome"

color-interpolation-filters="sRGB"x="0" y="0" height="100%" width="100%">

<feColorMatrix type="matrix"values="1.00 0 0 0 0

0.80 0 0 0 00.65 0 0 0 00 0 0 1 0" />

</filter></svg>

Page 21: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

Sources

• https://www.paciellogroup.com/blog/2013/12/using-aria-enhance-svg-accessibility/

• http://www.iheni.com/just-how-accessible-is-svg/

• https://css-tricks.com/svg-fallbacks/

• https://css-tricks.com/a-complete-guide-to-svg-fallbacks/

• https://css-tricks.com/probably-dont-base64-svg/

• https://css-tricks.com/icon-fonts-vs-svg/

• https://css-tricks.com/color-filters-can-turn-your-gray-skies-blue/

• https://www.image-map.net/

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 21

Page 22: Better Themes with Scalable Vector Graphics (SVG) · PDF fileWhat is a Scalable Vector Graphic? •It‘s a graphic at least most of the time •It‘s a vector format •It‘s scalable

Thank you for your attention!Slides will be posted on kau-boys.com

17.04.2016 Bernhard Kau | @2ndkauboy | kau-boys.de 22