Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6090301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:11:55+00:00 2026-05-23T12:11:55+00:00

In an example below, I am using @media queries to adjust the size of

  • 0

In an example below, I am using @media queries to adjust the size of a ‘layout’ as the screen resolution changes.

/* styles for small screen and preview */
@media only screen and (min-width: 600px) {
    #layout { width: 500px; height: 500px; }
}

/* styles for large screen */
@media only screen and (min-width: 900px) {
    #layout { width: 800px; height: 800px; }
}

That all works fine and dandy, but I would also like to access the css properties of the smaller layout using jQuery to create a preview of it. Because the preview will require some manipulation (e.g. it will be a smaller percentage of the original height and width), I would like to use jQuery to calculate the dimensions of the preview programmatically (rather than simply hard-code it).

So my question is, is there a way using jQuery to access the css properties of the smaller layout, regardless of screen resolution?

Note: the above example is a simplification of what I need to do in a wider use case, so I’m pretty clear on the need for this. I also need to do this many hundreds of times for different layouts (each with their own style sheets), so ideally want a programmatic way to do this.

Any help much appreciated!

EDIT

For interest, I’ve attached an example following on from Björn’s advice. There may be more elegant ways to do it, but it works for me.

<html>
<head>
<title>Example: accessing @media css properties</title>
<style>
/* default styles for tiny screen */
#preview { background: #ccf; }
#layout  { width: 300px; height: 300px; background: #eee; }

/* styles for small screen and preview */
@media only screen and (min-width: 600px) {
    #layout { width: 500px; height: 500px; background: #cdf; }
}

/* styles for large screen */
@media only screen and (min-width: 900px) {
    #layout { width: 800px; height: 800px; background: #ffc; }
}
</style>
<script src="jquery.js"></script>
<script>
$(function() {
    // first style sheet in page (only one in this example)
    var styles = document.styleSheets[0]; 

    for (i=0;i<styles.cssRules.length;i++) {
        theRules = styles.cssRules[i];
        // type 4 = media rules; media.mediaText = we can pick the media styles we want
        if (theRules.type == 4 && theRules.media.mediaText == 'only screen and (min-width: 600px)') {
            // media rules have their own cssRules collection
            // cssRules[0] = the first rule, i.e. #layout
            var layoutwidth = theRules.cssRules[0].style.width;
            var layoutheight = theRules.cssRules[0].style.height;
            var previewscale = 5;           
            var previewwidth = Math.round(parseInt(layoutwidth )/previewscale);
            var previewheight = Math.round(parseInt(layoutheight)/previewscale);
            // now I can set my preview dimensions      
            $('#preview').width(previewwidth).height(previewheight);            
            console.log('Preview dimensions: ' + previewwidth + ' x ' + previewheight);
        }
    }

});         
</script>
</head>
<body>
<div id="layout">layout
    <div id="preview">preview</div>
</div>
</body>
</html>

Console will show:

Preview dimensions: 100 x 100

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:11:56+00:00Added an answer on May 23, 2026 at 12:11 pm

    You can access the document’s stylesheets through the document.stylesheets object.

    For instance, this will output the first css-rule found in the first stylesheet present on the page:

    console.log(document.styleSheets[0].cssRules[0].cssText);
    

    You could use regular expressions or something on these object to get the set of rules for the declarations you want (once you have determined in which of the document’s styleSheet object they are declared), and apply these rules to a newly created styleSheet ($("<style />") should work.

    I’m not quite sure how you are going to preview the smaller version, so it’s unclear to me how to apply this new styleSheet to the preview, but I hope this is enough to get you started.

    For more info on the styleSheet object, see http://www.javascriptkit.com/domref/stylesheet.shtml

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my example below I'm using a dijit.form.DateTextBox : <input type=text name=startDate dojoType=dijit.form.DateTextBox constraints={datePattern:'MM/dd/yyyy'}
I am using the example below to do a cross-domain ajax call using .net
I have the array example below that I am using to dynamically create an
I'm using the example below: APT command line interface-like yes/no input? I want to
In the example below, the text is selected using jQuery. How can we isolate
I analyzed the sunspots.dat data (below) using fft which is a classic example in
I'm mapping out the name attributes of :input using the example below. I had
I am using the batch file example below that I got from batch file
I am using css media queries on my project to create a site that
I am testing IEEE 754 floating format with VS2008 using the example below: int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.