2

I would like to know that is there anyway to make changes in .css file using Jquery or javascript. I know, I can dynamically add css properties(or classes) to DOM Element but I don't want that! I want to make changes in .css file to customize css class of .css file.

Please let me know that, is there any easy way for this. Thanks in Advance..!

4
  • 1
    If you explain why you want to do this there may be a better solution to your problem. Otherwise no, you can't with JavaScript directly but maybe if you query a server-side handler this could be possible. Commented Sep 4, 2011 at 12:15
  • @ Marcel: I want to give user's to build a css class (i.e. style of DOM Node) and, that(new style) should be loaded every time..!(the style is gong to change frequently so i want to edit css class of .css file..! Commented Sep 4, 2011 at 12:21
  • What are you using serverside for the most part? PHP, Ruby, etc. Commented Sep 4, 2011 at 22:46
  • ASP.NET, I know I can store the values in database and i can load style from DB. But, worst thing is I am not using any database in this project.!! Commented Sep 5, 2011 at 5:12

7 Answers 7

3

Nope, you can't. You can override the css values with inline one. Because of *.css files are static and placed on the server - you can't bring any changes to the server css file. The only way - use server side of the application (PHP, ASP.NET, etc)

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

3 Comments

Sure, you can work with it like with text files. Probably if you need to substitute some literal (ex. ##bg-color-literal##) - it will work. But for really powerful work in such way - it's a lot of work. Also you can provide modified css file content via HttpHandler
can u provide some example link for that. thanks for this useful comment..!
checkout how to edit a line from a text file using c# for info about editing files and Serving Dynamic Content with HTTP Handlers for providing content via HttpHandler. And don't forget about caching.
1

I Don't think there is any easy way for what are you looking for..!!

Comments

1

Not really.

There's some ActiveX stuff in IE that lets you modify things in the file system but aside from not being a recomended solution, they'd only work in IE anyway.

You can still modify the styles your page uses for selectors in your css files, but you won't actually be modifying the css file itself.

Comments

1

You can't physically change the contents of a file using Javascript in a browser. That's for security reasons, so that content on a web page can't manipulate content on your computer (apart from cookies).

Comments

1

You cannot do it directly within jQuery, but you can load the .css file with jQuery, edit the contents and then send the data to a php or asp.net page which overrides the .css file with the new contents. But this is very dangerous because people can exploit the code and save any data in that .css file unless you have some security procedure but I can't think of any way you can make that work safely.

Comments

1

You can rather have different css files written based on your requirement then change css file using click event which will eventually change the look of ur webpage or a part of your webpage. Here is the solution. http://www.jquery4u.com/dynamic-css-2/jquery-change-css-file-2/

Comments

0

You could generate the CSS server-side and display inline (in the head). That would allow you to serve up styles on a per-user basis.

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.