How to combine containers in a flexbox layout? - css

I have a simple flexbox layout like this:
html, body {
margin:0;
padding:0;
background:grey;
}
.container {
display:flex;
flex-direction:row;
}
.image1, .image2, .image3, .image4, .image5, .image6 {
padding:10px;
}
img {
max-width:100%;
}
<div class="container">
<div class="image1">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image2">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image3">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>
<div class="container">
<div class="image4">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>
<div class="container">
<div class="image5">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image6">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>
I am currently doing it with 3 seperate flex containers, I am trying to combine everything in to 1.
Am I able to do this with flexbox or would CSS grid be more appropriate?

Am I able to do this with flexbox
Yes, using flex-wrap property and wrap value, which will force items to wrap onto multiple lines.
html, body {
margin: 0;
background: grey;
}
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.image {
padding: 10px;
box-sizing: border-box;
}
img {
width: 100%;
}
.third {
width: 33.333%;
}
.half {
width: 50%;
}
.full {
width: 100%;
}
<div class="container">
<div class="image third">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image third">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image third">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image full">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image half">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image half">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>
You could make it even shorter, using flex instead of width.
html, body {
margin: 0;
background: grey;
}
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.image {
flex: 1 0 0;
padding: 10px;
box-sizing: border-box;
}
.image.full {
flex: none;
width: 100%;
}
img {
width: 100%;
}
<div class="container">
<div class="image">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image full">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>

The layout (with a single container) is relatively easy and simple using CSS Grid.
Use Grid's line-based placement techniques to position grid items and create grid areas.
.container {
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-gap: 10px;
}
.image1, .image2, .image3 {
grid-column: span 2;
}
.image4 {
grid-column: 1 / -1;
justify-self: center; /* optional */
}
.image5, .image6 {
grid-column: span 3;
}
img {
max-width: 100%;
}
body {
margin: 0;
padding: 10px;
background: grey;
}
<div class="container">
<div class="image1">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image2">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image3">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image4">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image5">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image6">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>
jsFiddle demo

Related

Flexbox make columns equal heights

I am trying to get 3 columns in a row and be the same height.
I am using the HTML/CSS below. I can't figure out why the boxes arent the same height.
.container {
text-align: center;
display: block;
width: 100%;
}
.grid {
width: 100%;
height: 100%;
}
.row {
display: flex;
height: 100%;
flex-direction: row;
}
.col {
background: #444;
padding: 2em;
flex: 1;
height: 100%;
border: 1px solid blue;
}
<div class="container">
<div class="data">
<div class="grid">
<div class="row">
<div class="col">
<p>col 1</p>
</div>
<div class="col">
<p>col </br>2</p>
</div>
<div class="col">
<p>col 3</p>
</div>
</div>
</div>
</div>
</div>
How can I make the boxes all the same height. thanks
Simply, remove the height: 100% from .col.
flex: 1; will do the job.
.row {
display: flex;
}
.col {
flex: 1;
background: #444;
padding: 2em;
border: 1px solid blue;
text-align: center;
}
<div class="container">
<div class="data">
<div class="grid">
<div class="row">
<div class="col">
<p>col 1</p>
</div>
<div class="col">
<p>col </br>2</p>
</div>
<div class="col">
<p>col 3</p>
</div>
</div>
</div>
</div>
</div>

Display items in one line with scroll CSS

I am trying to display images in a single line horizontal with scroll but I can't figure how to I would appreciate it if anyone could help me.
CSS
.img {
max-width: 400px;
position: relative;
}
.animal {
float: left;
background-color: #fff;
}
.image-grid{
flex-wrap: wrap;
width: 1400px;
margin: 0 auto;
}
.pet-photo {
padding: 10px;
display: block;
box-sizing: border-box;
flex-basis: 100%;
float: left;
padding: 20px;
width: 200px;
height: 300px;
display: block;
}
js
<div class ="image-grid">
<div class="animal">
<img class="pet-photo " src="${pet.photo}" >
<div class="olay" onclick="location.href='${pet.href}';" style="cursor: pointer;">
<div id="mydiv">
<h2 class="pet-name">${pet.name}
<h1 class="species">${pet.species}
</div>
<div></div></div>
</div>
</div>
That's all thank you for reading :) really appreciate it.
solution is display: flex; with flex-wrap: nowrap;
.container-items {
overflow: auto;
width: 100%;
}
.items {
display: flex;
flex-wrap: nowrap;
}
.items .item {
min-width: 150px;
text-align: center;
padding: 5px;
}
.items .item h2,
.items .item h3 {
margin: 0;
}
.items .item img {
width: 100%;
}
<div class ="container-items">
<div class ="items">
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3>here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
</div>
</div>

