How do I retrieve an HTML element's actual width and height?

前端 未结 14 1668
后悔当初
后悔当初 2020-11-22 02:29

Suppose that I have a

that I wish to center in the browser\'s display (viewport). To do so, I need to calculate the width and height of the &l
14条回答
  •  盖世英雄少女心
    2020-11-22 02:44

    Just in case it is useful to anyone, I put a textbox, button and div all with the same css:

    width:200px;
    height:20px;
    border:solid 1px #000;
    padding:2px;
    
    
    
    

    I tried it in chrome, firefox and ie-edge, I tried with jquery and without, and I tried it with and without box-sizing:border-box. Always with

    The results:

                                                                   Firefox       Chrome        IE-Edge    
                                                                  with   w/o    with   w/o    with   w/o     box-sizing
    
    $("#t").width()                                               194    200    194    200    194    200
    $("#b").width()                                               194    194    194    194    194    194
    $("#d").width()                                               194    200    194    200    194    200
    
    $("#t").outerWidth()                                          200    206    200    206    200    206
    $("#b").outerWidth()                                          200    200    200    200    200    200
    $("#d").outerWidth()                                          200    206    200    206    200    206
    
    $("#t").innerWidth()                                          198    204    198    204    198    204
    $("#b").innerWidth()                                          198    198    198    198    198    198
    $("#d").innerWidth()                                          198    204    198    204    198    204
    
    $("#t").css('width')                                          200px  200px  200px  200px  200px  200px
    $("#b").css('width')                                          200px  200px  200px  200px  200px  200px
    $("#d").css('width')                                          200px  200px  200px  200px  200px  200px
    
    $("#t").css('border-left-width')                              1px    1px    1px    1px    1px    1px
    $("#b").css('border-left-width')                              1px    1px    1px    1px    1px    1px
    $("#d").css('border-left-width')                              1px    1px    1px    1px    1px    1px
    
    $("#t").css('padding-left')                                   2px    2px    2px    2px    2px    2px
    $("#b").css('padding-left')                                   2px    2px    2px    2px    2px    2px
    $("#d").css('padding-left')                                   2px    2px    2px    2px    2px    2px
    
    document.getElementById("t").getBoundingClientRect().width    200    206    200    206    200    206
    document.getElementById("b").getBoundingClientRect().width    200    200    200    200    200    200
    document.getElementById("d").getBoundingClientRect().width    200    206    200    206    200    206
    
    document.getElementById("t").offsetWidth                      200    206    200    206    200    206
    document.getElementById("b").offsetWidth                      200    200    200    200    200    200
    document.getElementById("d").offsetWidth                      200    206    200    206    200    206
    

提交回复
热议问题