简体   繁体   English

并排对齐3个项目的高度

[英]Align the height of 3 items when they are side by side

I use bootstrap to display 3 items. 我使用引导程序来显示3个项目。 When the screen is narrow (eg, in a smartphone), they are displayed one after another vertically. 当屏幕较窄时(例如,在智能手机中),它们会垂直垂直显示。 When the screen is large, I want them to be displayed side by side in one row. 当屏幕很大时,我希望它们可以并排显示。 The 3 items do not have same text, but i want them to ALWAYS have the same height when they are in one row. 这3个项目没有相同的文本,但我希望它们在同一行时始终具有相同的高度。 I use the following code: 我使用以下代码:

<section id="apps" class="apps section">
  <div class="container">
    <div class="row">
      <div class="col-md-4 col-sm-4 col-xs-12">
        <div class="app">
          <div class="content">
            ...
          </div><!-- content -->
        </div><!-- app -->
      </div><!-- col -->
    <div class="row">
      <div class="col-md-4 col-sm-4 col-xs-12">
        <div class="app">
          <div class="content">
            ...
          </div><!-- content -->
        </div><!-- app -->
      </div><!-- col -->
    <div class="row">
      <div class="col-md-4 col-sm-4 col-xs-12">
        <div class="app">
          <div class="content">
            ...
          </div><!-- content -->
        </div><!-- app -->
      </div><!-- col -->
    </div>
  </div>
</section>

And the CSS: 和CSS:

.apps .app .content {
  padding: 10px 15px;
  min-height: 250px;
}

Here is the JSBin . 这是JSBin

The above CSS allows the 3 items to have the same height when the screen is very large: 当屏幕很大时,上述CSS允许3个项目具有相同的高度:

在此处输入图片说明

However, when the screen is a little bit narrow, they don't have the same height: 但是,当屏幕略窄时,它们的高度不一样:

在此处输入图片说明

Does anyone know how to always align them when they are side by side? 有谁知道当它们并排放置时如何始终对齐它们?

Basically, here's what you want 基本上,这就是你想要的

Fiddle: https://jsfiddle.net/ec8Lbuvj/2/ 小提琴: https : //jsfiddle.net/ec8Lbuvj/2/

You are searching for flexbox. 您正在搜索flexbox。 I don't use your markup but here's how you should do it. 我不使用您的标记,但这是您应该使用的方式。 It's time for you to analyze your code. 现在是时候分析代码了。 Just put display flex to the parent element. 只需将display flex放在父元素上即可。

.parent {
    display: flex;
}

.child {
    width: 30%;
    // don't put min-height. It will be automatic
}

Note: This technique doesn't work in IE 8 and lower. 注意:此技术在IE 8及更低版本中不起作用。 Cheers. 干杯。 Hope it helped. 希望能有所帮助。

I used media queries because this is how bootstrap work. 我使用了媒体查询,因为这是引导程序的工作方式。

You can either set a fixed height or use jQuery plugin like jQuery Match Height to make sure that the heights are always the same. 您可以设置固定高度,也可以使用jQuery插件(例如jQuery Match Height)来确保高度始终相同。

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.matchHeight/0.7.0/jquery.matchHeight-min.js"></script>

<script type="text/javascript">
if( $(window).width() >= 768px ){ //preventing this from running on mobile device, 768px is bootstrap's breakpoint for col-sm-4
        $('.components-class-name').matchHeight();
    }
</script>

But this will make the components same height in mobile mode 但这将使组件在移动模式下具有相同的高度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM