4 Replies - 1281 Views - Last Post: 12 February 2011 - 12:57 PM

#1 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

.css getter

Posted 08 February 2011 - 12:46 PM

Hi. I have set the left-margin of a div to 20px:
div {
margin-left: 20px; }



However when I am using the following jQuery code to extract this margin left property value by using the left property, all I am getting is auto, whereas I expect 20px:
var $m= ($("div").css("left"));



I know that this is a valid property because I have seen a few animation examples in some jQuery books where an element is moved towards the right over a period of time by setting a final position for the left-margin of the element in this way:
var paraWidth = $('div.speech p').outerWidth();
var $switcher = $(this).parent();
var switcherWidth = $switcher.outerWidth();
$switcher.animate({left: paraWidth - switcherWidth,
height: '+=20px', borderWidth: '5px'}, 'slow');


Can anybody tell me how to extract the value 20px?

This post has been edited by cupidvogel: 08 February 2011 - 12:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: .css getter

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: .css getter

Posted 08 February 2011 - 10:53 PM

You haven't actually set the "left" css-property, but merely the "margin-left" css-property.

This should give you the 20px you're after.
$("div").css("margin-left");



Hope it helps! :)
Was This Post Helpful? 1
  • +
  • -

#3 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: .css getter

Posted 09 February 2011 - 02:03 AM

Thanks. It worked. However, I don't understand, why and how do we use just left while acting as a setter (as in line 4 of the jQuery code I provided), but margin-left while acting as a getter. Can you explain?
Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: .css getter

Posted 12 February 2011 - 12:35 PM

left and margin-left are two "completely" different css-properties. Left specifies the elements left position of its offset parent while margin-left specifies its offset from its own left position. :) the left property is dependent on what type of position ( static | fixed | absolute | relative ) the element has, margin-left is not. I'm not really an expert on the matter so my "truth" may not be 100% accurate but it's somewhere along those lines. Hope it helps! :)

View Postcupidvogel, on 09 February 2011 - 10:03 AM, said:

Thanks. It worked. However, I don't understand, why and how do we use just left while acting as a setter (as in line 4 of the jQuery code I provided), but margin-left while acting as a getter. Can you explain?

Was This Post Helpful? 1
  • +
  • -

#5 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: .css getter

Posted 12 February 2011 - 12:57 PM

Thanks. It helped..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1