Cufon: Fixing the damn flickering

4th of July 2010 18

Cufon: Fixing the damn flickering

Cufón is a great piece of javascript that easily let us add any font we would like to our webpages but it comes with its own set of quirks and there are plenty of good and bad ways of fixing them.

cufon fix

Ok, so how to fix it?

Well.. since Cufón has its inbuilt classes for loading, ready and so on we can simple use that together with a bit of CSS:

h1 { text-indent:-999em; }
.cufon-ready h1 { text-indent:0; }

Quite simple right? This efficiently hides the h1 in this case and shows it only when the .cufon-ready class has been added which is after the text has been replaced with Cufón. However the text still can make your content “jump” and feel a bit buggy so…

h1 { min-height: 36px; height: auto !important; height: 36px; text-indent:-999em; }
.cufon-ready h1 { text-indent:0; }

Note that you should match the height with the height that you currently have. You can check it via Firefox extension Web Developer Toolbar but you’ll almost defintely have to adjust it in order to be perfect in IE6/IE7 as well so its suggested (as always…) to have an IE6/IE7-stylesheet with conditional tags in your header.

Filed under § CSS, XHTML — Tagged with , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Share it!

Was it good for you to?

Related posts

Similar posts to this one

Sponsors

Support for the site!


18 great comment(s) for this post

  1. ForceFactor 1:21 am 22/07 of 2010

    I happened to be doing a bit of work-related research in Google today and stumbled on your blog. I’ve clearly put in a little bit of time here reading through and procrastinating! You have some good observations here, so I’ll add you to my personal Google Reader for the future. Enjoy the week!

    Reply
  2. Leon 3:11 pm 27/07 of 2010

    This is bad practice, since people that have javascript disabled will not be able to read your headings.

    Better is to use Cufon.now(); at the end of the document and to keep the fonts .js files small.

    Reply
  3. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 8:22 pm 31/07 of 2010

    @Leon:
    That would be true if we were back in 2008. However the number of users having javascript disabled nowadays is so small so it’s pretty much like doing support for IE5.

    Reply
  4. Efren Lugo 11:48 am 10/11 of 2010

    Tommie is right,

    As many users have javascript enable by default. Is nothing to worry.

    I think only servers had javascript disabled for security reasons.

    Reply
  5. soyo 3:45 pm 22/02 of 2010

    The idea is good – the implementation is wrong. Solution enclosed.

    Leon: “this is bad practice, since people that have javascript disabled will not be able to read your headings”

    Well, potential seo issues moreso – though search engine bots do process js a bit, a search engine could very possibly consider the text referenced in this manner as not visible, therefore not indexable – so potentially not helpful. Also some web appliances do not use JS still either, they would be s.o.l.

    New, the right way:

    Create a separate stylesheet with all of the .cufon-ready statements you need.

    Then throw this into the head section of your page:

        <script type="text/javascript">
        var ss = document.createElement("link");
        ss.type = "text/css";
        ss.rel = "stylesheet";
        ss.href = "/path-to-your-new-stylesheet.css";
        document.getElementsByTagName("head")[0].appendChild(ss);
        </script>

    Obviously, this uses javascript to build the reference to your new stylesheet… so, if javascript is off, no cufon-ready applied – all text still visible. If it’s on – cufon flicker is gone.

    Have a great day!

    Soyo

    Reply
  6. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 4:07 pm 22/02 of 2010

    @soyo: Nice solution, however using append and manipulating the DOM in such fashion surely will have an impact on performance. It’s a nice other way to do it though. :)

    But wouldn’t it be easier just to use the noscript tag straight of?

    Reply
  7. soyo 5:02 pm 22/02 of 2010

    Given all the code sites are running today, the impact is very minimal IMO. But like everything in this field, there’s always a tradeoff! I came down to that code because its as cross browser compliant as I could find and puts my mind at ease, while still solving the cufon problem.

    The problem with noscript is that there is no safe cross browser way of applying it – technically in newest forms of xhtml, it does doesn’t function as expected when you try to bend the rules. I tried different variations of noscript when I started tackling this problem, and every attempt was a cross browser failure with the exception of applying it as an ‘inline style’ fix at the point each element was displayed – not a great solution. :-(. It’s really not designed to be used for global style references, primarily due to the head vs. body issue which adds to the problems.

    I would be curious for you to try this solution on your end and let me know what you think, I’m always looking for the best way to get things done *the right way*…. or at least, as right as possible ;-)

    Reply
  8. soyo 5:19 pm 22/02 of 2010

    I should also add: I don’t apply cufon fix to every site. Depending on what features are incorporated, and what cufon font styles are being used, I have found that if I can match the default font type closely to the cufon font cross browser, and/or the site shows a minimal flicker effect due to fewer resources applied, I typically leave that alone. But for more progressive sites that reference youtube videos in slideshows, etc., (aka more noticeable cufon flicker on build) and especially those set on using unique cufon fonts, this is the path I’ve been following, (albeit with some browser detection thrown in for good measure)…

    Reply
  9. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 5:51 pm 22/02 of 2010

    @soyo: Your solution do work, haven’t got much time nowadays to check stuff out though.

    Doing it the *right* way is always good, however i feel that Cufón is a bit of a dying breed even though CSS3 has serious rendering issues in my opinion. Or… i haven’t seen anything better that look similar in all browsers… yet.

    Reply
  10. soyo 6:37 pm 22/02 of 2010

    Well maybe a fellow blogger will stumble in, try it out and offer their opinion – who knows we’ll see!

    As for Cufon, I agree 100%. It’s the best imperfect solution of the moment, ready to be unseated… though like many cross-browser technologies with a position of prominence, it will likely be a slow and heavily debated death… ;-)

    Cheers

    Reply
  11. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 6:42 pm 22/02 of 2010

    Yeah.. i’ve been in that debate and still, as i said, @font-face just doesn’t do it to a 100% and often result in jaggy fonts. It’s like a solution that will eventually be fine but just isn’t at the moment. :f

    Cheers

    Reply
  12. soyo 4:39 pm 1/03 of 2010

    By the way, I went back through my notes and there is another option that I wasn’t able to use when I was looking for my own solution, but it might be a more appropriate option here!

    I didn’t even think about it because all of our sites are designed around custom web applications, not semi-static web pages.

    simply add this to your css:

    .cufon-loading {
                    visibility: hidden;
    }

    This hides the entire display, then spits it out completely built. In IE, it means a second or two of nothing, then wham, there’s your page.

    Of course, it eliminates the DOM issue, as well as the missing text with JS off issue.

    I can’t use this effectively, because our custom web apps load in segments – so we need to have certain elements appear as the page loads. (If we did use this, the pause in IE becomes way too long).

    But it could be an effective alternative in the more mainstream environment of semi-static web pages, and it’s a lot easier to implement than the solution I ended up with for my own needs.

    Can’t speak for cross browser on this one, as I abandoned it before I got to that point.

    Cheers

    Reply
  13. soyo 4:40 pm 1/03 of 2010

    Huh, my last post didn’t come out right – I must have mistyped something… maybe you can fix it?

    Reply
  14. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 4:51 pm 1/03 of 2010

    @soyo: Yes, that’s an old method though.

    The problem with it is that if you got something that loads and isn’t needed for the page to work (say a slow call to Google Analytics, Adsense or some bookmarking stuff) it will serious delay the rendering of the page.

    I still find this to be somewhat of a non-issue due to the fact that javascript is just SO.. available. According to W3C 95% of visitors had javascript enabled in the year 2008. That’s 3 years ago. The discussion is nice though. :)

    Btw: You forgot to close your CODE-tag. Fixed it.

    Reply
  15. soyo 6:54 pm 1/03 of 2010

    Yep, and as I described that’s why we were never able to use it for our purposes… ;-(

    But for someone stumbling into this blog, I thought it would be worth mentioning, it can be a good fit for the right sites and it’s not quite as involved as our previous discussion.

    To be honest with you, I actually like the effect better so long as the pause is short – because with all of the methods we’ve discussed to this point, there is still content… and then more content. Even if not an actual font change, it is still a distraction. There is a certain cleanliness to having it all appear at once.

    (On the other hand, the same effect can be created with the first methods we discussed if you wanted, though I’m not sure to what resouce expense).

    As for javascript, for me it’s always about SEO, and making sure the search engines pull what I need. Even though there are some devices that default to JS off, they are going wayside as we type. Whatever methods are used, if seo is a consideration then just make sure the SE’s are able to see what you want them to see and all will be well…

    Cheers…

    Reply
  16. ismail 10:19 pm 12/12 of 2010

    life saver :)

    Reply
  17. Caithlin 7:08 pm 19/01 of 2010

    I’ve been looking everywhere to find a solution to this problem, thanks so much!

    Reply
  18. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 1:38 am 31/01 of 2010

    @Caithlin: Glad i could help. :)

    Reply

Your opinion matters. Add it below.


Tip: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Popular posts this month
  • 5 Random posts
  • jQuery show / hide anything Cufón is a great piece of javascript that easily let us add any font we would like to our webpages ...
  • Cufon: Fixing the damn flickering Cufón is a great piece of javascript that easily let us add any font we would like to our webpages ...
  • Addtoany slow loading Cufón is a great piece of javascript that easily let us add any font we would like to our webpages ...
  • Top posts this month Cufón is a great piece of javascript that easily let us add any font we would like to our webpages ...
  • Simple CSS buttons Cufón is a great piece of javascript that easily let us add any font we would like to our webpages ...

Follow me @design_byme RSS Design Feed

Copyright Tommie Hansen since 2010. All rights reserved. Design and the Absolute Theme by Tommie Hansen.