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

  • SEARCH
  • Home
  • 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 8855161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:04:11+00:00 2026-06-14T14:04:11+00:00

For example I might have some css stuff that looks like this: .divType1 {

  • 0

For example I might have some css stuff that looks like this:

.divType1 {
    position: absolute;
    width: 60px; height: 60px;
    left: 400px; top: 100px;
    border: 1px solid #89B;
    z-index: 0;
 }

Now within Javascript I want to gather div class divType1′ css attributes, but am provided only with the div class, so I can’t do something of form ( pseudo-code ):

selectDivWithClass( divType1 ).getCss(left). 

I could hack something by instantiating a div with class divType1 and grab its css attributes, and then destroy it, but is there a better way?


The original question has some ambiguity, resulting in lots of good answers that will work in other settings. So here’s the restated one:

Using Javascript, how do I gather a subset of a div class’ css attributes specified by the maker in the stylesheet, so no browser defaults. Furthermore, assume only ‘plain-vanilla’ attributes will appear in the css. So stuff like width, height, not stuff like:

transition: width 2s;
-moz-transition: width 2s; /* Firefox 4 */
-webkit-transition: width 2s; /* Safari and Chrome */
-o-transition: width 2s; /* Opera */

I am given nothing but the class name, so I am not given a list of attributes I need to collect before hand.

Finally, the function ideally should return an object mapping style attribute to value.

In the scope of what I am trying to do, the problem is solved by @RobG’s answer below.

  • 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-06-14T14:04:14+00:00Added an answer on June 14, 2026 at 2:04 pm

    Inpsecting an element that has the rule applied to it won’t work unless you can filter out the effects of all other rules and default properties (which might be different in different browsers and be the same as a rule property in some but not others). That seems unlikely to be successful in more than one or two browsers in their default configurations.

    To get the CSS rule text as text in a cross-browser fashion, use something like:

    function getCssRule(selector) {
      var sheets = document.styleSheets;
      var rulesObj, rules, ruleText;
    
      if (sheets.length) {
    
        // Determine the name of the rules object
        rulesObj = typeof sheets[0].cssRules != 'undefined'? 'cssRules' : 'rules';
    
        for (var i=0, iLen=sheets.length; i<iLen; i++) {
          rules = sheets[i][rulesObj];
    
          for (var j=0, jLen=rules.length; j<jLen; j++) {
            if (rules[j].selectorText.indexOf(selector) > -1) {
              return rules[j].cssText;
            }
          }
        }
      }
    }
    

    Note that this will return the actual text of the rule.

    Also, there is a CSSValue property, but I am unsure of support.

    Edit

    If you are dealing only with the rules part and not the selector, you should be able to turn it into an object using something like:

    function cssTextToObj(cssText) {
    
      // Trim selector, initial { and closing } plus whitespace, split on ';'
      var props = cssText.replace(/^[^{]+{\s*/,'').replace(/\s*}\s*$/,'').split(';');
      var resultObj = {};
      var bits;
    
      for (var i=0, iLen=props.length; i<iLen; i++) {
    
        // Split each property assignment on ':' only where
        // props[i] has a value
        if (props[i] != '') {
          bits = props[i].split(':');
          resultObj[bits[0]] = bits[1];
        }
      }
      return resultObj;
    }
    

    You may want to trim the property names and values of leading and trailing spaces, but that should work. I’m no expert on CSS properties though, so the above may need some tweaking.

    Note that if there is a trailing ‘;’ (which browsers seem to add if the original rule didn’t have one), the last member of props will be ” (empty string).

    One thing you absolutely can’t guarantee is the order that properties will be in, not that you can know what order for..in will return them in anyway. IE seems to return cssText in its own order, Firefox seems to preserve the original order.

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

Sidebar

Related Questions

I have got some markup that looks like this: <div class=button> <span class=image></span> <input
EDIT: My example might have created some confusion. I have changed the example below
I have some browser-intensive CSS and animation in my webpage and I'd like to
Suppose I was given a URL. It might already have GET parameters (e.g. http://example.com/search?q=question
I have the following example of what a user might type into a field
For example, many methods in frameworks/JDK might throw java.lang.SecurityException but this is not indicated
I have a problem that might be a simple one, but can't find a
It looks like Adobe Air has some sort of support for jQuery, so I'm
This might seem like a little bit of a nit picky question, but I
I need some advice. (For those who have already read this before I have

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.