Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: adding additional CSS files to current page

Tags:

jquery

css

i have a website that uses screen.css as main CSS file. I added this for monitors with a 1024+ resolution.

$(document).ready(function(){
if(screen.width > 1024) {
    $('link').attr('href','hi-res.css');
}
});

The problem is that hi-res.css is replacing screen.css... i don't want to replace it, i just want to load an additional css in that case. Any ideas? Thanks.

like image 656
Andres SK Avatar asked Jul 02 '10 19:07

Andres SK


People also ask

How to add CSS file dynamically in jQuery?

Using jQueryWith the $. ajax() method, you can load the CSS from the server and enclose the content within the <style> tags, and finally append it at the end of the <head> element of the current document.

How we can add CSS with jQuery?

jQuery Manipulating CSSaddClass() - Adds one or more classes to the selected elements. removeClass() - Removes one or more classes from the selected elements. toggleClass() - Toggles between adding/removing classes from the selected elements. css() - Sets or returns the style attribute.

Can we add CSS in JS file?

JavaScript can also be used to load a CSS file in the HTML document.

Does nginx support CSS?

Save this question.


1 Answers

Append it to your head:

$("head").append("<link id='yournewcss' href='hi-res.css' type='text/css' rel='stylesheet' />");
like image 138
Gert Grenander Avatar answered Oct 03 '22 01:10

Gert Grenander