The above graphics are embedded on the page once (and hidden) but then added back in. The top graphic has a CSS:hover selector added to it. Notice the emboss filter added at run-time.
It is possible to create a custom icon font from your vector graphics (such as SVG) through IcoMoon.
All this information came from: http://tympanus.net/codrops/2013/11/27/svg-icons-ftw/
The viewport is the area you carve out on a webpage for the artwork to be displayed in. To set the Viewport, you tweak the width and height attributes of the SVG tag. e.g. <svg width="100" height="200">
alternatively, you can set it with CSS. e.g. <svg style="width:100%;">
In these, the viewport size is modified.
The viewBox is similar in concept to Adobe Illustrator’s artboard; it is the internal canvas upon which the artwork is drawn. In these, the viewport is set to a square, and the viewBox is tweaked.
The preserveAspectRatio attribute of the <svg> tag takes the mandatory "align" value and an optional "meet or slice" value (separated by a space). The first (align) value can be:
The second value of the preserveAspectRatio can be “meet”, “slice”, or “none” and is needed when the first value is not "none".
Changing the width and height attributes affects the overall size on the screen. By default the viewBox will attempt to fit entirely within the viewport and it will scale proportionally.
Changing the viewBox is like changing Illustrator’s Artboard. This has a more drastic effect on the appearance of the artwork.
If the above fails, perhaps wrapping the SVG in a container than maintains its aspect ratio will be a better solution.
It is possible to put an image map on a responsive image when one uses SVG. This process is described here.
Codrops has more information about Responsive SVG.
I created a demo of dynamically sized graphics here.
If you have a large photograph that you want alpha transparency on, you can do that using SVG. Basically you have to create an 8-bit PNG mask and lay it on top of the JPEG. This technique is described in detail here.
<svg preserveAspectRatio="xMinYMin" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 560 1388"> <defs> <mask id="canTopMask"> <image width="560" height="1388" xlink:href="img/can-top-alpha.png"></image> </mask> </defs> <image mask="url(#canTopMask)" id="canTop" width="560" height="1388" xlink:href="can-top.jpg"></image> </svg>
Smashing Magazine describes how SVG filters can be used to make display text.
http://www.smashingmagazine.com/2015/05/26/why-the-svg-filter-is-awesome/
Filters are also available in CSS, but there is some sharing with SVG. See Color Filters Can Turn Your Gray Skys Blue.
Drop shadows can be achieved using a filter
.
<filter width="125%" height="125%" x="-12.5%" y="-12.5%" id="dropShadow"> <feOffset dx="6" dy="6" in="SourceAlpha" result="offOut" /> <feGaussianBlur in="offOut" stdDeviation="6" result="blurOut" /> <feComponentTransfer in="blurOut" result="lightenOut"> <feFuncA type="table" tableValues="0 0.4" /> </feComponentTransfer> <feBlend in="SourceGraphic" in2="lightenOut" /> </filter> <circle fill="#2FA7D1" cx="208" cy="608" r="200" filter="url(#dropShadow)" />
filter sets up the filter’s canvas compared to whatever the filter is applied to.
feOffset creates and moves the new object around on the stage. Tweak dx
and dy
to position the shadow. Since the in
attribute specifies SourceAlpha, it will only copy the alpha channel, not all the colors. This effectively makes an all black copy—black is good for drop shadows, so yay.
feGaussianBlur makes that black copy blurry (to mimic a shadow). Tweak the stdDeviation
to adjust bluriness.
feFuncA will tweak the alpha channel. Because the black, blurry copy is so dark, this is used to adjust the opacity. Tweak the second number in tableValues
to adjust the opacity.
feBlend with a “normal” mode
will simply stack the original graphic on top of the drop shadow object.
The Sitepoint article on SVG Accessibility contains several tips directly related to accessibility, but also has some interesting comments related to SVG in general, such as:
<svg version="1.1" width="300" height="200">...</svg>
<a xlink:href="http://example.com">
SVG’s <use> tag allows you to reference just a part of some SVG defined elsewhere and draw just that part somewhere else. However, you must use inline SVG to dynamically use only certain pieces. If you want to use SVG as a CSS background-image or in an <img> tag, then you can exploit fragment identifiers. It is a bit like using intra-page anchors (# syntax).
This information came from How SVG Fragment Identifiers Work. The concept here is to:
<view id="icon-clock-view" viewBox="0 0 32 32" /> <view id="icon-heart-view" viewBox="0 32 32 32" /> <view id="icon-arrow-right-view" viewBox="0 64 32 32" />
<img src="sprite.svg#icon-heart-view" alt=""> -or- .icon-clock { background: url(sprite.svg#icon-clock-view) no-repeat; }
It is super easy to make a stylized button when it is rectangular, but if you want an abstract shape then SVG provides a great solution.
<svg>
element only needs the viewbox
attribute (although you will often want an id
attribute too).<a id="Star1" xlink:href="http://example.com/">
In the second star I changed the fill color so you can see what happens a bit easier on the rollover. SVG strokes are always centered over their path.