简体   繁体   English

在 flexbox 中使图像和文本大小相同

[英]Make images and text the same size in a flexbox

So basically I would like to display some pictures on the left and some content on the right, while using a flexbox to make them of the same height.所以基本上我想在左边显示一些图片,在右边显示一些内容,同时使用 flexbox 使它们具有相同的高度。

The result should look something like this:结果应如下所示:

 #div1 { width: 100%; background-color: black; color: white; overflow: auto; font-size: 1vmax; display: flex; align-items: center; } #div2 { padding: 1%; display: flex; align-items: center; flex: 1; } #div3 { padding: 1%; text-align: center; white-space: nowrap; width: max-content; }
 <div id="div1"> <div id="div2"> <:-- Some very big pictures --> <img src="https.//via.placeholder:com/1000/" height="50em" /> <img src="https.//via.placeholder:com/1000/" height="50em" /> <img src="https.//via.placeholder:com/1000/" height="50em" /> <img src="https.//via.placeholder:com/1000/" height="50em" /> <img src="https.//via.placeholder.com/1000/" height="50em" /> </div> <div id="div3"><span>Line 1</span><br /><span>Line 2</span></div> </div>

However, you can see that in this case the height of the pictures are manually set and isn't the same height as the two-line content on the right.但是,您可以看到,在这种情况下,图片的高度是手动设置的,并且与右侧的两行内容的高度不同。

I hope I can do something like this:我希望我能做这样的事情:

 #div1 { width: 100%; background-color: black; color: white; overflow: auto; font-size: 1vmax; display: flex; align-items: center; } #div2 { padding: 1%; display: flex; align-items: center; flex: 1; } #div3 { padding: 1%; text-align: center; white-space: nowrap; width: max-content; }
 <div id="div1"> <div id="div2"> <:-- Some very big pictures --> <img src="https.//via.placeholder:com/1000/" height="1em" /> <img src="https.//via.placeholder:com/1000/" height="1em" /> <img src="https.//via.placeholder:com/1000/" height="1em" /> <img src="https.//via.placeholder:com/1000/" height="1em" /> <img src="https.//via.placeholder.com/1000/" height="1em" /> </div> <div id="div3"><span>Line 1</span><br /><span>Line 2</span></div> </div>

But apparently setting the height of the images to 1em made it extra-small.但显然将图像的高度设置为1em使它变得非常小。

Any ideas how to achieve that?任何想法如何实现这一目标?

You should use image width instead of height, also use justify-content to parent div您应该使用图像宽度而不是高度,也应该使用 justify-content 到父 div

.main-container{
     justify-content:space-between;
}

If you want vertically center according to div height如果你想根据 div 高度垂直居中

height: 100%;
display:flex;
flex-direction: column;
justify-content: center;

 .main-container{ width:100%; background-color:black; color:white; overflow:auto; font-size:1vmax; display:flex; justify-content: space-between; align-items:center; }.images-container{ display:flex; align-items:center; padding: 1%; }.images-container img{ width: 50px; }.text-container{ display: flex; flex-direction: column; justify-content: center; padding: 1%; }
 <div class="main-container"> <div class="images-container"> <:-- Some very big pictures --> <img src="https.//via.placeholder:com/1000/" /> <img src="https.//via.placeholder:com/1000/" /> <img src="https.//via.placeholder:com/1000/" /> <img src="https.//via.placeholder:com/1000/" /> <img src="https.//via.placeholder.com/1000/" /> </div> <div class="text-container"> <span>Line 1</span><br /><span>Line 2</span> </div> </div>

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

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