CSS Divs 浮动“卡片”包裹起来

发布于 2024-12-06 22:02:44 字数 433 浏览 0 评论 0原文

我有一个基本的场景设置,我试图将 div 浮动在包装内。它工作得很好,但是当 div 具有可变高度时,我试图实现的简单效果并不配合。我对此进行了研究,尝试对第三张卡应用清除修复,但没有成功。我所取得的唯一成功实际上是在第三张卡之后注入另一个 div,其风格均为清晰。然而,这不是我想要采取的方法,因为我不想更改任何干净的标记。关于完成这项工作的最佳方法的想法?如有必要,我不介意使用一些简单的 JS/jQuery。

所需:

在此处输入图像描述

当前: http://andrewherrick.com/spike/floatcards/

I have this basic scenario setup where I am trying to float divs inside of a wrap. It is working nicely, however the simple effect I am trying to achieve isn't cooperating when the divs have variable heights. I have researched this a bunch tried to apply a clearfix to the 3rd card without success. The only success I've had is actually injecting another div after the 3rd card with a style of clear both. However this is not an approach I want to take as I don't want to change any of the clean markup. Thoughts on the best way to get this done? I don't mind using a simple bit of JS/jQuery if necessary.

Desired:

enter image description here

Current:
http://andrewherrick.com/spike/floatcards/

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

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

发布评论

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

评论(6

动听の歌 2024-12-13 22:02:44

也许我不明白这个问题,但你不能简单地在第四张卡上添加一个“明确:两者”吗?

Perhaps I'm not understanding the question, but couldn't you simply add a "clear: both" to the 4th card?

春风十里 2024-12-13 22:02:44

解决方案 1:

使用 .card:nth-child(3n+1) 将 clear: Both 添加到 3n+1 元素{clear: Both } 或在需要时手动添加类。

解决方案 2:

使用 display: inline-block; 代替 .card 上的 float: left垂直对齐:顶部。您还需要将 margin-right 减少到 16px

如果需要支持IE7,则display: inline-block需要为display:inline-block; *显示:内嵌;缩放:1。

Solution 1:

Add clear: both to the 3n+1 elements, using .card:nth-child(3n+1) { clear: both } or manually adding a class where needed.

Solution 2:

Instead of float: left on .card, use display: inline-block; vertical-align: top. You'll also need to reduce the margin-right to 16px.

If you need to support IE7, then display: inline-block needs to be display:inline-block; *display:inline; zoom:1.

执笔绘流年 2024-12-13 22:02:44

您必须以某种方式定义“行”。您可以为每个 div 设置相同的高度(从而设置 row-height),或者您可以放置​​一个空高度分隔符,或者您可以为从第一个 div 开始的每个第三个 div 设置 clear:left

You must define the "rows" somehow. You can set the same height of each div (thus setting row-height), or you can place a null height separator, or you can set clear:left for every 3rd div starting from the first one.

も星光 2024-12-13 22:02:44

使用 nth-child 选择器设置的 clear:both 就是您所追求的。

您可以使用带有 jQ​​uery 的 nth-child 选择器来动态添加类,对于本​​机不支持它的旧版浏览器:http://api.jquery.com/nth-child-selector/

clear:both set with an nth-child selector is what you are after.

You can dynamically add a class, using the nth-child selector with jQuery for older browsers that don't support it natively: http://api.jquery.com/nth-child-selector/

水中月 2024-12-13 22:02:44

使用 :nth-child 选择器与 clear:both 结合使用。以下将清除每三张牌。由于您使用的是固定宽度(910px),因此这将可靠地工作。

.card:nth-child(3n+1) {
  clear: both;
}

只需将规则添加到现有 CSS 中即可。在 Firefox、Chrome 和 IE 中测试:jsfiddle

Use the :nth-child selector in combination with clear:both. The following will clear every third card. This will work reliably since you're using a fixed width (910px).

.card:nth-child(3n+1) {
  clear: both;
}

Simply add the rule to your existing CSS. Tested in Firefox, Chrome, and IE: jsfiddle

雨夜星沙 2024-12-13 22:02:44

解决方案 1

如果您真的不愿意修改标记,您可以使用一些 jQuery(感谢您的提示):

Jquery

$('.card:nth-child(3n+1)').css('clear','left');


Solution 2

只需稍微改变一下 HTML,它可能会更干净(并且向后兼容)。
也许将每一行包装在自己的包装器中,并对其应用clearfix。

jsfiddle: http://jsfiddle.net/leifparker/sh4fR/

您需要添加 ' clearfix' CSS 代码。见小提琴。

HTML

<div id="wrap">
    <div class="cards clearfix">
        <div class="card">
            1 text text text text text text text text text text text text text text text text
            text text text text text text text text text text
        </div>
        <div class="card">
            2 text text text text text text text text text text text text text text text text
            text text text text text text text text text text
        </div>
        <div class="card">
            3 text text text text text text text text text text text text text text text text
        </div>
    </div>
    <div class="cards clearfix">
        <div class="card">
            4 text text text text text text text text text text text text text text text text
            text text text text text text text text text text
        </div>
        <div class="card">
            5 text text text text text text text text text text text text text text text text
        </div>
        <div class="card">
            6 text text text text text text text text text text text text text text text text
        </div>
    </div>

Solution 1

If you're really averse to touching your mark-up, you could use some jQuery as such (Thanks to thirtydot for the tip):

Jquery

$('.card:nth-child(3n+1)').css('clear','left');


Solution 2

It might be cleaner (and more backwards-compatible) to just slightly alter your HTML.
Perhaps wrap each row in its own wrapper, with a clearfix applied to that.

jsfiddle: http://jsfiddle.net/leifparker/sh4fR/

You'll need to add the 'clearfix' CSS code. See the fiddle.

HTML

<div id="wrap">
    <div class="cards clearfix">
        <div class="card">
            1 text text text text text text text text text text text text text text text text
            text text text text text text text text text text
        </div>
        <div class="card">
            2 text text text text text text text text text text text text text text text text
            text text text text text text text text text text
        </div>
        <div class="card">
            3 text text text text text text text text text text text text text text text text
        </div>
    </div>
    <div class="cards clearfix">
        <div class="card">
            4 text text text text text text text text text text text text text text text text
            text text text text text text text text text text
        </div>
        <div class="card">
            5 text text text text text text text text text text text text text text text text
        </div>
        <div class="card">
            6 text text text text text text text text text text text text text text text text
        </div>
    </div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文