html - 在中间设置 3 个 div 容器

标签 html css

我现在创建了 3 个 Div 容器。每个容器的宽度为 300px + 2*4px 每个边框(右和左)。 div 容器位于一个部分中,该部分的宽度为 1200px。结果现在每个容器之间有 92px 的空间。 (1200px - (308px*3))/3 = 92px。但是,如果我将 margin-left 设置为 92px,则空间不正确并且容器不在该部分的中间。这是 myPage和 代码:

*{
    margin: 0px;
    padding: 0px;
}

section{
    width: 1200px;
    margin-left: auto;
    margin-right: auto;
    background-color: #00ff00;
    overflow: hidden;
}


.divbox{
    height: 300px;
    width: 250px;
    border: 4px dashed black;
    border-radius: 10px;
    float: left;
    margin-left: 92px;
}

.divbox:after{
    float: none;
}
<html>
    <head>
        <title>Startseite</title>
        <link rel="stylesheet" href="index.css">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <section>
            <article>
                <div class="divbox">
                    content
                </div>
                <div class="divbox">
                    content
                </div>
                <div class="divbox">
                    content
                </div>
            </article>
        </section>
    </body>
</html>

最佳答案

你的数学有问题。例如,您的宽度是 250px 而不是 300px。当有 4 个空间需要平均处理时,您将除以 3。所有空格之间的差异为 106.5(向下舍入):

enter image description here

.divbox{
    height: 300px;
    width: 250px;
    border: 4px dashed black;
    border-radius: 10px;
    float: left;
    margin-left: 106px;
}

EXAMPLE 1

但不是使用 float 和边距来使它们居中,而是使用 display: inline-block 然后设置 text-align: center 在父 部分 上:

section{
    width: 1200px;
    margin-left: auto;
    margin-right: auto;
    background-color: #00ff00;
    overflow: hidden;
    text-align: center; //add
}


.divbox{
    display: inline-block; //add instead of float
    height: 300px;
    width: 250px;
    border: 4px dashed black;
    border-radius: 10px;
    margin: 0 50px;
}

EXAMPLE 2

关于html - 在中间设置 3 个 div 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27990805/

相关文章:

javascript - Html 选择多个在 onchange 事件中获取所有值

javascript - JQuery/CSS 父子继承 NIGHTMARE

javascript - 您可以将 .resize() 绑定(bind)到 $(document) 而不是 $(window) 吗?

javascript - Javascript中绝对旋转元素的环绕矩形和高度

html - 如何在用户名和密码表单之间留空

jquery - React-Redux 中的简单背景颜色更改

javascript - keyup 函数突出显示正在输入的值

jquery - 具有适当文本对齐的列表样式和元素符号效果

html - Mozilla Firefox 中缺少 CC 按钮

html - 可折叠的 CSS 左侧列和自动扩展的右侧列...垂直扩展?