Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery css() behaviour

Tags:

jquery

Why these 2 instructions don't produce the same render please ?

$("#j_idt14\\:panelGrid label").css({
    "position": "absolute",
    "width": $("#j_idt14\\:panelGrid label").parent().outerWidth() - 
        parseInt($("#j_idt14\\:panelGrid label").css("padding-left")) - 
        parseInt($("#j_idt14\\:panelGrid label").css("padding-right")) -
        parseInt($("#j_idt14\\:panelGrid label").css("border-left-width")) -
        parseInt($("#j_idt14\\:panelGrid label").css("border-right-width")) -
        24 + "px"
});

and

$("#j_idt14\\:panelGrid label").css("position", "absolute");
$("#j_idt14\\:panelGrid label").css("width", 
    $("#j_idt14\\:panelGrid label").parent().outerWidth() - 
        parseInt($("#j_idt14\\:panelGrid label").css("padding-left")) - 
        parseInt($("#j_idt14\\:panelGrid label").css("padding-right")) -
        parseInt($("#j_idt14\\:panelGrid label").css("border-left-width")) -
        parseInt($("#j_idt14\\:panelGrid label").css("border-right-width")) -
        24 + "px");

in first case, width equals 500px and in 2nd case width equals 394px (as expected).

Thank you for clarification.

like image 589
Olivier J. Avatar asked Feb 24 '26 17:02

Olivier J.


1 Answers

When the position isn't absolute, the j_idt14\\:panelGrid label element is included in the width calculation of the parent element. My guess is that the j_idt14\\:panelGrid label element is wider than the parent.

When the element's position gets set to absolute, its width is ignored in the calculation since it's no longer contained within the boundaries of the parent.

like image 120
Jan Avatar answered Feb 26 '26 05:02

Jan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!