Html Flexbox 与同一容器内的图像和文本对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27734263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flexbox alignment with images and text inside same container
提问by Jason Keith McCoy
I am making a flexbox Employee ID card layout. The employee's picture will go to the left and the employee's info (name, employee-id, department, etc) will go to the right of the image in a list format top-to-bottom.
我正在制作一个 flexbox 员工 ID 卡布局。员工的图片将显示在左侧,员工的信息(姓名、员工 ID、部门等)将以列表格式从上到下显示在图像的右侧。
I need to do this using flexbox.
我需要使用 flexbox 来做到这一点。
Here is a link to my JSFiddle with what I have done so far:
这是我的 JSFiddle 的链接,其中包含我迄今为止所做的工作:
http://jsfiddle.net/Hopped_Up_Designs/3teLbqqf
http://jsfiddle.net/Hopped_Up_Designs/3teLbqqf
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
align-items: center;
flex-wrap: wrap;
min-width: 320px;
max-width: 1220px;
}
.flex-item {
height: 120px;
width: 300px;
background-color: #e46119;
border: 1px solid #626262;
margin: 3px;
padding: 10px 0 0 10px;
}
span {
padding-left: 5px;
}
<div class="flex-container">
<div class="flex-item"><img src="http://placehold.it/100x100">
<span>Text fields will go here</span>
</div>
<div class="flex-item"><img src="http://placehold.it/100x100">
<span>Text fields will go here</span>
</div>
<div class="flex-item"><img src="http://placehold.it/100x100">
<span>Text fields will go here</span>
</div>
<div class="flex-item"><img src="http://placehold.it/100x100">
<span>Text fields will go here</span>
</div>
<div class="flex-item"><img src="http://placehold.it/100x100">
<span>Text fields will go here</span>
</div>
<div class="flex-item"><img src="http://placehold.it/100x100">
<span>Text fields will go here</span>
</div>
<div class="flex-item"><img src="http://placehold.it/100x100">
<span>Text fields will go here</span>
</div>
<div class="flex-item"><img src="http://placehold.it/100x100">
<span>Text fields will go here</span>
</div>
<div class="flex-item"><img src="http://placehold.it/100x100">
<span>Text fields will go here</span>
</div>
</div>
Any and all help would be much appreciated. Thanks, Jason
任何和所有的帮助将不胜感激。谢谢,杰森
回答by paceaux
The most unobtrusive approach which I found was to add this:
我发现的最不引人注目的方法是添加以下内容:
.flex-item {
display:flex;
align-items: center;
}
.flex-item img{
flex-grow:0;
flex-shrink:0;
}
If you add multiple <span>
immediately after the img, they'll continue displaying in row fashion (like a float). This may not be the what you want, though. One option could be to set each item to a fixed width - but that breaks the spirit of flexbox.
如果您<span>
在 img 后立即添加多个,它们将继续以行方式显示(如浮动)。不过,这可能不是您想要的。一种选择是将每个项目设置为固定宽度 - 但这打破了 flexbox 的精神。
But if you modify your text wrapper to contain a single <div>
, I think you'll be fine. The <div>
will display as a block level element until you choose otherwise.
但是,如果您修改文本包装器以包含单个<div>
,我认为您会没事的。该<div>
直到您选择,否则将显示为一个块级元素。
<div class="flex-item">
<img src="http://placehold.it/100x100">
<div>
<ul>
<li>Text fields will go here</li>
<li>and here</li>
</ul>
</div>
</div>
Here's a fork of your fiddle where I did this. http://jsfiddle.net/Paceaux/7fcqkb50/1/
这是你的小提琴的一个叉子,我在那里做了这个。http://jsfiddle.net/Paceaux/7fcqkb50/1/
回答by jmore009
Just wrap your info in a container set to display: inline-block
:
只需将您的信息包装在一个容器中,设置为display: inline-block
:
HTML
HTML
<div class="flex-item">
<img src="http://placehold.it/100x100"/>
<div class="info-container">
<p>Field 1</p>
<p>Field 2</p>
<p>Field 3</p>
</div>
</div>
CSS
CSS
.info-container{
display: inline-block;
vertical-align: top;
padding: 0 0 0 5px;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.info-container p{
padding: 0;
margin: 0 0 10px;
}