How to make div expand with content using flex?

I have this HTML and CSS:
.container {
width: 100%;
}
.group {
display: flex;
justify-content: space-between;
min-width: 214px;
background: #eee;
}
.abbr {
/* some styling */
}
.name {
/* some styling */
}
<div class="container">
<div class="group">
<div class="abbr">
<p>MS</p>
</div>
<div>
<p class="name">Mark Smith</p>
</div>
</div>
</div>
Now, if I use just min-width, the whole div stretches as the entire width of the container. If I just use width, it won't expand if the name is longer than Mark Smith (rather it will go to the next line).
This is what I wanted to achieve:
How do I achieve this in flexbox?
What you're looking for is to apply width: fit-content to .group.
Then you can adjust the offset between the abbreviation and name with min-width on the .abbr.
This can be seen in the following:
.group {
display: flex;
width: fit-content;
background: #eee;
margin: 10px 0;
}
.group > div {
margin: 0 10px;
}
.abbr {
min-width: 50px;
}
<div class="container">
<div class="group">
<div class="abbr">
<p>MS</p>
</div>
<div>
<p class="name">Mark Smith</p>
</div>
</div>
<div class="group">
<div class="abbr">
<p>MS</p>
</div>
<div>
<p class="name">A Really Really Long Name</p>
</div>
</div>
</div>
I use inline-block on .container so that it won't take up the whole line.
.container {
display: inline-block;
}
.group {
display: flex;
background: #eee;
}
.abbr {
padding: 0 7px;
}
.name {
padding: 0 7px;
}
<div class="container">
<div class="group">
<div class="abbr">
<p>MS</p>
</div>
<div>
<p class="name">Mark Smith</p>
</div>
</div>
</div><br/><br/>
<div class="container">
<div class="group">
<div class="abbr">
<p>MR</p>
</div>
<div>
<p class="name">Loooooooooooooooong Name</p>
</div>
</div>
</div>
Another solution is to use a third element that consume all the remaining space and set the background color on the text content only:
.container {
margin: 0 0 5px 0;
}
.group {
display: flex;
}
.abbr {
padding: 0 7px;
background: #eee;
}
.name {
padding: 0 7px;
background: #eee;
flex: 0 0 auto;
}
.blank-space{
flex: 1 1 auto;
}
<div class="container">
<div class="group">
<div class="abbr">
<p>MS</p>
</div>
<div class="name">
<p>Mark Smith</p>
</div>
<div class="blank-space"></div>
</div>
</div>
<div class="container">
<div class="group">
<div class="abbr">
<p>MR</p>
</div>
<div class="name">
<p>Loooooooooooooooong Name</p>
</div>
<div class="blank-space"></div>
</div>
</div>

How to make a layout, where the 3rd child is full width?

