div 标签未正确对齐

发布于 2024-12-10 12:56:53 字数 1409 浏览 0 评论 0原文

我在 div 标签上遇到了一个奇怪的问题,实际上这些 div 标签是对齐并分组在一个容器中的,但问题是这里真正愚蠢的是我的代码,

<html>
<head>
<style>
    body {
        padding: 0 2%;
    }
    #footer {
     width: 100 %;
        clear: both;
        border: 1px solid #CCC;
    }
    #footer .col_one {
        float: left;
        width: 25%;
        border: 1px solid #CCC;
    }
    #footer .col_two {
        float: left;
        width: 25%;
        border: 1px solid #CCC;
    }
    #footer .col_three {
        float: left;
        width: 24%;
        border: 1px solid #CCC;
    }
    #footer .col_four {
        float: left;
        width: 25%;
        border: 1px solid #CCC;
    }
</style>
</head>
<body>
    <div id="footer">
        <div class="col_one">
            &nbsp; something here
        </div>
        <div class="col_two">
            &nbsp; something here
        </div>
        <div class="col_three">
            &nbsp; something here
        </div>
        <div class="col_four">
            &nbsp; something here
        </div>
        <div style="clear: both">
        </div>
    </div>
</body>
</html>

现在在 #footer .col_third 中,当我设置宽度时到 24% 时,它们会按正确的顺序显示所有框,但后来我将其设置为 25%,最后一个框正好位于第一个框下方,为什么会这样?总页脚容器为 100%,有四个 div 框,每个宽度为 25%,我哪里错了,希望您能理解,并对英语不好表示歉意。

I get a weird problem with the div tags actually these div tags are aligned and grouped in a single container but the problem something really stupid here is my code

<html>
<head>
<style>
    body {
        padding: 0 2%;
    }
    #footer {
     width: 100 %;
        clear: both;
        border: 1px solid #CCC;
    }
    #footer .col_one {
        float: left;
        width: 25%;
        border: 1px solid #CCC;
    }
    #footer .col_two {
        float: left;
        width: 25%;
        border: 1px solid #CCC;
    }
    #footer .col_three {
        float: left;
        width: 24%;
        border: 1px solid #CCC;
    }
    #footer .col_four {
        float: left;
        width: 25%;
        border: 1px solid #CCC;
    }
</style>
</head>
<body>
    <div id="footer">
        <div class="col_one">
              something here
        </div>
        <div class="col_two">
              something here
        </div>
        <div class="col_three">
              something here
        </div>
        <div class="col_four">
              something here
        </div>
        <div style="clear: both">
        </div>
    </div>
</body>
</html>

Now in the #footer .col_three when I make the width to 24% they display all the boxes in right order but then I make it 25% the last one goes just below the first box why is that so? The total footer container is 100% and there are four div boxes made 25% each in width where am I wrong hope you understand and sorry for bad english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不乱于心 2024-12-17 12:56:53

这是因为您设置的宽度是内容的宽度,不包括填充、边距和边框。所以总宽度是 100% + 8px(4 个 div 每边 1px 边框)。因此它不会适合。

您可以通过在 div 中添加额外的 div 来解决此问题。您制作的外部 div 宽度为 25%,无边框。内部 div,您可以给出边框,但没有明确的宽度,因此它将填充其父级:

http: //jsfiddle.net/GolezTrol/fm7LV/1/

我冒昧地通过在这些 div 上放置单独的类来稍微修改选择器: col one 而不是col_one。这样,您可以对所有列使用 .col 选择器,并在需要设置特定列样式时使用 .col.one 选择器。 :)

That's because the width you set, is the width of the content, excluding padding, margin and borders. So the total width is 100% + 8px (for 1px borders on each side of 4 divs). Therefor it won't fit.

You can solve this by putting extra divs in the div. The outer div you make 25% wide without borders. The inner div, you can give a border, but no explicit width, so it will fill its parent:

http://jsfiddle.net/GolezTrol/fm7LV/1/

I took the liberty of modifying the selectors a little by putting separate classes on those divs: col one instead of col_one. That way, you can use the .col selector for all columns and the .col.one selector in case you need to style a specific column. :)

み零 2024-12-17 12:56:53

去掉边框,它们应该可以工作。浏览器根据百分比计算每个 div 的宽度应有的像素数。当您添加额外的像素(带有边框)时,div 的总宽度大于窗口宽度,并且它们需要向下移动

#footer{
    width: 100 %;
    clear: both;
}
#footer .col_one,.col_two,.col_three,.col_four{
    width:25%;
    float:left;
}

Take out the border and they should work.The browser calculates the amount of pixels each div should be in width from the percentages. When you add extra pixels (with a border) the total width of the divs is then more then the window width and they need to shift down

#footer{
    width: 100 %;
    clear: both;
}
#footer .col_one,.col_two,.col_three,.col_four{
    width:25%;
    float:left;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文