html - 为什么 CSS 不恰好将 n 个方 block 放入一个 div 容器中?

标签 html css

<分区>

我正在尝试在 CSS 中创建一个数据框。但是,当我尝试将 5 个 input 正方形(每个 30x30 像素)放入 150x150px 的容器 div 时,但它并不完全适合容器。其中 4 个被放置,最后一个被放置在正下方。我找不到为什么会这样。我该如何解决这个问题?

.box-container {
  width: 150px;
  height: 150px;
  border: 1px solid black;
}
.machine-box {
  color: darkgreen;
  font-size: 10px;
  width: 20%;
  height: 20%;
  float: left;
}
<div class="box-container">
  <input type="text" class="machine-box">
  <input type="text" class="machine-box">
  <input type="text" class="machine-box">
  <input type="text" class="machine-box">
  <input type="text" class="machine-box">
</div>

最佳答案

你应该看看box-sizing属性(property)。默认情况下,它设置为 content-box.machine-box 上的 width: 20%; 不包括输入元素上的边框、边距和填充。

如果您在 .machine-box 上应用 box-sizing: border-box;,边框、边距和填充将成为该 宽度的一部分: 20% 它们会很合身。

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

.container{
    width:500px;
    height: 100%;
    border: 1px solid black;
    display: table;
}
.job-box{
    width: 150px;
    height: 170px;
    float:left;
    border: 1px solid black;
}
.job-title{
    width: 150px;
    height: 20px;
    line-height: 20px;
    font-weight: bold;
    font-size: 16px;
    text-align: center;
    border: 1px solid black;
}
.box-container{
    width: 150px;
    height: 150px;
    border:1px solid black;
}
.machine-box{
    color: darkgreen;
    font-size: 10px;
    width: 20%;
    height: 20%;
    float :left;
    box-sizing: border-box;
}
<div class="container">
    <div class="job-box">
        <div class="job-title">J1</div>
        <div class="box-container">
            <input type="text" class="machine-box">
            <input type="text" class="machine-box">
            <input type="text" class="machine-box">
            <input type="text" class="machine-box">
            <input type="text" class="machine-box">
        </div>
    </div>
</div>

关于html - 为什么 CSS 不恰好将 n 个方 block 放入一个 div 容器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36973354/

相关文章:

javascript - 如何使 jQuery 对话框在 IE8 中的页面加载时不显示?

javascript - 如何使用ajax从div id中提取数据?

javascript - 如何禁用 div 元素内的 anchor 标记 onclick 事件?

css - 如何在less中使用递归定义?

php - 我的 PHP 登录脚本出现以下错误

html - 如何水平对齐 ul 到 div 的中心?

html - 在固定宽度的 div 内将一个固定的 div 居中

javascript - 滚动到元素时创建导航淡入不同的颜色

jquery - 检查用户是否滚动到屏幕的特定百分比

CSS 恢复到定义的样式