0

Initial Research

I am aware of using .css() to get and set the CSS rules of a particular element. I have seen a website with this CSS:

body, table td, select {
    font-family: Arial Unicode MS, Arial, sans-serif;
    font-size: small;
}

I never liked Arial Unicode as a font. Well, that was my personal feel. So, I would use Chrome's Style Inspector to edit the Arial Unicode MS to Segoe UI or something which I like. Is there anyway, other than using the following to achieve the same?

Case I

$("body, table td, select").css("font-family", "Segoe UI");
  • Recursive, performance intensive.
  • Doesn't work when things are loaded on the fly.

Case II

$('<style>body, table td, select {font-famnily: "Segoe UI";}</style>')
    .appendTo("head");
  • Any other better method than this?
  • Creates a lot of <style> tags!
Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    I think you're looking for the CSS OM: http://dev.w3.org/csswg/cssom/ – millimoose Nov 17 '12 at 13:46
  • May I ask: Are you doing this because of personal preference (just your own browser)? Or do you have access to the page itself (Are you the web dev)? Or is this for an extension that everyone will be using? – Joseph Nov 17 '12 at 13:46
  • possible duplicate of [JavaScript access CSS class by its name?](http://stackoverflow.com/questions/13020792/javascript-access-css-class-by-its-name) – Bergi Nov 17 '12 at 13:47
  • @Bergi Seriously, are you out of your minds? Can you please explain, in which way does it actually **solve** or provide my answer? I am unable to find any. Do you find something? – Praveen Kumar Purushothaman Nov 17 '12 at 14:04
  • @JosephtheDreamer Good question. Right now, yeah, as I said before, it is for my personal preference, but can be made scalable, by putting them in UserScripts or something. In one site, I am the developer, and not in others. It is the application of Gerrit Code Review, FYI! – Praveen Kumar Purushothaman Nov 17 '12 at 14:07
  • Guys, am not a newbie and have made some initial research befotr posting this question. Please be considerate in giving close or downvotes. Thanks. – Praveen Kumar Purushothaman Nov 17 '12 at 14:12
  • @millimoose Dude, thats awesome. But just someone thought about it and is still in draft. If that comes out, it will solve a lot of things I guess, what say? So trying to do it in current implementation. Lets see. – Praveen Kumar Purushothaman Nov 17 '12 at 14:14
  • Adding to the question, I also wanna know how the browser tools (Chrome Dev Tools / Firebug) achieve this. – Praveen Kumar Purushothaman Nov 17 '12 at 14:27
  • @PraveenKumar: In the possible duplicate the question was asked "*How to get the style rule objects for a selector?*", like you want to get them for `body, table td, select`. The answer was "No, you have to search (recursively) through the stylesheet objects, there is no query function". Just overwriting them (your case II) would be much easier and the downsides are minimal. – Bergi Nov 17 '12 at 14:38
  • @PraveenKumar If you read that document's intro, you'll see the CSS OM is a continuation / cleanup of [DOM Level 2 Style](http://www.w3.org/TR/DOM-Level-2-Style/) to access stylesheet data. A lot of it should already work fairly well. – millimoose Nov 17 '12 at 14:42
  • @Bergi I guess you got me wrong. I know the style rules. I don't want to get the CSS, but to set the CSS by rule and not inline. On the other hand, I feel you got it too! `:P` – Praveen Kumar Purushothaman Nov 17 '12 at 16:23
  • @PraveenKumar: No, you don't have the style rule object. What you know is the selector it uses - but there is no easy way to get the style declaration objects to change them (as you confirmed in a comment on my answer). Overwriting it by adding a new ` – Bergi Nov 17 '12 at 16:37
  • @Bergi Yup. I know. Now the problem is, I don't wanna introduce new ` – Praveen Kumar Purushothaman Nov 17 '12 at 16:58
  • 1
    @PraveenKumar: Good question (ask a separate one!). Afaik, they just insert a new ` – Bergi Nov 17 '12 at 18:03

2 Answers2

5

Ok, if:

  • Personal Preference

    Then use user styles CSS. According to priority, user styles takes precedence above all other styles. Next comes inline-styles, then !important styles, then specificity, then default browser styles. If it's just for personal preference, pack-up a custom CSS with you and a browser that supports it.

  • You are the developer

    Then don't do this in JavaScript or user scripts. Go down to the problem and change those styles! You are just making the browser work more by actually making it parse stuff you don't want. Since you have access to the code, change the styles instead.

    However, since your site could be a public site, style it like genericly for everyone. It's not only you that's viewing the site.

  • Not the developer:

    The best way I can think of (and already did this before*) is to remove external stylesheets from the page and replace them with modded versions of your own liking. That is taking a copy of the original CSS file, change what needs to be changed, and then load it via JS by creating a <link> to that external file, or load the contents via AJAX and put them in a <style>. However, this approach is riddled with obstacles. <link> does not have an onload event so you won't know the external CSS was loaded (there are crafty workarounds though) and AJAXing CSS contents imply that your CSS is in the same domain or the browser and server supports CORS.

    Another way you can do it is to have JS peek into loaded stylesheets and modify their contents. This is a more JS intensive work since JS looks for your definition to change in a pile of CSS declarations. This is a safer bet, however, I believe not all browsers can traverse CSS styles or at least do it differently across browsers. Also, if you got a large stylesheet, you'd be parsing it every time.

    As you said, .css() would be recursive. True, because it applies inline styles to each affected element. This is good in a sense that inline styles are higher up in the priority so whatever is placed using .css(), you are almost sure that they will take effect. However, it's more work intensive since it involves the DOM now.

* The library I created does not remove existing styles, it just loads and unloads predefined styles declared using it's API

Joseph
  • 117,725
  • 30
  • 181
  • 234
1

Have a look at:

I'm afraid there is no library for that, I really would like to see one...

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375