Flexbox alignment with images and text inside same container - html

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

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/

Just wrap your info in a container set to display: inline-block:
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
.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;
}
FIDDLE

Related

"Flex-box: row" CSS rule not working as expected

My flex row won't work and I don't know why. I've tried everything. I need the images to be three in a row then break and start a new row. How can I overcome this issue?
.flex-div > div {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-content: space-between;
}
/*kitchen hacks*/
#kitchen-hacks {
padding-top: 80px;
}
.row-one {
width: 100%;
}
#kitchen-hacks h2 {
text-align: center;
margin-bottom: 15px;
border: solid black;
border-radius: 10px;
}
#kitchen-hacks p {
height: auto;
width: 54%;
margin-top: 5px;
margin-bottom: 50px;
border: solid black;
}
<div class="flex-div">
<div id="kitchen-hacks">
<h2 id="hacks"> Kitchen Hacks</h2>
<div class="row-one" style="order: 4"> <img src="/assets/images/kitchen-hack.one.png" alt="Chalk board on the inside of a shelf with mesuring cups hanong from it"> <p> Place a chalkboard and hangers on the inside of your kitchen shelf to store measuring cups the fashion way. </p> </div>
<div class="row-one" style="order: 2"><img src="/assets/images/kitchen-hack-two.png" alt="A shelf inside a shelf with plates on it"> <p> Store shelves in your shelves to optimize spacce and to better take advantage of the empty space not used. </p> </div>
<div class="row-one" style="order: 3"><img src="/assets/images/kitchen-hack-three.png" alt="Coffe pouring into an ice tray">
<p>Fill an ice tray with coffee so that you can enjoy your coffee without watering it down. </p> </div>
<div class="row-one" style="order: 1"> <img src="/assets/images/kitchen-hack-four.png" alt="Spices attached to the fridge with magnets"> <p> Put magnets under your spices to make them stick to the fridge. That way you can multitask</p>
</div>
<div class="row-one" style="order: 5"><img src="/assets/images/kitchen-hack-five.png" alt="Kitchen sink area"> <p> Using your sink as a temporary chopping surface frees the rest of your counter for other cooking steps. </p> </div>
<div class="row-one" style="order: 6"><img src="/assets/images/kitchen-hack-six.png" alt="An egg in a glass of water and two eggs beside the glass">
<p> Fill a tall glass of water and lower an egg in it. If it sinks to the bottom, the egg is fresh. If it floats to the top, it’s time to toss it </p> </div>
</div>
</div>
Make a parent container that will contain all the images you want in a row and use flex properties.
e.g:
.row-one, .row-two{
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
<div class="row-one">
<span>Image 1</span>
<span>Image 2</span>
<span>Image 3</span>
</div>
<br>
<br>
<div class="row-two">
<span>Image 4</span>
<span>Image 5</span>
<span>Image 6</span>
</div>
Try Grids; it is much better to display galleries.
But in this scenario with flex elements, I would use the following approach:
.flex-div{
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.flex-div > .image{
width: 30%;
display: flex;
align-items: center;
flex-direction: column;
justify-content: space-around;
background-color: magenta;
}
<div class="flex-div">
<div class="image">
<img src="custom image" alt="custom image" loading="lazy">
<p>Some text</p>
</div>
<div class="image">
<img src="custom image" alt="custom image" loading="lazy">
<p>Some text</p>
</div>
<div class="image">
<img src="custom image" alt="custom image" loading="lazy">
<p>Some text</p>
</div>
</div>
Codepen: https://codepen.io/rohhthone/pen/wvjVBWy
This video shows how to create a grid media scroller, which might be useful to you.

Is there a way to have different justify-content values for different rows inside a flexbox-container?

Is there a way to have different justify-content values for different rows inside a flexbox-container?
Here is my html:
<div class="wrapper flex-container">
<div class="flex-item">
<h2>Text</h2>
<p>Text</p>
</div>
<div class="flex-item">
<h2>Text</h2>
<p>Text</p>
</div>
<figure class="flex-item"><img src="materiale/junckers.png" alt="junckers"></figure>
<figure class="flex-item"><img src="materiale/byg-garanti.png" alt="byg-garanti"></figure>
<figure class="flex-item"><img src="materiale/gulvbranchen.png" alt="gulvbranchen"></figure>
</div>
I want the first two flex-items (the two div's) to fill one row and to be aligned in accordance with justify-content: space-between;
And I want the three last flex-items (the three figure's) to fill the next row and to be aligned in accordance with justify-content: space-evenly;
Is that possible in the same flex-container?
The answer is no.
In grid, there is the justify-self property in which this would be true. However, in flex there is no support for this style as of May 2022.
flex does support align-self for aligning flex item's on the y-axis (align-items), but not for the x-axis (justify-content).
The good news.
You can still replicate these styles in your stylesheet but they will have to be done manually. You can target the first and second flex-item and use width: calc(100%/2) for the first two flex items that you want to each take 50%. Then you can add flex-wrap: wrap on to your flex-container so the image flex-items wrap onto a new row. Then you can replicate justify-content: space-between; by adding margin: auto; to those respective flex-items.
See below:
.flex-container {
display: flex;
flex-wrap: wrap;
}
.flex-item:first-child,
.flex-item:nth-child(2) {
width: calc(100%/2);
outline: dotted 1px black;
}
.flex-item:nth-child(3),
.flex-item:nth-child(4),
.flex-item:nth-child(5) {
outline: solid 1px;
background: orange;
padding: 10px;
}
<div class="wrapper flex-container">
<div class="flex-item">
<h2>Text</h2>
<p>Text</p>
</div>
<div class="flex-item">
<h2>Text</h2>
<p>Text</p>
</div>
<figure class="flex-item" style="margin-right: auto;"><img src="https://dummyimage.com/100/000/fff" alt="junckers"></figure>
<figure class="flex-item" style="margin: auto;"><img src="https://dummyimage.com/100/000/fff" alt="byg-garanti"></figure>
<figure class="flex-item" style="margin-left: auto;"><img src="https://dummyimage.com/100/000/fff" alt="gulvbranchen"></figure>
</div>

How can I align span text with text in other columns?

I have a row with six columns of image and text here: https://i.imgur.com/UKGGCrX.png
I want to align the two-line text (e.g. Resources and Reserves) with "Exploration" and "Geological Data". https://i.imgur.com/Tplzivb.png
Adding a margin below the image doesn't do the trick, nor does adding a margin above the text... also tried setting a min-height for the icon, but that doesn't do it either.
How can I accomplish this?
Using flexbox you can easily align your text on "top".
This is default flexbox behaviour and does align your text on "baseline":
.container{
display: flex;
flex-flow: row nowarp;
}
.text{
max-width: 30px;
}
<html>
<div class="container">
<div class="imgt">
<img src="https://dummyimage.com/20x20">
<div>Some Text</div>
</div>
<div class="imgt">
<img src="https://dummyimage.com/20x20">
<div class="text">Some Long long Text</div>
</div>
</div>
</html>
I assume you have a margin-top: auto; and margin-bottom: auto;, or some flexbox properties like align-items: center; somewhere, that your text gets centered inside the div.
You can use flex styles to obtain your desired results. I created an example showing how the align-items: stretch; property will help with keeping icons/images/and content aligned with siblings.
Live example: https://codepen.io/SROwl/pen/xNrRmw
HTML:
<h3> align content top </h3>
<div class="bucket-row">
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Single Line</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Single Line</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Triplen Extra Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
</div>
<h3> align content bottom </h3>
<div class="bucket-row content-end">
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Single Line</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Single Line</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Triplen Extra Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
</div>
SCSS:
body {
font-family: sans-serif;
}
.bucket-row {
display: flex;
flex-direction: row;
align-items: stretch;
justify-content: space-between;
width: 600px;
margin-bottom: 40px;
.bucket {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
max-width: 85px;
background-color: #eee;
padding-bottom: 20px;
img {
margin-bottom: 15px;
}
}
&.content-end {
.bucket {
span {
margin-top: auto;
}
}
}
}

CSS Issues with word wrapping in an infiinite scrolling div

I am working on a project and want to have infinite horizontal scrolling for cells that each contain a thumbnail and some text. I have everything mostly working. However, I can not get text to wrap. The css and html are below. The first two items in the scroll box have text that should wrap, but overflows.
This is the style (cell 2 is only for demo to alternate background colors):
.container {
display: flex;
flex-direction: row;
padding: 10px;
height: 10%;
overflow-x: auto;
white-space: nowrap;
}
.cell {
background: #818181;
flex: fit-content;
padding: 10px;
width: 220px;
height: 8%;
display: inline-block;
}
.cell2 {
background: #f44336;
flex: fit-content;
padding: 10px;
width: 220px;
height: 8%;
display: inline-block;
}
.data {
justify-content: center;
word-wrap: break-word;
}
<div>
<h2>Scroll Test</h2>
<div class="container">
<div class="cell">
<a href= "#">
<span class="data"> <img src="http://roncabeanz.com/Roncabeanz/images/CoffeeIcon.jpg" width="200px"></span>
<span class="data"> <h3>Some text here</h3></span>
</a>
</div>
<div class="cell2">
<a href= "#">
<span class="data"> <img src="http://roncabeanz.com/Roncabeanz/images/CoffeeIcon.jpg" width="200px"></span>
<span class="data"> <h3>Some text here that will over flow</h3></span>
</a>
</div>
<div class="cell">
<a href= "#">
<span class="data"> <img src="http://roncabeanz.com/Roncabeanz/images/CoffeeIcon.jpg" width="200px"></span>
<span class="data"> <h3>Some text here</h3></span>
</a>
</div>
<div class="cell2">
<a href= "#">
<div class="data"> <img src="http://roncabeanz.com/Roncabeanz/images/CoffeeIcon.jpg" width="200px"> </div>
<div class="data"> <h3>Some text here</h3></div>
</a>
</div>
<?php } ?>
</div>
</div>
Link to fiddle: https://jsfiddle.net/jonathannah/3rxcktpm/29/
Just add
h3 { white-space: normal }. The children of .containerinherit white-space: nowrap, which causes the problem.
Another issue in your html is the h3 inside a span, which is invalid. You shouldn't use block elements inside inline elements. You can change it to a div give it the style display: inline

Styling the last column of a flexbox

I have to implement a design looking like this:
This will be the index.php page for WordPress and therefore it will be using the WordPress loop to output the blog articles as individual "resources".
Normally I would implement this as a flexbox, because the number of items is variable and I need it to be responsive, however this time our designer has added borders in between the items.
This would be fine but the borders are not included before or after the end of the rows. I cant solve this any of the pseudo selectors that I know of.
Currently my HTML and CSS look something like this:
section {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
section div {
display: flex;
flex-direction: column;
border-right: 1px solid red;
padding-right: 20px;
margin-right: 20px;
margin-bottom: 10px;
}
<section>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
</section>
Is this something I can solve with CSS grid and is there a way to achieve this using flexbox or another layout that isn't grid?
This really depends on the background under the items, but if it is made of one color, you could use the solution of simply overlapping the left border with the pseudo element, like this:
section {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
position: relative;
margin-left: -20px;
}
section::before {
content: "";
position: absolute;
top: 0;
bottom: 0;
left: 20px;
width: 1px;
background: #fff;
}
section div {
display: flex;
flex-grow: 1;
flex-direction: column;
border-left: 1px solid red;
padding-left: 20px;
margin-left: 20px;
text-align: center;
}
<section>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
</section>
I kind of have what you need, I'm displaying the right border with pseudo element and pushing it off screen if its the last element of a row, and some empty divs to push a new row to keep the layout.
section {
display: flex;
align-items: center;
flex-wrap: wrap;
overflow: hidden;
}
section div {
display: flex;
flex-grow: 1;
flex-direction: column;
padding: 0 20px;
position: relative;
}
section div:nth-last-child(9) ~ div{
height: 0;
width: 140px;
}
section div::after {
content: '';
position: absolute;
border-right: 1px solid red;
left: 100%;
display: block;
height: 100%;
}
section div:nth-last-child(9)::after {
border: none;
}
section div p { width: 140px; }
<section>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>