Home > WebDev > cross browser shadows and fonts with css only

cross browser shadows and fonts with css only

November 1st, 2009 daKmoR

cross browser css only shadow

If you are like me you probably have have seen a million ways of implementing shadows but none seems really serious. I mean there are quite fancy ways of archiving it. You can use a lot of pictures and wrap the content in a lot of divs. You can use javascript to generate millions of spans/divs to display a smooth shadow with html elements and css colors. You can use canvas (all browser except IE) and VML (IE) for this. And I guess there are even more crazy ways of archiving this.

AS you probably have guessed all these methods are fare from being perfect and pretty complicated to use compared to a css only solution. I’m really surprised that I haven’t seen this solution anywhere else in all my researches.

So how does it work. It’s simple as hell – it’s just css!!

/* the following shadow works in IE5.5-8, FF2-3, Safari3, Chrome */
.shadow {
	box-shadow: 3px 5px 6px #888;
	-moz-box-shadow: 3px 5px 6px #888;
	-webkit-box-shadow: 3px 5px 6px #888;
	filter:progid:DXImageTransform.Microsoft.Shadow(color=#888888, direction=135, strength=7);
}

Using that you get a nice shadow in all browser you currently need (means IE, FF, Safari, Chrome). The IE shadow probably looks a little different however it still way better than no shadow or any fancy above solution (in my opinion).
Make sure to check out the DEMO.

Here is comparison between the css only shadows in IE and the rest:
cssOnlyShadowComparision

cross browser css only fonts

Yet again a quite hard to believe mystery. I always thought that using custom fonts on my webpage is next to impossible. And looking at some quite complex technical solutions like:

I was pretty sure there was no easy solution to this problem. Guess what I just found a “css only” solution.

All you need is a *.eot (IE) and a *.ttf (rest) file of your desired font. You can get a bunch of fonts in these formats at http://www.fontsquirrel.com/. All fonts you can find there are 100% free for commercial use. If you want to use a non free front where you can’t get those 2 required formats you can still use on of the above methods.
After you have those two files you simple need to add this css

/* get fonts from: http://www.fontsquirrel.com/ */

@font-face {
	font-family: 'Bloody Normal';
	/* IE */
	src: url('fonts/BLOODY.eot');
	/* for other Browser you can define multiple formats but usually truetype alone is enough */
	src: local('Bloody Normal'), local('Bloody'),
		url('fonts/BLOODY.woff') format('woff'),
		url('fonts/BLOODY.svg#Bloody') format('svg'),
		url('fonts/BLOODY.TTF') format('truetype');
}

@font-face {
	font-family: 'Droid Sans Regular';
	src: url('fonts/DroidSans.eot');
	src: local('Droid Sans Regular'), url('fonts/DroidSans.ttf') format('truetype');
}

h1 { font-family: 'Bloody Normal'; }

Now you can use the font in your css as with the above H1 or you can use it inline with

<p style="font-family: 'Droid Sans Regular';">

This method works again in all tested browsers (Firefox, Internet Explorer, Safari, Chrome).

Just check out the DEMO.

Conclusion

The here provides solutions for shadows and fonts are extremely easy to use and don’t need any complex technical “workaround”. It’s just pure CSS (with specific CSS for each browser group) and still works in all current browsers you need (Firefox, IE, Safari, Chrome). I’m still really surprised to never have heard from this before. Life would have been so much easier if I knew this a year ago. Funny thing is that these solutions work in browser far older than a year. So this solutions could have been used a long time ago but I have never seen it before.

What do you think about these solutions? why aren’t they being used? do people simple don’t know them or is there any problem I didn’t stumbled upon?

Categories: WebDev Tags: , , ,
  1. November 4th, 2009 at 17:24 | #1

    Hi Thomas,

    I found only one problem with your demo in my browser tests. Opera (10.1) sometimes ignores the font and renders it like usual text. IMHO this can be tolerated due to the low market share. I will test it soon myself!

    BTW: You are my personal hero of the day, now! :-)


    Stefan

  2. November 5th, 2009 at 00:07 | #2

    it feels good to be a hero – even if it’s just for one day. :)

    thx for the nice words and yeah I never tested Opera (never said it would work *hehe*) but if you found anything – pls let me know.

  3. November 6th, 2009 at 16:14 | #3

    Hi,

    I have finally tested it myself. Here my resume…

    * Nobody can use this method with any copyrighted font, because it’s not possible to encrypt the font files! Maybe you are lucky and the EULA allowed Web Embedding. Another solution seems to be the eot format which contains some kind of copy protection.

    * Our copyrighted font looked strange on windows machines and all other browsers than Firefox. Only the combination Linux/Fx was acceptable. Maybe a problem with our font…

    * Some older Browsers (Opera < 10, Fx < 3.5, …) doesn't support the font-face tag

    Generally a very disappointing issue. Maybe this resume has answered your question, why it's not used commonly. :-)

    BTW: Some interesting articles and webpages abouth this problem.

    * http://www.alistapart.com/articles/cssatten
    * http://en.wikipedia.org/wiki/Font_embedding_on_the_Web
    * http://randsco.com/index.php/2009/07/04/cross_browser_font_embedding

  4. April 10th, 2010 at 09:26 | #4

    You should update the comment on the shadow one – Opera 10.5 supports it :)

  5. October 11th, 2010 at 16:25 | #5

    That is a nice declare. Deserve besieged branch. Smilee\ymmeebible

  6. October 15th, 2010 at 03:27 | #6

    It’s possible to create a box-shadow that look on IE exactly like the other browsers. It’s just with CSS but with the aid of CSS3PIE http://css3pie.com/

  7. October 15th, 2010 at 13:39 | #7

    thx for the link Ahmad Alfy. Looks like a nice VML solution. So it is a lot of javascript with 28KB but if I would need (I don’t :p) a nicer shadow in IE I would use it. :)

  8. October 17th, 2010 at 17:06 | #8

    I just Stumbled Upon this. Not bad. I’ll give it a thumbs up.

  9. December 24th, 2010 at 22:45 | #9

    I ought to say that it’s my first time viewing your site. I can definitely write that it contains so many useful things. Good website. I just finished mine and i was looking for some ideas and you gave me a few. Great read!

  10. January 6th, 2011 at 08:15 | #10

    It’s people like you that make my day time…

  11. January 6th, 2011 at 20:35 | #11

    I thought it was going to be several boring aged post, but it surely compensated with regard to my time

  12. January 23rd, 2011 at 17:27 | #12

    A great post, thanks for taking the time to share, continued success to your site in the future!

  13. February 2nd, 2011 at 11:01 | #13

    These kind of post will always be inspiring and i choose to read great content so I am happy to locate many good point here in the post, writing is simply great, thank you for the post.

  14. February 5th, 2011 at 03:07 | #14

    Thanks for your contribution. You just helped me pass some time @ work… =P

  15. March 2nd, 2011 at 20:01 | #15

    Cool article it is definitely. I have been seeking for this content

  16. March 6th, 2011 at 15:09 | #16

    It was my contentment getting to your site the other day. I came here now hoping to get something new. And I was not let down. Your ideas in new approaches on this area were enlightening and a wonderful help to my family. Thank you for making time to create these things as well as for sharing your mind.

  17. March 13th, 2011 at 05:54 | #17

    I bookmarked your website !

  18. March 15th, 2011 at 14:10 | #18

    Very Interesting Information! Thank You For Thi Post!

  19. March 16th, 2011 at 12:39 | #19

    Hi….thanks for this good information.I’m so happy because it’s very useful for my thesis research.I hope you will keep updating your content constantly as you have one dedicated reader here :)

  20. March 19th, 2011 at 18:23 | #20

    Just have to thank you for this great information. This is kind of a thing I was looking for (Thanks Bing

  21. March 20th, 2011 at 12:12 | #21

    Dude, you should be a writer. Your article is so good. You should do it for a living

  22. March 21st, 2011 at 01:52 | #22

    Dude, you should be a writer. Your article is really interesting. You ought to do it professionaly

  1. February 8th, 2010 at 12:38 | #1
Comments are closed.