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 8352879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:05:55+00:00 2026-06-09T09:05:55+00:00

Cannot Detect Margin with JavaScript DOM While It Is Set in CSS: <div> <div

  • 0

Cannot Detect Margin with JavaScript DOM While It Is Set in CSS:

<div>
  <div id="elem">

  </div>
</div>


#elem{
   margin:0;   
}

console.log(document.getElementById('elem').style.marginLeft)     //""
console.log(document.getElementById('elem').style["margin-left"]) //undefined
console.log($('#elem').css('margin-left'))                        //0px

What’s wrong?
What is the bullet-proof way to certainly detect margin without setting it via DOM?
Why does jQuery detect?
Can anyone explain in technical details why margin cannot be detected so I have an understanding?

http://jsfiddle.net/SBznP/1/

  • 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-09T09:05:56+00:00Added an answer on June 9, 2026 at 9:05 am

    Please remember, that CSS and DOM are different parts. Changes in CSS won’t change DOM content and vice-versa.

    This means, that element.style.marginLeft (DOM) returns the current value of the style attribute’s marginLeft property.

    Using document.defaultView.getComputedStyle(element, '').getPropertyValue('margin-left') you can get the computed value instead.

    PS: That last way is -sadly- not compatible with some of the older browsers. You might be interested in a cross-browser version: http://cross-browser.com/x/lib/view.php?s=xGetComputedStyle

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

Sidebar

Related Questions

After this weekend my Wicket application cannot correctly detect javascript anymore. It doesn't work
I cannot find a simple example about my question above: how can i detect
I cannot detect if the scrollbars of a form are visible. Googleing the Internet
I'm making an application running multiple webcams/cameras. I cannot manage to detect the number
Possible Duplicate: How come millisUntilFinished cannot detect exact CountDownTimer intervals? Why does using the
The Notes section in the function documentation of ignore_user_abort() suggest that PHP cannot detect
i had a javascript to detect user broswer width and want to pass the
Cannot seem to loop through the XElement and build a datatable. My XElement will
Cannot dump Stack Overflow XML file into SQL Server 2008. Could you please explain
Cannot find any example on Google.

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.