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

前端 未结 14 1913
后悔当初
后悔当初 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:58

    Flex

    In case that you want to display in your

    some kind of popUp message on screen center - then you don't need to read size of
    but you can use flex

    .box {
      width: 50px;
      height: 20px;
      background: red;
    }
    
    .container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      width: 100vw;
      position: fixed; /* remove this in case there is no content under div (and remember to set body margins to 0)*/
    }
    My div

提交回复
热议问题