Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing media specific CSS properties from Javascript

I have a CSS property (font) that I need to be able to change from Javascript (a pulldown). However, this font should only be used when printing (@media print).

So, the javascript can't just change the value of the font, because that will effect the screen view as well. Is there a way to change ONLY the print version of the font property?

Alternatively is there a way to have a CSS property be a reference to another property?

That way, in the print CSS, I could say font:printfont, and in the screen CSS font:12. And then change the value of printfont, and it would only change the font when printing.

thanks.

EDIT: The point is that I need to be able to change the font size that the document gets printed at from the pulldown, but I don't want to change the font size that the document gets displayed at.

like image 878
Brian Postow Avatar asked Apr 28 '09 15:04

Brian Postow


People also ask

Can JavaScript change CSS properties?

With JavaScript, we are able to set CSS styles for one or multiple elements in the DOM, modify them, remove them or even change the whole stylesheet for all your page.

How use CSS media query in JavaScript?

Using Media Queries With JavaScriptThe window. matchMedia() method returns a MediaQueryList object representing the results of the specified CSS media query string. The value of the matchMedia() method can be any of the media features of the CSS @media rule, like min-height, min-width, orientation, etc.

How do I override a media query in CSS?

To override a specific media query rule, append a new css rule after the one you want to override. For example, if the last css rule does not have a media query attached, it will override all previously declared media queries (presuming the same selectors).

Can you use CSS custom properties in media queries?

Well... if you're using CSS custom properties (CSS variables) and wanted to reference them in a media query, you probably discovered that you can't use custom properties in this context. CSS custom properties weren't designed for that use case.


2 Answers

That's an interesting dilemma you have going on there. Off the top of my head, the only thing I can think of is to add a new tag to the header where your font-size is declared with !important. For example, in your head tags:

<style type="text/css" media="print">

.printfont {
    font-size: 16px !important;
}

</style>

This will ensure that the new font-size will take precedence.

The following is a very quick example of how you may accomplish this with javascript

<script type="text/javascript">

    var inlineMediaStyle = null;

    function changeMediaStyle ()
    {
        var head = document.getElementsByTagName('head')[0];
        var newStyle = document.createElement('style');
        newStyle.setAttribute('type', 'text/css');
        newStyle.setAttribute('media', 'print');
        newStyle.appendChild(document.createTextNode('.printFont { font-size: 16px !important;}'));

        if (inlineMediaStyle != null)
        {
            head.replaceChild(newStyle, inlineMediaStyle)
        }
        else
        {
            head.appendChild(newStyle);
        }
        inlineMediaStyle = newStyle;
    }

</script>

Just ensure that you have onchange="changeMediaStyle()" as an attribute on your dropdown. Also, as a disclaimer in my example, I am not accounting for things like memory leaks, so you will have to work out those kind of issues on your own.

As to your alternate question, as far as I am aware, there isn't any method for declaring/using what is essentially CSS variables. However, there is currently a recommendation out there for it: http://disruptive-innovations.com/zoo/cssvariables/

like image 162
Jordan S. Jones Avatar answered Sep 30 '22 05:09

Jordan S. Jones


seems like what you want to do is myabe just change or add a class to the item with JS

<p class="inrto comicSans">this is the text to change</p>

@screen p.intro {font-family:verdana;}

@print p.comicSans {font-family:comic-sans;}
like image 23
nickmorss Avatar answered Sep 30 '22 06:09

nickmorss