innerWidth and outerWidth oddness on desktop

前端 未结 3 1057
长发绾君心
长发绾君心 2020-12-02 02:03

Open the console in chrome (whilst on SO) and copy in innerWidth + \"|\"+outerWidth + \"|\" + screen.width, for me this will return 2133|1920|1920,

相关标签:
3条回答
  • 2020-12-02 02:24

    There is a difference between getting of innerWidth and outerWidth. Look at official definitions:

    Window.innerWidth: is Width (in pixels) of the browser window viewport including, if rendered, the vertical scrollbar.

    Window.outerWidth: The outerWidth attribute must return the width of the client window.

    As you can see innerWidth has bound to viewport width, while outerWidth has bound to browser window width.

    Therefore outerWidth can be less than innerWidth when your page is just zoomed in, or page view is scaled up. I think you need to state folloving tag in your page:

     <meta name="viewport" content="width=device-width, initial-scale=1">
    

    It will make you page to behave as expected (fit to width limits of screen) in small viewports.

    And as a possible cause of large innerWidth is the scripts or styles that can change window dimensions.

    0 讨论(0)
  • 2020-12-02 02:31

    One reason innerWidth could be larger than outerWidth is if your browser is zoomed. I got the following results with the browser in fullscreen mode:

    zoom  inner  outer
    75%   1706   1280
    90%   1422   1280
    100%  1280   1280
    110%  1164   1280
    

    The only way I could get outerWidth to be larger than screen.width is by changing the window width by dragging.

    0 讨论(0)
  • 2020-12-02 02:33

    If we take the MDN definition of window.outerWidth:

    Window.outerWidth read-only property returns the width of the outside of the browser window. It represents the width of the whole browser window including sidebar (if expanded), window chrome and window resizing borders/handles.

    And for window.innerWidth:

    The read-only Window property innerWidth returns the interior width of the window in pixels. This includes the width of the vertical scroll bar if one is present.

    Concluding:

    1. The outerHeight and outerWidth take into account the browser window size and not the html visible size. Because of that, the values can change from browser to browser and device to device. Moreover, the values can be larger than the device screen itself.
    2. The innerWidth value includes the scroll if present. This means the width value will not be only referring to the visible part but also the amount of scroll left which could be greater than window.outerWidth.
    0 讨论(0)
提交回复
热议问题