Noupe
12:32 PM Noupe | |
| Ways to embed a Clickable SVG-Logo into Your Website Posted: 10 Sep 2012 07:48 AM PDT With the growing number of screen resolutions, devices that support an internet connection (desktop computer, laptops, but also tablets, mobile, TVs, and even your fridge someday), flexibility and scalability has become more and more important for websites. With the new arrival of the Macbook Pro Retina, this went a step further and brought an HiDPI display that was unknown to the market. This means a whole new challenge for web designers who have to find new ways to handle and make images size more flexible to avoid blurry rasterized effects. In this article, we will see how the SVG-format can help us solve this challenge, using an everyday example of a website header with a clickable logo. My personal roadmap to this project: What I wanted to achieveFrom a UX / code point of view, I wanted two things for my logo:
Taking a look at these two things, I ran some tests, and found a few different code variants to achieve this result. They proved not equally successfull though. Following I’ll show you how good the alternatives will perform. Here is a visual of what we will build: You can see a demo here, or fork the code on github You can take a look at this table to see the SVG support in browsers. The SVG logo: what is this format, and how did I create it?SVG stands for Scalable Vector Graphics. It is a markup language that enables us to create two-dimensional graphics that are fully scalable. It is based on an XML syntax, so you can create and open a SVG file in a text editor. Don't panic, you don't need to be a hardcore developer to do so. http://raphaeljs.com/ for example is a very nice library to generate SVG images in an easier way. If you're a designer, you can use your favorite design software to create SVG files. Illustrator and Inkscape will do the job nicely and even Fireworks has a little plugin http://fireworks.abeall.com/extensions/commands/Export/ for this task. Save a SVG logo with Ilustrator using save as > svg inside the format dropdown Just remember a few rules when creating a SVG logo:
To sum it up : Keep your SVG drawing as simple as possible, export it, and test in different browsers to see the result. For my example, I'm using a simple SVG-logo with no gradients and plain colors. (Many thanks to Geeks and the City for letting me kindly use their logo in this example. The logo is their property, I only use it for the example with their consent. You can re-use the code for your projects, but not the logo.) 1. The < object > embed solutionThis is the historical solution to embed SVG in webpages. The big advantage of this method is that the fallback is easy to provide, you simple have to add the .png image in an img tag : The code :
Pros:
Cons :
2. The <svg> tag solution with foreignObject as fallbackSince HTML5, you can use a the new <svg> tag. Remember when I said that SVG was a markup, you can simply copy the markup inside the SVG tag. All you have to do is open the .svg logo in a text editor, and copy paste the code. In the foreignObject, we will include an img-tag with the .png fallback The code
Pros
Cons :
3. The <img> solutions, with different fallbacksA last way of embedding SVG files is to use the img-tag. Note that the tag was not created for this purpose, so you might encounter problems if you use many JavaScripts to modify your SVG object. a. The <img> solution with JavaScript on error-fallbackCredits where due, I found this technique on Tavmjong Bah’s blog (a great read if you want to go deeper into the SVG manipulation techniques using JavaScripts). This solution suggests using the error handler to detect the SVG support, and replace the .svg by a .png logo when the browser does not support SVG and triggers and error. The code
Pros
Cons
b. The <img> solution with a CSS background fallbackFor this technique, we will only provide the SVG image in the HTML, and instead use Modernizr to detect SVG support. If the SVG is supported, the script will add a .svg class to the html, if not, it will add a .no-svg. The HTML code
The CSS code
This one gets a little tricky, let's explain it. In order to make this work without JS activated, we will not use the .no-svg class. First we will hide the image for one simple reason: Internet explorer 8 and browsers that don't support SVG will otherwise display the title of the img. If we put the background on the img-tag, we will see both the background, and the alt-attribute displayed above. Instead, we hide the image, and put the background PNG-file in the a-tag. Now, if SVG is supported, we get the .svg class in the html-tag, and do it all the way around: We display the img-tag, with the SVG in it, and hide the CSS background of the a-tag. Pros:
Cons:
c. The <img> method with modernizR SVG detection to provide data-fallback fallbackIn this method, we will use the new HTML5 data-attributes to provide the fallback file, coupled with modernizr to swap the image for browsers that do not support SVG. The HTML code:
The JavaScript code:
Using modernizr, we detect the SVG-support. If the SVG is not supported, we replace our logo with a .png one, using JavaScript. Pros
Cons
d. The <img> method with JavaScript and < noscript > as fallbackOnce again, we use the image-tag, but this time, we will provide a noscript-tag with the .png-image in it, in case JavaScript is deactivated. The HTML code:
The JavaScript code
We detect SVG-support, swap the image for the SVG-logo if supported, and the PNG if not. Pros
Cons
Going further: font-icon and htaccess-trick for the roadIn this article, I did not mention the font-icon embedding-technique. You can see the Icon Fonts are Awesome page to have a little demo, and find a nice list of icons here. I did not use this technique, because I thought it was not very appropriate and creating a font just for a simple logo was maybe "too much". Also, logos should be images (in the wild sense, I include SVGs into images here), so using a font-icon for a logo on a website was not a solution in my opinion. Also note that you might have to declare the SVG-format in the .htaccess so that the server delivers the correct header. You can do so by adding this to the .htaccess:
More on SVG-usage from around the web:ConclusionIn this article, we saw different techniques to embed a SVG-logo, with a link back to the homepage of the site. I'm not telling you that one method is better than the other, you will have to choose. At the moment, I'm using the last solution on the live website I did this research for, but that's just personal preferences. There must also be other solutions to achieve this and I would be glad to hear about how you manage this challenge, so don't hesitate to share your results in the comments! So, will you try to use some SVG logos on your website soon? What is your favorite technique? (dpe) |
| You are subscribed to email updates from noupe To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
| Google Inc., 20 West Kinzie, Chicago IL USA 60610 | |
0 comments: