javascript - 如何仅使用 CSS 使左右两侧的高度相等?

标签 javascript css

我需要使 Hero 元素的高度相等,如下所示 https://codepen.io/hellouniverse/pen/KqvYoj .我是用 JavaScript 做的。但是,我感觉很恶心。我尝试使用 CSS flex 使高度相等。但是,只有当左右两侧的白框内的文本行数不同时,我才无法使用 CSS 设置高度。

当行号不同时,如何仅使用 CSS 使高度相等?

  var $firstHeroTwinHeight;
  var $secondHeroTwinHeight;
  var $firstHeroTwinHeightContents;
  var $secondHeroTwinHeightContents;

  if ($(window).width() > 767) {
    if ($('.section-hero').length > 0) {
      $firstHeroTwinHeightContents = $('.section-hero .field-items .field-item:first-child .hero__contents');
      $secondHeroTwinHeightContents = $('.section-hero .field-items .field-item:last-child .hero__contents');
      $firstHeroTwinHeight = $firstHeroTwinHeightContents.outerHeight();
      $secondHeroTwinHeight = $secondHeroTwinHeightContents.outerHeight();

      if ($firstHeroTwinHeight > $secondHeroTwinHeight) {
        $secondHeroTwinHeightContents.css('height', $firstHeroTwinHeight);
      }
      else {
        $firstHeroTwinHeightContents.css('height', $secondHeroTwinHeight);
      }
    }
  }

我有一个codepen https://codepen.io/hellouniverse/pen/KqvYoj我试图在没有 js 的情况下复制我所拥有的东西

最佳答案

最好的猜测:)

.field-items {
  display: flex;
  justify-content: space-around;
  /* justify-content: space-between; ** pushes children to the sides
  justify-content: flex-start; ** pushes children to the left
  justify-content: flex-end; ** pushes children to the left */
  padding: 1em;
  border: thin solid lightgray;
}
<div class="container">
  <div class="section-hero">
    <div class="field-items">
      <div class="field-item"><img src="http://placehold.it/200/00ff00"></div>
      <div class="field-item"><img src="http://placehold.it/200/0000ff"></div>
    </div>
  </div>
</div>

关于javascript - 如何仅使用 CSS 使左右两侧的高度相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44735549/

相关文章:

javascript - 哪些文档描述了传递给 Express app.METHOD 回调参数的内容

javascript - 单元测试导出的模块功能

javascript - react .js : Why delete button is not working?

javascript - 异步代码中需要同步

javascript - 是否可以在 javascript 中为文本设置渐变动画?

html - 在移动设备上为网站设置背景图片 slider

javascript - 更改 CSS 动画 anchor

javascript - 如何在单击按钮时播放 CSS 动画

javascript - 表格列的高度相等

Css 重复背景