With jQuery.css() I've been told I can use the following two functions for the same results:
$(".element").css("marginLeft") = "200px"; $(".element").css("margin-left") = "200px";
I've always used marginLeft
as this is what is used in the documentation:
http://api.jquery.com/css/
Shorthand CSS properties (e.g. margin, background, border) are not supported. For example, if you want to retrieve the rendered margin, use: $(elem).css('marginTop') and $(elem).css('marginRight'), and so on.
Why has jQuery allowed for marginLeft
as well as margin-left
? It seems pointless and uses more resources to be converted to the CSS margin-left
?
Left is the position of your entire element, margin-left is the amount of your left margin. For example if your are not in a normal document flow, your left margin is not going to let anything occupy that space.
The margin-left CSS property sets the margin area on the left side of an element. A positive value places it farther from its neighbors, while a negative value places it closer.
margin: 10px 5px 15px 20px; top margin is 10px. right margin is 5px. bottom margin is 15px. left margin is 20px.
Inheriting Margins The inherit value transfers the margins of a parent to a child element. The example below shows that the parent element <div> has a specific value of the left margin, and the child element <p> has the inherit value of the left margin.
jQuery's underlying code passes these strings to the DOM, which allows you to specify the CSS property name or the DOM property name in a very similar way:
element.style.marginLeft = "10px";
is equivalent to:
element.style["margin-left"] = "10px";
Why has jQuery allowed for marginLeft as well as margin-left? It seems pointless and uses more resources to be converted to the CSS margin-left?
jQuery's not really doing anything special. It may alter or proxy some strings that you pass to .css()
, but in reality there was no work put in from the jQuery team to allow either string to be passed. There's no extra resources used because the DOM does the work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With