如何使多个容器的高度与高度相同:自动 [英] How to get multiple containers height the same with height: auto

查看:71
本文介绍了如何使多个容器的高度与高度相同:自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个内联块元素,分别是容器-#keyPointsBlock#caseSlider.带有图像的图块将始终更高.我想匹配两个元素的高度,因此#keyPointsBlock必须与#caseSlider相同.在这种情况下,我无法将父元素的height: auto更改为固定数字.

I have two inline-block elements which are containers - #keyPointsBlock and #caseSlider. The block with the image will always be taller. I am wanting to match the two elements in height, so #keyPointsBlock needs to be the same height as #caseSlider. I can't change the parent element's height: auto to a fixed number in this situation.

有没有办法做到这一点?

Is there a way to do this?

/*-- Slider Section --*/
#slideSec {
	width: 100%;
	height: auto;
	min-height: 400px;
}
.slideSecBlock {
	display: inline-block;
	vertical-align: top;
	height: 400px;
	position: relative;
}
/*- Key Points -*/
#keyPointsBlock {
	width: 40%;
	background: green;
	position: relative;
}
#keyPointsTitle {
	font-family: 'Muli', sans-serif;
	font-size: 2rem;
	letter-spacing: .2rem;
	text-transform: uppercase;
}
#keyPointsList {
	text-decoration: none;
}
#keyPointsList li {
	margin-bottom: 8px;
}
/*- Case Slider -*/
#caseSlider {
	width: 60%;
	height: auto;
}
.caseSlide {
	width: 100%;
	height: 100%;
}
.caseSlide img {
	width: 100%;
	height: 100%;
	object-fit: contain;
}

<section id="slideSec">
  <div class="slideSecBlock" id="keyPointsBlock">
    <div>
      <h2 id="keyPointsTitle">Key Points</h2>
      <ul id="keyPointsList">
        <li>A</li>
        <li>B</li>
        <li>C</li>
      </ul>
    </div>
  </div><div class="slideSecBlock" id="caseSlider">
    <div class="caseSlide">
      <img src="https://justifiedgrid.com/wp-content/uploads/life/biking/137646854.jpg" alt="">
    </div>
  </div>
</section>

推荐答案

只需使用Flexbox并将display: flex添加到您的#slideSec中,它们将以相同的高度排列.

By simply use Flexbox and add display: flex to your #slideSec they will line up with equal high.

这在行方向(默认)上用作flex子代,默认为align-items,将使同一行上的所有子代都具有相同的高度,无论哪个最高.

This works as flex children in a row direction (the default), with the align-items default stretch, will make all children on the same row be the same height, no matter which is the highest.

此外,在img中添加display: block,使其正确对齐(不包含行内/块元素周围的空白),然后删除所有height: 100%(和height: auto)默认设置),因为它不适用于Flexbox.

Furthermore, add display: block to your img so it lines up properly (w/o the white space that surrounds inline/-block elements) and then remove all the height: 100% (and height: auto as it is the default), as that will not work well with Flexbox.

注意,在下面的代码示例中,我删除了不必要的属性.

如果您还需要控制内部元素,则最好嵌套Flexbox.

If you need to control the inner elements too, better to nest Flexbox.

堆栈片段

/*-- Slider Section --*/
#slideSec {
  min-height: 400px;
  display: flex;
}

.slideSecBlock {
  position: relative;
}

/*- Key Points -*/
#keyPointsBlock {
  width: 40%;
  background: green;
  position: relative;
}

#keyPointsTitle {
  font-family: 'Muli', sans-serif;
  font-size: 2rem;
  letter-spacing: .2rem;
  text-transform: uppercase;
}

#keyPointsList {
  text-decoration: none;
}

#keyPointsList li {
  margin-bottom: 8px;
}

/*- Case Slider -*/
#caseSlider {
  width: 60%;
}

.caseSlide {
}

.caseSlide img {
  display: block;
  width: 100%;
  object-fit: contain;
}

<section id="slideSec">
  <div class="slideSecBlock" id="keyPointsBlock">
    <div>
      <h2 id="keyPointsTitle">Key Points</h2>
      <ul id="keyPointsList">
        <li>A</li>
        <li>B</li>
        <li>C</li>
      </ul>
    </div>
  </div>
  <div class="slideSecBlock" id="caseSlider">
    <div class="caseSlide">
      <img src="https://justifiedgrid.com/wp-content/uploads/life/biking/137646854.jpg" alt="">
    </div>
  </div>
</section>

这篇关于如何使多个容器的高度与高度相同:自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