Flexbox - Laying out a photo section - html

My goal is to get the section looking like this:
(https://i.gyazo.com/7dd160aeadd2ed2c5f696e9cfd5158e3.png)
This is what my current code is giving me:
(https://i.gyazo.com/1fb61e98783f823f3bd003d1ffec3bf8.png)
I'm a beginner and struggle laying laying out sections. How would i get the my current code to give me what i want in the my goal image. Thanks.
My HTML for the image section
<div class="mid-section">
<div class="left-side">
<img src="340h.png" alt="">
</div>
<div class="right-side">
<img src="160hx140.png" alt="">
<img src="160hx140.png" alt="">
<img src="160hx140.png" alt="">
<img src="160hx140.png" alt="">
</div>
</div>
My CSS for the image section:
.mid-section{
width: 760px;
background-color: gray;
display: flex;
align-items: center;
justify-content: center;
margin: auto;
}
.right-side, .left-side{
justify-content: space-around;
}
.left-side img{
width:350px;
height:280px;
margin-top: 0;
}
.right-side img{
width:165px;
height:135px;
margin: 0px 20px 20px 0px;
}
.right-side{
width: 380px;
align-items: center;
justify-content: center;
}
.left-side{
width: 380px;
align-items: center;
justify-content: center;
}

CSS-grid is good for this. I simplified your HTML a bit for brevity...
HTML
<div class="mid-section">
<img src="340h.png" alt="">
<img src="160hx140.png" alt="">
<img src="160hx140.png" alt="">
<img src="160hx140.png" alt="">
<img src="160hx140.png" alt="">
</div>
CSS
.mid-section {
display: grid;
grid-template-columns: 2fr 1fr 1fr;
grid-column-gap: 1em;
grid-row-gap: 1em;
}
.mid-section img:first-child {
grid-row-start: 1;
grid-row-end: span 2;
}
img {
border: red solid 1px;
min-height: 100px;
}
Everything you need to know about grid: https://css-tricks.com/snippets/css/complete-guide-grid/#prop-grid-column-row

Here's a basic example using CSS Grid which implements the layout you're after. I don't need to specify the grid placement for .item1 (the large image) because the placement is implicit. Also note that I use display: inline-grid, so that the width of the overall grid is as wide as the content that fills it, not 100% of the row.
.grid {
display: inline-grid;
grid-template-columns: 350px repeat(3, 1fr);
grid-template-rows: 135px 135px;
grid-gap: 10px;
}
.item2, .item4 {
grid-column: 2 / 3;
}
.item3, .item5 {
grid-column: 3 / 4;
}
<div class="grid">
<img class="item1" src="http://placekitten.com/350/280" alt="">
<img class="item2" src="http://placekitten.com/165/135" alt="">
<img class="item3" src="http://placekitten.com/165/135" alt="">
<img class="item4" src="http://placekitten.com/165/135" alt="">
<img class="item5" src="http://placekitten.com/165/135" alt="">
</div>
jsFiddle

First I'd recommend reading:
Introduction to the CSS basic box model
Next we need to break down what it is we are trying to achieve. It's typically easiest to break down a layout by rows and columns and do any spacing between elements last. Since there is an image that spans across two rows, to me, it would be easier to start with a columns. I'm starting with a border to help visually see if the layout is working as intended.
| Col 1 | Col 2 | Col 3 |
To Achieve this with flexbox:
div {
border: 2px solid blue;
}
div > div {
border: 2px solid red;
}
.d-flex {
display: flex;
}
.flex-row {
flex-direction: row;
}
<div class="d-flex flex-row">
<div> Col 1 </div>
<div> Col 2 </div>
<div> Col 3 </div>
</div>
Now we can break down columns into rows for the additional spaces required:
| Col 1 | Col 2 Row 1 | Col 3 Row 1 |
| Col 1 | Col 2 Row 2 | Col 3 Row 2 |
div {
border: 1px solid blue;
}
div > div {
border: 2px solid red;
}
div > div > div {
border: 3px solid green;
}
.d-flex {
display: flex;
}
.flex-row {
flex-direction: row;
}
.flex-column {
flex-direction: column;
}
<div class="d-flex flex-row">
<div> Col 1 </div>
<div class="d-flex flex-column">
<div> Col 2 Row 1</div>
<div> Col 2 Row 2</div>
</div>
<div class="d-flex flex-column">
<div> Col 3 Row 1</div>
<div> Col 3 Row 2</div>
</div>
</div>
Here are some assumptions Ill work against since they were not included at the time I wrote this answer; the spacing between these elements and the container elements is all the same, and we are not trying to dynamically change the images sizes to be scaled as the window grows or shrinks (we won't resize the images to fit in the space allocated, they are of a static size).
Basically My Opinion of Margins vs Padding:
Margins - When you want to separate elements from each other.
Padding - When you want the contents of your elements to be separated from the elements border.
There are many subtle differences between the two which I won't go over here. Another advantage of margins in this scenario is that we only need to apply them to the inner most containers. Since we are in a flexbox model we need to do some semi-tricky stuff to get the margins to all align correctly.
div {
border: 1px solid blue;
}
div > div {
border: 2px solid red;
}
div > div > div {
border: 3px solid green;
}
.d-flex {
display: flex;
}
.flex-row {
flex-direction: row;
}
.flex-column {
flex-direction: column;
}
.m-20 {
margin: 20px;
}
.mtr-20 {
margin: 20px 20px 0 0;
}
.mtrb-20 {
margin: 20px 20px 20px 0;
}
<div class="d-flex flex-row">
<div class="m-20"> Col 1 </div>
<div class="d-flex flex-column">
<div class="mtr-20"> Col 2 Row 1</div>
<div class="mtrb-20"> Col 2 Row 2</div>
</div>
<div class="d-flex flex-column">
<div class="mtr-20"> Col 3 Row 1</div>
<div class="mtrb-20"> Col 3 Row 2</div>
</div>
</div>
Now we can see that all the borders are lining up as needed. Now we can remove the border and insert any images and see if everything will work as intended.
.d-flex {
display: flex;
}
.flex-row {
flex-direction: row;
}
.flex-column {
flex-direction: column;
}
.m-20 {
margin: 20px;
}
.mtr-20 {
margin: 20px 20px 0 0;
}
.mtrb-20 {
margin: 20px 20px 20px 0;
}
<div class="d-flex flex-row">
<div class="m-20"><img src="https://via.placeholder.com/150x220.png"/></div>
<div class="d-flex flex-column">
<div class="mtr-20"><img src="https://via.placeholder.com/150x100.png"/></div>
<div class="mtrb-20"><img src="https://via.placeholder.com/150x100.png"/></div>
</div>
<div class="d-flex flex-column">
<div class="mtr-20"><img src="https://via.placeholder.com/150x100.png"/></div>
<div class="mtrb-20"><img src="https://via.placeholder.com/150x100.png"/></div>
</div>
</div>
Now we notice that the images aren't quite aligned even though we've been very careful to get it pixel perfect. This is because images are display: inline by default and you can read all about those affects on the question Image inside div has extra space below the image.
So instead of inline we'll set them to flex.
.d-flex {
display: flex;
}
.flex-row {
flex-direction: row;
}
.flex-column {
flex-direction: column;
}
.m-20 {
margin: 20px;
}
.mtr-20 {
margin: 20px 20px 0 0;
}
.mtrb-20 {
margin: 20px 20px 20px 0;
}
img {
display: flex;
}
<div class="d-flex flex-row">
<div class="m-20"><img src="https://via.placeholder.com/150x220.png"/></div>
<div class="d-flex flex-column">
<div class="mtr-20"><img src="https://via.placeholder.com/150x100.png"/></div>
<div class="mtrb-20"><img src="https://via.placeholder.com/150x100.png"/></div>
</div>
<div class="d-flex flex-column">
<div class="mtr-20"><img src="https://via.placeholder.com/150x100.png"/></div>
<div class="mtrb-20"><img src="https://via.placeholder.com/150x100.png"/></div>
</div>
</div>

Related

How to left-align items with each other in a grid that used justify-items: center

I am using grid. I centered my items but i want left position to be same on cross axis.
.box {
display: grid;
grid-template-columns: 1fr;
padding: 10px;
border: 1px solid blue;
gap: 1rem;
justify-items: center;
}
img {
width: 200px;
}
<div class="box">
<img src="img/featured.jpg">
<div class="item2">Item 2 Lorem</div>
</div>
Note : i want solution in only grid. There is many temporary fixes for that but i don't want that because i am looking for a perfect standard grid alignment solution for it.
You can do it a few ways:
1. Fixed Width Text (Not Responsive)
Based on the information you gave in your question. you can simply set the width of the item2 div to be the same as the image, e.g.:
.box {
display: grid;
grid-template-columns: 1fr;
padding: 10px;
border: 1px solid blue;
gap: 1rem;
justify-items: center;
}
.item2,
img {
width: 200px;
}
<div class="box">
<img src="https://via.placeholder.com/200x150">
<div class="item2">Item 2 Lorem</div>
</div>
2. The Responsive Way
This will allow us to responsively set up the 2 separate alignments you require: centre the container element, and left-align its contents, e.g.
<div class="box">
<div class="boxcontent">
Content here...
</div>
</div>
CSS: .boxcontent { text-align: left; }
Why this works:
You must have a defined relationship between the image and text, otherwise there is no way to tell the grid that these 2 individual elements must be positioned in relation to each other. You can do this by putting them in a container.
If you think about it, you are trying to do 2 separate alignments here:
make the image and text be centred in relation to the grid but also
make them be in alignment with each other so they are left aligned.
Putting them in a container achieves both of these objectives by creating a relationship between the image and text so that:
they act as a single unit in relation to the grid and also
allow them to be positioned in relation to each other regardless of the grid
Working Example:
.box {
display: grid;
grid-template-columns: 1fr;
padding: 10px;
border: 1px solid blue;
gap: 1rem;
justify-items: center;
}
.boxcontent {
text-align: left;
}
img {
width: 200px;
}
<div class="box">
<div class="boxcontent">
<img src="https://via.placeholder.com/200x150">
<div class="item2">Item 2 Lorem</div>
</div>
</div>
.box {
display: block;
margin: 0 auto;
width: max-content;
padding: 10px;
border: 1px solid blue;
}
img {
width: 200px;
}
.item2 {
margin-top: 1rem;
}
<div class="box">
<img src="pic_trulli.jpg" alt="Hi dear">
<div class="item2">Item 2 Lorem</div>
</div>

How do I make one column in my flex table occupy less width than the others?

I'm trying to create an HTML table using DIVs and the flex CSS property. Roughly, all my rows look like
<div class='row'>
<div class='col'>
<div class='data-cell'>
Col A
</div>
</div>
<div class='col'>
<div class='data-cell'>
Col B
</div>
</div>
<div class='col'>
<div class='data-cell'>
Col C
</div>
</div>
<div class='col'>
<div class='data-cell'>
<div class='img-wrapper'>
<img src='images/delete.png' title='Delete' />
</div>
</div>
</div>
Below is the CSS I'm using
.row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 100%;
padding: 15px 0 15px 30px;
border-bottom: 1px solid $lightGrey
}
.col {
display: flex;
flex-direction: col;
flex-basis: 100%;
flex: 1;
}
.header-row {
color: $eclipse;
font-weight: bold;
}
.img-wrapper {
width: 100%;
text-align: right;
}
Right now the cells in each row take up 1/4th of the row width. What I would like to change is make the col with the image only be the size of the image and have the remaining rows even split up the remaining space. How do I do that using DIVs and flex?

How to not wrap an item on a new row? [duplicate]

This question already has answers here:
Is it possible for flex items to align tightly to the items above them?
(5 answers)
Closed 3 years ago.
As the picture describes, I want to wrap items as such.
This is my current HTML and CSS.
<div class="column-container">
<div class="col item">1 <- More text and thus taller than the other ones</div>
<div class="col item">2</div>
<div class="col item">3</div>
<div class="col item">4</div>
<div class="col item">5</div>
</div>
.column-container {
display: flex;
justify-content: center;
flex-flow: row wrap;
height: 100%;
}
.item {
height: fit-content;
min-width: 300px;
max-width: 300px;
margin: 10px;
}
Here's a fiddle as well..
https://jsfiddle.net/3Ly5zh4n/1
Flexbox is probably not the best choice for this since flexbox is used to display content next to each other either vertical or horizontal. I'd suggest using CSS Grid instead. It might be a new area for some, but it's a quite good choice for handling columns in CSS.
The following is an example of how it can be used. The method repeat(auto-fill, ...) fills the whole container with either a full fraction for each element, or the minimum width of 150px, which should be 300px in your case.
.column-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
grid-template-rows: 1fr 1fr;
grid-gap: 10px;
height: 300px;
}
.item {
display: flex;
justify-content: center;
align-items: center;
font-size: 36px;
color: white;
background-color: red;
}
.item--first {
grid-row: 1 / span 2;
}
<div class="column-container">
<div class="item item--first">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
</div>
I'd suggest reading css tricks A Complete Guide to Grid for further information. Hope this helps a bit.
.column-container {
display: flex;
flex-flow: row wrap;
height: 100%;
}
.item {
height: fit-content;
min-width: 150px;
max-width: 150px;
margin: 10px;
border: 1px solid black;
}
<div style="display: flex;justify-content: center">
<div class="column-container">
<div class="col item" style="height: 100px;">1 <- More text and thus taller than the other ones</div>
</div>
<div class="column-container">
<div class="col item">2</div>
<div class="col item">3</div>
<div class="col item">4</div>
<div class="col item">5</div>
</div>
</div>
I think this this will do what you want. Its a simpler approach but it behaves the way you explain in your requested image.
HTML:
<div>
<ul>
<!-- I have set the height of this li to 300px to demo the concept. -->
<li class="col item" style="height: 300px">
1 More text and thus taller than the other ones.
</li>
<li class="col item">2</li>
<li class="col item">3</li>
<li class="col item">4</li>
<li class="col item">5</li>
</ul>
</div>
CSS:
ul {
padding: 0;
}
ul .item {
list-style: none;
float: left;
height: 100px;
width: 300px;
margin: 10px;
border: 1px solid red;
}
This should give you a result of:
Result layout
Hope this helps...

How can I get even heights of unknown elements inside a column? [duplicate]

This question already has answers here:
Equal height flex items in flex columns
(3 answers)
Closed 5 years ago.
I'm using flex to create even columns and vh to make them the same height. That's working fine but inside the columns I can have an x number of items in them. I'd like for elements in each column to be even height depending on how many items are present (using css).
1 = 100%
2 = 50%
3 = 33.33%
etc.
I know I can do this through JS but I'd like to automate this through css via flex, grid, or something elese.
I've tried replicating your problem. Use flex: 1 on .items so that each and every item take equal space (according to the problem statement).
Have a look at the snippet below:
body {
margin: 0;
}
.parent {
width: 80%;
padding: 20px;
display: flex;
border: 1px solid red;
}
.child {
flex: 1;
display: flex;
flex-direction: column;
align-items: stretch;
justify-content: flex-end;
padding: 20px;
border: 1px solid blue;
height: 60vh;
}
.item {
flex: 1;
background: lightGreen;
border: 1px solid green;
}
<div class="parent">
<div class="child">
<div class="item">33.33%</div>
<div class="item">33.33%</div>
<div class="item">33.33%</div>
</div>
<div class="child">
<div class="item">50%</div>
<div class="item">50%</div>
</div>
<div class="child">
<div class="item">100%</div>
</div>
</div>
Hope this is what you are trying to achieve.
This is all you need to make it work with the Flexbox:
.flex-container {
display: flex;
}
.flex-item {
display: flex;
flex-direction: column;
flex: 1;
}
.item {
flex: 1;
border: 1px solid;
}
<div class="flex-container">
<div class="flex-item">
<div class="item">1/1</div>
</div>
<div class="flex-item">
<div class="item">1/2</div>
<div class="item">1/2</div>
</div>
<div class="flex-item">
<div class="item">1/3</div>
<div class="item">1/3</div>
<div class="item">1/3</div>
</div>
<div class="flex-item">
<div class="item">1/4</div>
<div class="item">1/4</div>
<div class="item">1/4</div>
<div class="item">1/4</div>
</div>
</div>

How to use `flex: grow` on floating content?

I have a header with 2 rows of 2 Foundation columns of content, as below:
<div class="wrapper">
<div class="header row">
<div class="large-6 columns">
HEADER
</div>
<div class="large-6 columns">
menu
</div>
</div>
<div class="row">
<div class="large-5 none show-for-medium columns info">
Some information to the left
</div>
<div class="large-7 columns">
<div class="image-container">
<div class="image">
image to the right
</div>
</div>
</div>
</div>
</div>
The .header height is dynamic and not set. I want the .image element to take up 100% of the remaining vertical space.
eg:
To that affect I have tried using flex and flex-grow, eg:
.wrapper {
display: flex;
flex-direction: column;
min-height: 100vh;
width: 100%;
}
.image-container {
flex-grow: 1;
}
but had no luck, see fiddle: https://jsfiddle.net/9kkb2bxu/46/
Would anyone know how I could negate the dynamic height of the header from the 100vh of the image container?
.wrapper {
display: flex;
flex-direction: column;
min-height: 100vh;
width: 100%;
border: 1px solid black;
background-color: #ccc;
}
.row {
width: 100%;
}
.header {
background-color: green;
}
.info {
background-color: yellow;
border: 1px solid #ccc;
}
.image-container {
border: 1px solid black;
display: flex;
}
.image {
background-color: red;
flex-grow: 1;
width: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.1/css/foundation.min.css" rel="stylesheet"/>
<div class="wrapper">
<div class="header row">
<div class="large-6 columns">
<h1>
HEADER
</h1>
</div>
<div class="large-6 columns">
<h1>
menu
</h1>
</div>
</div>
<div class="row">
<div class="large-5 none show-for-medium columns info">
Some information to the left
</div>
<div class="large-7 columns">
<div class="image-container">
<div class="image">
image to the right
</div>
</div>
</div>
</div>
</div>
Set the second row to take up the rest of the remaining height with flex: 1 and make sure you nest that flex with display: flex:
.row.target-row {
flex: 1;
display: flex;
}
Set the .image-container to 100% height of its column parent.
.image-container {
height: 100%;
}
By default both columns will expand. Stop the left column from expanding with:
.large-5 {
align-self: flex-start;
}
(flex-start reference: https://stackoverflow.com/a/40156422/2930477)
Complete Example
.wrapper {
display: flex;
flex-direction: column;
min-height: 100vh;
width: 100%;
border: 1px solid black;
background-color: #ccc;
}
.row {
width: 100%;
}
.header {
background-color: green;
}
.info {
background-color: yellow;
border: 1px solid #ccc;
}
.image-container {
height: 100%;
background-color: red;
}
.large-5 {
align-self: flex-start;
}
.row.target-row {
flex: 1;
display: flex;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.1/css/foundation.min.css" rel="stylesheet" />
<div class="wrapper">
<div class="header row">
<div class="large-6 columns">
<h1>
HEADER
</h1>
</div>
<div class="large-6 columns">
<h1>
menu
</h1>
</div>
</div>
<div class="row target-row">
<div class="large-5 none show-for-medium columns info">
Some information to the left
</div>
<div class="large-7 columns">
<div class="image-container">
<div class="image">
image to the right
</div>
</div>
</div>
</div>
</div>
flex-grow only applies to flex children.
.image-container isn't a direct child of a display: flex element, so that property has no effect.
Plus, it affects the flex axis, which is not what you want.
Instead, you need to put those two elements in their own flex row, and use align-items (on the parent) and align-self (on either child) so that the first one aligns (on the cross axis) to flex-start (stick to top) and the second one to stretch.
You'll also want that flex row (parent) to have flex-grow: 1 so that it stretches along the vertical flex axis of its parent (.wrapper) to fill the rest of the page (otherwise, the grandchild will have nothing to stretch to).
For more information, read a good flex tutorial.
div.wrapper > div:not(.header).row {
flex: 1; /* 1 */
display: flex; /* 1 */
}
div.large-7.columns {
display: flex; /* 2 */
}
div.image-container { /* 3 */
flex: 1;
}
div.large-5.show-for-medium { /* 4 */
align-self: flex-start;
}
jsFiddle
Notes:
flex container and items consume all remaining height of respective parents
give children full height (via align-items: stretch initial setting)
flex item consumes all available width
yellow box does not need to expand to full height; now set to content height