关于html:CSS-Calc误差

CSS - Calc inaccuracy

我有一个ul元素和5个子

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    <ul>

     
    <li>

    </li>

     
    <li>

    </li>

     
    <li>

    </li>

     
    <li>

    </li>

     
    <li>

    </li>


    </ul>

      具有display: flex属性。
      我尝试在

    • 上使用calc属性来平均调整列表项的大小:

      1
      calc:(100% / 5);

      这样可以提供所需的结果,并均匀分配5个

    • 块的大小

      现在,我在所有元素的右侧添加了边框,但最后一个子元素

    • 却添加了边框。因此,我从

        的总宽度中减去了所有边框的总宽度。

        1
        calc:((100% - 8px) / 5);

        这也可以正常工作,并使用边框均匀地调整

      • 块的大小。

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        ul {
          width: 600px;
          height: 300px;
          margin: 0;
          padding: 0;
          display: flex;
          background: red;
        }

        li {
          list-style: none;
          display: block;
          width: calc((100% - 0.8px) / 5);
          height: 100%;
          border-right: 0.2px solid black;
          background: blue;
        }

        li:last-child {
          border-right: none;
        }
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        <ul>

         
        <li>

        </li>

         
        <li>

        </li>

         
        <li>

        </li>

         
        <li>

        </li>

         
        <li>

        </li>


        </ul>

        现在,我尝试在视口单位vw中设置边框宽度,而不是px,但结果却有所不同。

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        ul {
          width: 600px;
          height: 300px;
          margin: 0;
          padding: 0;
          display: flex;
          background: red;
        }

        li {
          list-style: none;
          display: block;
          width: calc((100% - 0.8vw) / 5);
          height: 100%;
          border-right: 0.2vw solid black;
          background: blue;
        }

        li:last-child {
          border-right: none;
        }
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        <ul>

         
        <li>

        </li>

         
        <li>

        </li>

         
        <li>

        </li>

         
        <li>

        </li>

         
        <li>

        </li>


        </ul>

        从上面的代码片段中可以看到,右边有一些空间。视口越宽,它就会变得越大(尝试查看摘要页面)。所以我认为问题出在vw单位和flexbox上。

        So what is the cause of this error?

        编辑:

        从提供的答案和评论中,我已经看到还有其他更合适的方法可以实现我想做的事情。我很欣赏这些答案,但不是我的问题的答案。由于calc在这种情况下显示错误,因此在其他情况下(不仅是边框)尝试使用calc和视口单位时,很可能会引起更多问题。因此,我需要知道原因和" calc"修复程序。


        您无需进行计算即可为li添加内部内容。如果提供box-sizing: border-box;道具,则边框和填充将不会使容器增大。

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        ul {
          width: 600px;
          height: 300px;
          margin: 0;
          padding: 0;
          display: flex;
          background: red;
        }

        li {
          list-style: none;
          display: block;
          width: calc(100% / 5);
          height: 100%;
          border-right: 2px solid black;
          background: blue;
          box-sizing: border-box;
        }

        li:last-child {
          border-right: none;
        }
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        <ul>

         
        <li>

        </li>

         
        <li>

        </li>

         
        <li>

        </li>

         
        <li>

        </li>

         
        <li>

        </li>


        </ul>


        像素(px):绝对像素。因此,例如20px在任何屏幕上实际上都是20个像素。如果监视器的分辨率为1980x1200,并且您将元素的高度设置为200px,则该元素将占用200像素。

        视口的高度/宽度(vw / vh):相对于视口的大小(基本上是浏览器窗口)。

        1px =(100 / document.documentElement.clientWidth)vw
        1像素=(100/500)= 0.2vw


        似乎您正在使用flexli分发到您的ul内部。在这种情况下,您无需calc计算宽度即可使宽度相等。这就是flex应该做的。

        请参阅以下示例:

        1
        2
        3
        4
        5
        6
        7
        8
        9
        * { box-sizing: border-box; margin: 0; padding: 0; }
        ul, li { list-style: none; }
        ul { width: 600px; height: 300px; background: red; display: flex; }
        li {
          flex: 1 0 auto;
          border-right: 0.8vw solid black;
          background: blue;
        }
        li:last-child { border-right: none; }
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        <ul>

         
        <li>

        </li>

        <li>

        </li>

        <li>

        </li>

        <li>

        </li>

        <li>

        </li>


        </ul>

        在容器上指定flex dsiplay时,只需在子项上应用flex属性,以使它们表现为灵活项。在您的特定用例中,您需要以下内容:

      • flex-grow:设置为1。这将使您的li增长为填充整个ul宽度。
      • flex-shrink:设置为0。您不希望li缩小。
      • flex-basis:设置为auto。您希望您的li自动增长到相等的宽度。
      • 因此,您看到根本不需要calc

        您也不必专门将display: block应用于您的li。根据此处的规范:https://www.w3.org/TR/css-flexbox-1/#flex-items

        The display value of a flex item is blockified: if the specified
        display of an in-flow child of an element generating a flex container
        is an inline-level value, it computes to its block-level equivalent

        正如@BoltClock在注释中指出的那样,列表项已被阻塞。

        边框

        现在,来到边界。在第一个示例中,您以0.2px单位指定了宽度。这是没有道理的。尽管规格允许使用小数像素,但它将四舍五入为显示屏上最近的可用像素,通常为1。

        在第二个示例中,您希望边框随着视口缩放。那很好。 0.8vw没问题。

        请看我在这个小提琴中发布的示例。调整结果窗口的大小,您将看到边框相对于视口大小发生变化。

        最后,您可能会或可能不想设置box-sizing。这取决于您的布局。无论您使用什么,都可以持续使用。