Why does vw include the scrollbar as part of the viewport?

前端 未结 3 1001
孤城傲影
孤城傲影 2020-11-27 17:53

I\'m trying to build a website that has lots of boxes that are of equal width and height. For example, I have a page that has 2 equal size boxes side by side.

The si

相关标签:
3条回答
  • 2020-11-27 18:09
    html { overflow-x: hidden; }
    

    seems to work

    0 讨论(0)
  • 2020-11-27 18:17

    I have a different answer, and feel the need to share my frustration

    BECAUSE STANDARD-MAKERS ARE STUPID

    (committees, in general, always are)

    One simple (simplicistic) workaround is keeping the scrollbar always around and be dealt with it

    html,body {margin:0;padding:0}
    html{overflow-y:scroll}
    

    (use overflow-x for a layout that uses vh)

    I believe they seriously screwed the pooch on this one.

    0 讨论(0)
  • 2020-11-27 18:19

    It would be convenient if viewport units didn't include cause scrollbars but it is the display size (screen) after all. Have look at this solution with a pseudo element though:

    http://www.mademyday.de/css-height-equals-width-with-pure-css.html

    Makes for a square in your example as well:

    https://jsfiddle.net/3z887swo/4/

    .box {
        float: left;
        width: 50%;
    }
    
    .box::before {
        content: "";
        display: block;
        padding-top: 100%;
    }
    

    Edit - if anyone is wondering why this works (vertical padding responding to the original element's width)... that's basically how it's defined in the specification:

    The percentage is calculated with respect to the width of the generated box's containing block, even for 'padding-top' and 'padding-bottom'.

    http://www.w3.org/TR/CSS2/box.html#padding-properties


    After coming across my own answer, I think it needed some refinement. Semantic ambiguity is why I replaced the word "include" with "cause" at the top. Because it's more the fact that vw units only take the viewport size into account - not including any scrollbar and causing overflow and a scrollbar in the other direction when its width is added to 100vw (making the total space that is needed the viewport plus scrollbar width, exceeding the screen).

    As with the question here, the best way to handle vw units is likely to avoid them if you can because they just aren't very compatible with desktop browser (that don't have overlaying scrollbars).

    I edited out the idea that included a CSS variable, however hopeful it seemed.

    0 讨论(0)
提交回复
热议问题