You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Bootstrap 4中.col与.col-xs/sm/md/lg-6的区别及元素换行问题咨询

Hey there! Let's break this down clearly for you—Bootstrap's grid can trip up even experienced devs sometimes, so no worries asking about this.

1. Differences Between .col and .col-{breakpoint}-6 in Bootstrap 4

Let's start with the core distinctions between these two column classes:

  • .col is an auto-layout column
    • It’s part of Bootstrap’s flexbox-powered "auto columns" system. Any .col elements inside the same .row will split the available row width equally (3 .cols = each takes 1/3 of the row, 4 = 1/4, etc.).
    • It has no breakpoint specified, so this equal-width behavior stays consistent across all screen sizes—from tiny mobile (xs) up to large desktops (lg).
  • .col-{breakpoint}-6 is a responsive fixed-width column
    • The -6 means this column takes up 6 out of Bootstrap’s 12 grid columns (aka 50% of the row’s width) at the specified breakpoint and above.
    • It’s inherently responsive: for example, .col-md-6 will take 50% width on medium screens and larger, but stack to 100% width on smaller screens unless you add a smaller breakpoint class (like .col-sm-6).
    • Unlike .col, this class explicitly defines a fixed width ratio at specific screen sizes, rather than auto-splitting available space.
2. Why Your Third .col Wraps to the Next Line?

Looking at your code snippet, you only have one .col in the .row—but based on your question, I assume you actually have three .col elements inside that same .row (easy typo when pasting code!).

The most common culprit here is content inside your .col breaking the flex layout—specifically, your image isn’t set to be responsive. Bootstrap’s .row uses flexbox, which lets .cols split space equally by default, but if an element inside a .col (like your image) has a fixed width larger than the column’s available space, it’ll stretch the column wider than its intended share. When the total width of all three columns exceeds the .row’s width, the last column wraps to the next line (since .row has flex-wrap: wrap enabled by default).

Fixes to Try:

  1. Make your image responsive with Bootstrap’s built-in .img-fluid class. This forces the image to max out at 100% of its parent container’s width, so it won’t stretch the column:
    <img src="http://qlip.in/images/YMCA-Realtor-Donation-3.jpg" class="img-fluid">
    
  2. Double-check your nesting: Bootstrap requires columns to be direct children of a .row—if you added extra divs around your .col elements that aren’t part of Bootstrap’s grid system, that could throw off the layout.
  3. Verify no custom CSS is overriding Bootstrap styles: If you have custom styles that change flex-grow, flex-shrink, or width on .col or .row, that could disrupt the auto-layout behavior.

内容的提问来源于stack exchange,提问作者bkirklint

火山引擎 最新活动