网页渲染之谜:按钮太宽

时间:2018-01-21 08:18:16

标签: css

我有一个包含HTML和CSS的文档。 它以一种我不理解的方式呈现。

我用3种不同的浏览器观察到了这一点 (Firefox,Chromium和Opera), 所以它不太可能是一个浏览器错误。

下图显示了我期望看到的内容。 (在拍摄此截图之前,我使用了我的 浏览器的缩放功能可放大。)

enter image description here

下图显示了我实际看到的内容。 (再次,我在拍摄此图像之前放大了。)

enter image description here

上面的按钮组正如我所料。 然而, 在较低的按钮组中, “y”按钮比我预期的要宽。

我马上就会显示文档的标记, 但首先我要谈谈它。 两个“y”按钮之间的区别 是上层的那个 班button-wider-1, 而较低的一个有button-wider-2类。

在CSS中,每个类都设置一个宽度, 使用calc表达式。 两个表达式之间的区别 是button-wider-2的表达式 添加“x”按钮的宽度, 这意味着我期待 “y”按钮的右边框 与...对齐 右侧“x”按钮的右侧边框。 然而, 正如您从上面的第二张图片中看到的那样,这种情况不会发生。 我的问题是:为什么?

这是文件(完全独立):

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>CSS Question</title>

    <style type="text/css">
      .mystery,
      .mystery *
      {
        padding: 0;
        border: 0;
        margin: 0;
      }

      .button-container
      {
        background-color: #f77;
        padding: 1rem;
        margin: 1rem;
      }

      .button
      {
        float: left;
        font-family: 'Courier', monospace;
        width: 3.1rem;
        height: 2rem;
        border: solid black 2px;
        margin-right: 0.7rem;
      }

      .button-wider-1
      {
        /*
          The following 'calc' expression adds up:

            * the standard width of a button (i.e. the width
            of an element with class "button");
            * the width of a button's border;
            * the horizontal margin (i.e. the value of margin-right for a
            button);
            * the width of a button's border;
        */
        width: calc(
          3.1rem +
          2px +
          0.7rem +
          2px
        );
      }

      .button-wider-2
      {
        /*
          The following 'calc' expression is the same as the previous,
          except that there is an extra term at the end, the standard width
          of a button.
        */
        width: calc(
          3.1rem +
          2px +
          0.7rem +
          2px +
          3.1rem
        );
      }
    </style>
  </head>

  <body>
    <div class="mystery">
      <div class="button-container">
        <button type="button" class="button">x</button>
        <button type="button" class="button">x</button>
        <div style="clear: both"></div>
        <button type="button" class="button button-wider-1">y</button>
        <div style="clear: both"></div>
      </div>

      <div class="button-container">
        <button type="button" class="button">x</button>
        <button type="button" class="button">x</button>
        <div style="clear: both"></div>
        <button type="button" class="button button-wider-2">y</button>
        <div style="clear: both"></div>
      </div>
    </div>
  </body>
</html>

我在以下3个浏览器中观察到了这种意外的渲染 (全部在Linux上):

  • Firefox ESR 52.5.2
  • Chromium 57.0.2987.98
  • Opera 50.0.2762.58(最新版本的 歌剧在写作时)

1 个答案:

答案 0 :(得分:2)

您正在通过添加按钮的宽度和边框宽度来计算按钮的宽度。但是,现在浏览器默认为按钮上的属性border-boxbox-sizinghttps://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

快速解决方法是将box-sizing: content-box;添加到.button,以便实际情况符合您对计算宽度的期望。

另一个解决方法是从第二个+2px删除两个calc();边框是按钮宽度的一部分,您只需要第二个按钮为“2个按钮+ 1个边距”。第一个按钮没有改变,因为你实际上希望它是“1个按钮+ 1个边距+ 2个边框”,因此它可以同时使用边框和内容框。