html - CSS 计算宽度与指定宽度不同

标签 html css flexbox

如图所示bin为预览按钮指定的宽度为 120 像素,而呈现的宽度始终为 132 像素(在不同的屏幕上)。

.btn-grey{
    cursor: pointer;
    background: #f1f3f6;
    border: 1px solid #d7d9e1;
    box-shadow: 1px 1px 1px #fff inset;
    border-radius: 3px;
    color: #838AAB;
    display: inline-block;
    height: 30px;
    padding: 0 5px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
            <div style="display: flex; width: 60%; justify-content: center;">
                <input name="preview" class="btn-grey" value="Preview" style="flex-basis: 120px; width: 120px; height: 35px;max-width: 120px; max-height: 35px; margin : 5px 5px 5px 5px " title="preview"/>
                <input name="load" class="btn-grey" type="submit" value="Load " style="width: 120px; height: 35px;max-width: 120px; max-height: 35px;margin : 5px 5px 5px 5px "/>
            </div>

</body>
</html>

最佳答案

只需在按钮样式中添加 box-sizing: border-box;

这将确保宽度和高度属性(以及最小/最大属性)包括内容、填充和边框,但不包括边距。

有关 box-sizing 的更多信息,请查看以下链接 https://www.w3schools.com/cssref/css3_pr_box-sizing.asp

关于html - CSS 计算宽度与指定宽度不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47347764/

相关文章:

javascript - 不允许刷新网页

css - 如何使用css将froala设置为计算高度

html - 创建一个下半部分一种颜色和上半部分另一种颜色的div

html - 表单标签不会增长以填充 flexbox?

php - 使用 PHP、JS、CSS 和 HTML 创建类似跳棋的实时 Web 应用程序?

javascript - 有没有办法兄弟非事件 div?

html - 固定 flexbox 侧边栏中的可滚动内容

html - flex 标签到底是什么意思?

javascript - 使用 JavaScript 或 Jquery 链接到过滤后的 html

iPhone 网页版,推荐px 还是%?