0

Ok in this case I ONLY focus on speed.

What is faster?

  1. $('#header').css({'background':'#ff0000','width':'100px','height':'100px'});
  2. $('#header').addClass('header');
  3. Just use #header in css

I can't get the answer by using FireBug.

8
  • 7
    The third, obviously ! Commented Aug 28, 2012 at 8:55
  • 1
    @Bondye Because it's not executing any JavaScript. Commented Aug 28, 2012 at 8:57
  • 2
    What is the faster : put a rock on the table, or give this rock to a third person, and ask him to put it on the table ? You're using a useless middleman in both first and second points. Commented Aug 28, 2012 at 8:59
  • Also, addClass() will be faster than css(), jsperf Commented Aug 28, 2012 at 8:59
  • 1
    Mmmh right, I didn't thought that way. But you'll always need a CSS file (well, in 99.99999% of cases), then it'll be faster to use this already-loaded-css than JS. Commented Aug 28, 2012 at 9:03

1 Answer 1

4

Just use Embedded CSS, that is #header in css on our page. If you try to apply CSS using JavaScript, your page may not display correctly if JavaScript on browser is disabled. You may use external CSS file, embedded css on head and the inline CSS.

Always give priority to pure HTML and CSS. Trust me, its faster.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.