如何使多个容器的高度与高度相同:自动
我有两个内联块元素,分别是容器-#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>