Is it possible to make the following layout just with Flexbox? Or what would be the recommended way?
Example code (getting images from an API):
<div class="container">
{images.map(image => {
return(
<div class="item">
<img src={image.thumbnail} />
</div>
);
})}
</div>
Example CSS:
.container {
display: flex;
flex-flow: row wrap;
}
img:nth-child(3) {
flex: 1 100%;
}
Simply set flex-basis: 100% to your third flex item
An additional rule setting the img to width: 100% will make them size properly.
Also, for best cross browser support, keep the wrapper around each img
.container {
display: flex;
flex-flow: row wrap;
}
.item {
background: lightgray;
flex-basis: calc(50% - 10px);
margin: 5px;
}
.item:nth-child(3) {
flex-basis: calc(100% - 10px);
}
.item img {
width: 100%;
}
<div class="container">
<div class="item">
<img src='http://placehold.it/300x100' />
</div>
<div class="item">
<img src='http://placehold.it/300x100' />
</div>
<div class="item">
<img src='http://placehold.it/300x100' />
</div>
<div class="item">
<img src='http://placehold.it/300x100' />
</div>
<div class="item">
<img src='http://placehold.it/300x100' />
</div>
<div class="item">
<img src='http://placehold.it/300x100' />
</div>
<div class="item">
<img src='http://placehold.it/300x100' />
</div>
</div>
Maybe i don't understand correctly what you want, but here it is :
see jsFiddle or snippet below
.container {
display: flex;
flex-flow: row wrap;
}
img {
height:auto;
flex:1 50%;
}
img:nth-child(3) {
flex: 1 100%;
}
<div class="container">
<img src="http://via.placeholder.com/150x150">
<img src="http://via.placeholder.com/150x150">
<img src="http://via.placeholder.com/150x150">
<img src="http://via.placeholder.com/150x150">
<img src="http://via.placeholder.com/150x150">
<img src="http://via.placeholder.com/150x150">
<img src="http://via.placeholder.com/150x150">
<img src="http://via.placeholder.com/150x150">
<img src="http://via.placeholder.com/150x150">
<img src="http://via.placeholder.com/150x150">
<img src="http://via.placeholder.com/150x150">
</div>
In your example you're doing img:nth-child(3) but your images are wrapped with an div. So your images are not the flex childs. Here is a possible solution to your problem:
.container {
display: flex;
flex-wrap: wrap;
}
.container div{
width: 50%;
/* just for demonstration, not needed: */
text-align:center;
margin-bottom: 10px;
background-color: #eee;
border: 2px solid white;
box-sizing: border-box;
}
.container div:nth-child(3) {
width: 100%;
}
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
Your CSS works, you just need an additional rule for the other images to get two each into the other rows, plus you can leave out the wrapping DIVs (if that's not possible/desired, just apply the same rules to the DIVs instead and set all image widths to 100%):
.container {
display: flex;
flex-flow: row wrap;
}
img {
box-sizing: border-box;
width: 50%;
padding: 10px;
}
img:nth-child(3) {
flex: 1 100%;
}
<div class="container">
<img src="http://placehold.it/500x200/ed8">
<img src="http://placehold.it/500x200/ed8">
<img src="http://placehold.it/500x200/ed8">
<img src="http://placehold.it/500x200/ed8">
<img src="http://placehold.it/500x200/ed8">
<img src="http://placehold.it/500x200/ed8">
<img src="http://placehold.it/500x200/ed8">
<img src="http://placehold.it/500x200/ed8">
</div>

line out images to the left side among each other

I have some images where I put a trach bin image on with z-index. This works. But when I resize the browser to a smaller screen the pictures slide over each other. But the intention is that they retain their original format and instead next to each other they line out to the left side among each other.
Below the code I use.
<style type="text/css">
.test {
position:absolute;
top: 0px;
left: 0px;
z-index: 1;
display: inline-block;
min-width: 160px;
min-height: 120px;
width: 160 px;
height: 120px;
}
.trashbin {
position:absolute;
top: 65px;
left: 115px;
z-index: 3;
}
</style>
<fieldset>
<div class="form-group">
<div class="col-xs-4">
<img src="/img/l16001.jpg" class="test" >
<img src="/img/trash.png" class="trashbin">
</div>
<div class="col-xs-4 test2">
<img src="/img/l16001.jpg" class="test" >
<img src="/img/trash.png" class="trashbin">
</div>
<div class="col-xs-4 test2">
<img src="/img/l16001.jpg" class="test" >
<img src="/img/trash.png" class="trashbin">
</div>
</div>
</fieldset>
Using flexboxes for layout. Will wrap when needed.
.images {
display: flex;
justify-content: space-evenly;
flex-wrap: wrap;
}
.test2 {
display: flex;
flex-wrap: no-wrap;
align-items: center;
}
.test2 img {
margin-right: .5em;
}
<fieldset>
<div class="form-group images">
<div class="col-xs-4 test2">
<img src="http://placehold.it/100">
<img src="http://placehold.it/10" class="trashbin">
</div>
<div class="col-xs-4 test2">
<img src="http://placehold.it/100">
<img src="http://placehold.it/10" class="trashbin">
</div>
<div class="col-xs-4 test2">
<img src="http://placehold.it/100">
<img src="http://placehold.it/10" class="trashbin">
</div>
</div>
</fieldset>

Resources