用CSS换行

发布于 2024-11-25 04:40:08 字数 819 浏览 6 评论 0原文

我连续有 7 个 div,内容都很少。我想要前 3 个一行,然后是下 3 个,依此类推。

<div class="parent">
 <div class="child-a">abc</div>
 <div class="child-b">def</div>
 <div class="child-c">ghi</div>
 <div class="child-d">jkl</div>
 <div class="child-e">mno</div>
 <div class="child-f">pqr</div>
 <div class="child-g">stu</div>
</div>

那么我怎样才能做到这一点呢?

对于第一行,我有

.child-a, .child-b, .child-c {
  padding: 0 2% 0 0;
  width:100%
  display: inline;
  float: left;
 }

child-c、child-d、child-e 的 css 是什么,以便它们显示在 child-a、child-b、child-c 下面而不是在同一行?

我的完整代码: http://jsfiddle.net/winchendonsprings/UfswL/11/

I have 7 divs in a row with all very little content. I want to have the first 3 one a line then the next 3, and so on.

<div class="parent">
 <div class="child-a">abc</div>
 <div class="child-b">def</div>
 <div class="child-c">ghi</div>
 <div class="child-d">jkl</div>
 <div class="child-e">mno</div>
 <div class="child-f">pqr</div>
 <div class="child-g">stu</div>
</div>

So how can I make this work?

For the first line I have

.child-a, .child-b, .child-c {
  padding: 0 2% 0 0;
  width:100%
  display: inline;
  float: left;
 }

What would the css be for child-c, child-d, child-e so that they would be displayed below child-a, child-b, child-c rather than on the same line?

My complete code: http://jsfiddle.net/winchendonsprings/UfswL/11/

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

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

发布评论

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

评论(3

痴者 2024-12-02 04:40:08

更好的方法是这样的。

.parent div { float: left; }
.parent div:nth-child(3n + 1) { clear: left; }

第三个之后的每个元素都将转到下一行。 演示

这是按照您的要求进行的前 3 个一行,然后是下 3 个,然后尽管存在一些浏览器兼容性问题,但仍然有效。

A better way to go would be something like this.

.parent div { float: left; }
.parent div:nth-child(3n + 1) { clear: left; }

Every element after the third will go to next line. Demo

This is do what you asked the first 3 one a line then the next 3, and so on. efficiently despites some browsers compatiblity issues.

逆流 2024-12-02 04:40:08

您忘记了类的点:

.child-a, .child-b, .child-c,
.child-d, .child-e, .child-f,
.child-g, .child-h, .child-i {
    padding: 0 2% 0 0;
    width:100%
    display: inline;
    float: left;
}

.child-d { clear: left; }
.child-g { clear: left; }

这将使“d”和“g”的左侧不能有浮动元素。

You forgot the dots for the classes:

.child-a, .child-b, .child-c,
.child-d, .child-e, .child-f,
.child-g, .child-h, .child-i {
    padding: 0 2% 0 0;
    width:100%
    display: inline;
    float: left;
}

.child-d { clear: left; }
.child-g { clear: left; }

This will make it so that 'd' and 'g' can't have a floated element to their left.

欢烬 2024-12-02 04:40:08

我得到的是你想要 3 个 div 在顶行,然后 3 个 div 在第二行,然后最后两个 div 在最后一行。如果这是正确的,那么您不需要 7 个 ID。而是使用一个类。

.child_div {
    width: 33%;
    height: 50px; /* for beautification */
    float: left;
}

所有子 div 都必须使用此类。 div 会自动并排对齐。

可能会出现一些问题:

  • 父 div 的高度为 0,因为它的所有子 div 都是浮动的。要解决此问题,您可以设置父 div 的高度(不响应内容长度),或设置 overflow:hidden;overflow:auto; (“拉动”里面的子内容)
  • 不同的浏览器的反应可能略有不同。例如,对于 IE,您需要 width:32.5%; 因为它的工作方式有点不同。希望有帮助。

What I am getting is you want 3 divs one top row, then 3 divs on second row, then the last two divs on last row. If that is correct, you dont need 7 ID's. Instead use one class.

.child_div {
    width: 33%;
    height: 50px; /* for beautification */
    float: left;
}

All child divs must use this class. The divs will automatically align themselves side by side.

A couple of issues that may arise:

  • The parent div will have 0 height because all of it's child divs are floated. To fix this, you can set the parent div's height (does not respond to content length), or set overflow:hidden; or overflow:auto; ("pulls" the child content inside)
  • Different browsers may react a little differently. For example for IE, you need width:32.5%; for example because it works a bit different. Hope it helps.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文