【问题标题】:<div class="clearfix"></div> causes a gap between my columns<div class="clearfix"></div> 导致我的列之间出现间隙
【发布时间】:2018-02-06 01:07:06
【问题描述】:

我正在尝试在移动视图中使用 Bootstrap 3 创建此设计 (here)。但是,我希望图像彼此相对,例如 this

这是我到目前为止所做的:

HTML:

<div class="container">
        <div class="row">
            <div class="col-md-6"><img src="http://www.dialhousehotel.com/wp-content/uploads/2017/09/room-5.jpg" alt="Picture of room one" class="img-responsive" width="500px" height="300px"></div>
            <div class="col-md-6">
                <div class="rooms-photo-text">
                    <h2><span style="color: #bb9b50;">Cosy<br>
</span></h2>
                    <p>Our Cosy double rooms are small but perfectly appointed for your stay. Each one has a double bed and views out to the garden from the main house.</p>
                    <div class="wideSeparatorDark"></div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="col-md-6 col-md-pull-6"><img src="http://via.placeholder.com/500x300" alt="Picture of room two" class="img-responsive" width="500px" height="300px"></div>
            <div class="col-md-6 col-md-push-6">
                <div class="rooms-photo-text">
                    <h2><span style="color: #bb9b50;">Splendid<br>
</span></h2>
                    <p>Our Splendid rooms are more spacious than the Cosy and located in both the main house and the coach house building. Beautifully appointed with Cotswolds charm.</p>
                    <div class="wideSeparatorDark"></div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="col-md-6"><img src="http://via.placeholder.com/500x300" alt="Picture of room three" class="img-responsive" width="500px" height="300px"></div>
            <div class="col-md-6">
                <div class="rooms-photo-text">
                    <h2><span style="color: #bb9b50;">Awesome<br>
</span></h2>
                    <p>Our Awesome rooms are wonderfully comfortable, similar to the Splendid but with stunning views of the village and River Windrush. These truly sumptuous rooms are the perfect place to relax and call ‘home from home’.</p>
                    <div class="wideSeparatorDark"></div>
                </div>
            </div>
        </div>
    </div>

第二个 col-md-6 组之间似乎存在差距。似乎没有修复重叠的列。 密码笔是https://codepen.io/anon/pen/pabRJg

【问题讨论】:

  • 你不能像那样搞乱网格系统 - .row 需要只包含 .col 元素,你不能只插入任意其他东西。只需将这 6-of-12 宽列中的两列放入 .row 中,这应该会自动解决您的问题(.row 已经隐式应用了 clearfix。)
  • .clearfix 在单个.row 中使用超过 12 个时有意用于响应式重置:getbootstrap.com/docs/3.3/css/#grid-responsive-resets

标签: twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

您应该反转推/拉列...

            <div class="clearfix"></div>
            <div class="col-md-6 col-md-push-6"><img src="http://via.placeholder.com/500x300" alt="Picture of room two" class="img-responsive" width="500px" height="300px"></div>
            <div class="col-md-6 col-md-pull-6">
                <div class="rooms-photo-text">
                    <h2><span style="color: #bb9b50;">Splendid<br>
</span></h2>
                    <p>Our Splendid rooms are more spacious than the Cosy and located in both the main house and the coach house building. Beautifully appointed with Cotswolds charm.</p>
                    <div class="wideSeparatorDark"></div>
                </div>
            </div>
            <div class="clearfix"></div>

https://codepen.io/anon/pen/RQRKLd

【讨论】:

  • 谢谢。现在我明白了。
  • 此外,如果 OP 将每个图像/文本列组包装在其自己的 row 中,他们可以完全省略 clearfix 的需要。
  • @RobertC 是的,但在某些情况下无法响应。在单个.row 容器中需要超过 12 个的情况stackoverflow.com/questions/26679160/…
  • 同意,在某些情况下row 并不理想。但是像上面那样已知列数固定的列?它深入基于意见,但对我来说row 是这里的最佳选择,因为它在保持框架预期实现的同时省略了不必要的元素。