Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox alignment with images and text inside same container

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.

I need to do this using flexbox.

Here is a link to my JSFiddle with what I have done so far:

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

like image 822
Jason Keith McCoy Avatar asked Jan 01 '15 20:01

Jason Keith McCoy


People also ask

How do I align an image in Flex container?

Images can be aligned using both the text-align and flexbox properties. Applying text-align to an image's parent element can be used to right, left or center align the image. You can also use right and left float to position an image along the desired container's edge.


1 Answers

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.

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 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/

like image 195
paceaux Avatar answered Oct 12 '22 19:10

paceaux