Not able to distribute DIVs vertically - html

I'm trying to recreate 2:1 layout from the photo below but I'm unable to distribute the right 2 photos vertically so they align perfectly with the photo on the left. I tried to put it in a flex but then they distribute vertically and I need them to be responsive.
Current Layout
How it should look like
img-row {
display: flex;
flex-wrap: wrap;
}
.gridImage1 {
width: 100%;
height: 100%;
object-fit: cover;
}
.right-photos-div {
justify-content:space-around;
}
.gridImage23 {
width:100%;
height:100%;
object-fit:cover;
justify-content: space-around;
}
<div class="container-fluid row img-row">
<div class="col-12 col-md-6">
<h5 class="grid-image1-title position-absolute"><b>GINA Smart | The new standard in filter coffee</b></h5>
<h5 class="grid-image1-price position-absolute">220€</h5>
<img src="Images/goat-story-gina-specialty-coffee-preparation-66_3_2x_cb3f02c4-e13e-4945-b38f-513e42091a4e.jpeg" class="gridImage1" alt="Gina">
</div>
<div class="col-12 col-md-6 right-photos-div">
<div class="col-12 d-inline-block">
<h5 class="grid-image2-title position-absolute"><b>COLD BREW KIT | Cold brew made easy</b></h5>
<h5 class="grid-image2-price position-absolute">22€</h5>
<img src="Images/20190902-A7R01337_2x_120dba37-6706-456a-89f5-fdfe1c56edef.jpeg" alt="" class="gridImage23">
</div>
<div class="col-12 d-inline-block">
<h5 class="grid-image2-title position-absolute"><b>GOAT MUG | Unique coffee cup on the go</b></h5>
<h5 class="grid-image2-price position-absolute">29,5€</h5>
<img src="Images/goat_mug_hemp_3_2x_dd7d99a2-21cd-4730-a623-786b47beab02.jpeg" alt="" class="gridImage23">
</div>
</div>
</div>

I managed to achieve what you're looking for with the following code:
.grid-test{
display:grid;
grid-template-columns: 1fr 1fr;
width: 100%;
height: 50vh;
}
img{
width: 100%;
object-fit: cover;
}
.left-side-image{
height: 100%;
}
.right-grid-container{
gap: 30px;
display: flex;
flex-wrap: wrap;
padding-left: 30px;
}
<div class="grid-test">
<div>
<img class="left-side-image" src="https://www.w3schools.com/css/img_lights.jpg">
</div>
<div class="right-grid-container">
<img class="right" src="https://www.w3schools.com/css/img_lights.jpg">
<img class="right" src="https://www.w3schools.com/css/img_lights.jpg">
</div>
</div>
By the logic I've changed, you should be able to do that by simply adding the following styles to your right-photos-div element:
gap: 30px;
display: flex;
flex-wrap: wrap;
padding-left: 30px;
Noting that you're transforming that element itself into a display flex element with flex-wrap so that you can add gap between items and adding a padding-left equivalent to that same gap (if you want to have equal spaces between your elements).

Related

How to make divs stack on top of each other as page is minimized

I am trying to figure out how to make a section of divs on my page responsive so that as the page is minimized, the divs begin stacking on top of each other. I've tried flex-direction: column but that doesn't seem to be working. I have attached photos of what I am trying to achieve.
Here is my HTML:
<div>
<!-- <section> -->
<center>
<div class="container">
<div class="item3">
<image-tag src="https://></image-tag>
</div>
<div class="item3">
<image-tag src="https://></image-tag>
</div>
<div class="item3">
<image-tag src="https://></image-tag>
</div>
<div class="item3 idk">
<image-tag src="https://></image-tag>
</div>
<div class="item3 idk">
<image-tag src="https://></image-tag>
</div>
</div>
</center>
<!-- </section> -->
</div>
CSS
.container {
display: flex;
flex-direction: row;
}
Photos of what I am trying to achieve
Desktop
Tablet
Mobile
FYI I have taken some code out for confidentiality purposes.
try flex-wrap: wrap;
use .item3{ flex: 1 } if needed
A flex-box solution is hard because it is meant for 1 directional layouts where as your desired layout is 2 directional based on screen width. However, you can come close to the desired layout behavior by using flex-wrap which depends on either the width of your flex-items or flex-container to force the items to a new line. Try the below and adjust either the items' or container's width
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
align-items: start;
}
You can give max-width: 1200px; to your body element then
margin:0 auto to center vertically.
Than create a flexbox inside container class and make flex-wrap: wrap; to make flexbox create new row else all the images will always stay in same row.
You can also add padding: 0 1rem; for always having empty spaces when images comes to limit of wrapping.
/* RESET */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background-color: antiquewhite;
min-height: 100vh;
max-width: 1200px;
margin: 0 auto;
}
img {
max-width: 100%;
display: block;
}
/* RESET ENDS */
.container {
display: flex;
flex-direction: row;
justify-content: center;
flex-wrap: wrap;
gap: 1rem;
padding: 0 1rem;
}
<div class="container">
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
</div>

How can I make div that contains images to have fixed size?

I'm working on a project, and I'm trying to get a fixed div container so that I can have all pictures in the div container and they will have the same size. I tried many suggested ways, but somehow it does not do anything to my div container. Nothing at all. I'm not 100% sure but I assume it's because I use bootstrap? Why am I not able to change the size?
[image1][image2]
[ image3 ]
This is how I try to have a fixed size div, but what my css does is still:
[image1][image2]
[image3]
.imagesContainer {
max-width: 200px;
max-height: 100px;
overflow: hidden;
}
.imagesContainer img {
width: 100%;
min-height: 100%;
}
<div class="album">
<div class="imagesContainer">
<div class="row justify-content-center mt-3">
<div class="col-4 px-2">
<img src="https://via.placeholder.com/150" class="img-fluid" alt="1">
</div>
<div class="col-4 px-2">
<img src="https://via.placeholder.com/150" class="img-fluid" alt="1">
</div>
</div>
</div>
<div class="imagesContainer">
<div class="row">
<div class="col mt-3"></div>
<div class="col mt-3"><img src="https://via.placeholder.com/300" alt="3"></div>
<div class="col mt-3"></div>
</div>
</div>
</div>
If you are trying to stick to Bootstrap, this solution might help
.album {
display: flex;
align-items: center;
justify-content: center;
}
.imagesContainer {
max-width: 400px;
max-height: 700px;
overflow: hidden;
}
.imagesContainer img {
width: 100%;
min-height: 100%;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="album">
<div class="imagesContainer">
<div class="row mt-3">
<div class="col px-2">
<img src="https://fujifilm-x.com/wp-content/uploads/2019/08/x-t30_sample-images02.jpg" class="img-fluid" alt="1">
</div>
<div class="col px-2">
<img src="https://fujifilm-x.com/wp-content/uploads/2019/08/x-t30_sample-images02.jpg" class="img-fluid" alt="1">
</div>
</div>
<div class="row mt-3">
<div class="col"><img src="https://fujifilm-x.com/wp-content/uploads/2019/08/x-t30_sample-images02.jpg" alt="3"></div>
</div>
</div>
</div>
You can use your own styles rather than bootstraps. So you can have 3 separate divs inside the main div and set the images as the background images. With the three separate divs, you can use display- flex on the overall image container and have them be responsive and meet your designs. Here is what I mean and I hope it helps.
.imagesContainer {
width: 800px;
height: 700px;
display: flex;
flex-wrap: wrap;
}
//overall image container
.imagesContainer {
width: 800px;
height: 700px;
display: flex;
flex-wrap: wrap;
}
//individual image div
.imageDiv {
background-image: url(${Image});
background-position: center;
background-repeat: no-repeat;
background-size: cover;
flex: 1 1 400px;
}
You can add an additional class name to the last image container and have it specifically change the background size to contain as so if you want it to stretch and fill out the empty space
.lastImageContainer{
background-size: contain;
}

Basic 3 grid layout html/css

So basically I need to create a 3 grid layout which looks like:
-------------------------------------
| Image | The name of logo | > |
-------------------------------------
I have somewhat do this with :
<div class="panel panel-default">
<div class="card-header">
<a class="card-link row" href="/webview/topup">
<p style="font-size: 14sp; margin-bottom: 0">
<img src="../images.png" th:src="#{../images/image.png}" width="10%"
height="10%" style="vertical-align: middle; font-family: 'Barlow', sans-serif;">
The name of logo
<img src="../arrow.png" th:src="#{../arrow.png}" width="2%" height="2%"
style="vertical-align: middle;" align="right";>
</p>
</a>
</div>
</div>
But somehow my arrow is not centered because it only uses align right. How can I make my arrow to be align right and centered at the same time?
Im sorry to ask such basic questions, because Im kinda new to html/css. Thanks.
There are multiple methods to achieve this.
You can use flexbox on the parent:
{
display: flex;
align-items: center;
justify-content: flex-start;
}
and add:
{
margin-left: auto;
}
to the last child. In your example, the arrow.
A good visual introduction/overview of flexbox is available here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
A concrete example of this with your code:
.card-link {
display: flex;
align-items: center;
justify-content: flex-start;
}
.arrow {
margin-left: auto;
width: 1em;
height: 1em;
}
.logo {
width: 2em;
height: 2em;
}
img {
width: 100%;
height: auto
}
<div class="panel panel-default">
<div class="card-header">
<a class="card-link row" href="/webview/topup">
<img class="logo" src="https://via.placeholder.com/150">
<p class="text">The name of logo </p>
<img class="arrow" src="https://via.placeholder.com/150">
</a>
</div>
</div>
This can be done using css "grid" or "flex". You can refer to many
articles related to 'css flex' and 'css grid'. You can easily align
items in both vertical and horizontal. And there are many ways to align items inside a grid. This will be suitable for your case.
.grid {
display: grid;
height: 100px;
background-color: #f9f9f9;
grid-template-columns: auto 1fr auto;
align-items: center;
grid-gap: 20px;
}
<div class="grid">
<div>Image</div>
<div>The name of logo</div>
<div>></div>
</div>

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 to make bootstrap columns the same height in a row?

http://www.bootply.com/somVIZEXxC# Here is the bootply for my website, basically I want the cols col-lg-6 to be the same height so that the images I have inside them are even, at the moment, one image stretches below the other one.
Edit: Current version http://www.bootply.com/UIWf6q09h4
Content should be placed within columns, and only columns may be
immediate children of rows
So get out the h1 from the .row and set the class .row-eq-height to .row like this:
.row-eq-height{
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
<div class="row row-eq-height"></div>
Here the full information http://getbootstrap.com.vn/examples/equal-height-columns/
Here a minimal snippet to illustrate:
.row-eq-height{
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<h1 class="text-center"> Where do we cater to? </h1>
<div class="row row-eq-height">
<div class="col-xs-6">
<img src="http://i.stack.imgur.com/gijdH.jpg?s=328&g=1" class="img-responsive">
</div>
<div class="col-xs-6" style="background: blueviolet">
<img src="http://orig00.deviantart.net/0cbb/f/2013/107/3/a/lnd_small_bmp_64x64_by_shadowblitz-d620m47.jpg" class="img-responsive">
</div>
</div>
Like I said I don't know the limitation of your img (height, if can be background, etc.) Here some tips to achieve what you want:
Remove the margin from row and the padding from col-, add width: 100% to your image like this:
.row-eq-height{
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
margin: 0;
}
.row-eq-height [class*="col-"]{
padding: 0;
}
.row-eq-height img{
width: 100%;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<h1 class="text-center"> Where do we cater to? </h1>
<div class="row row-eq-height">
<div class="col-xs-6">
<img src="http://i.stack.imgur.com/gijdH.jpg?s=328&g=1" class="img-responsive">
</div>
<div class="col-xs-6" style="background: blueviolet">
<img src="http://orig00.deviantart.net/0cbb/f/2013/107/3/a/lnd_small_bmp_64x64_by_shadowblitz-d620m47.jpg" class="img-responsive">
</div>
</div>
If the image can be background you can define a specific proportional height you can use padding-top, let say 4:3 that would be 3 * 100 / 4 = 75 = padding-top
So you can do this:
.row-eq-height{
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
margin: 0;
}
.row-eq-height [class*="col-"]{
padding: 0;
}
.img-container{
background-size: cover;
padding-top: 75%; /*you can change it to obtain a desired height*/
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<h1 class="text-center"> Where do we cater to? </h1>
<div class="row row-eq-height">
<div class="col-xs-6">
<div class="img-container" style="background-image: url(http://i.stack.imgur.com/gijdH.jpg?s=328&g=1)"></div>
</div>
<div class="col-xs-6">
<div class="img-container" style="background-image: url(http://orig00.deviantart.net/0cbb/f/2013/107/3/a/lnd_small_bmp_64x64_by_shadowblitz-d620m47.jpg)"></div>
</div>
</div>
just add the "equal-heights" class to the columns parent
$('.equal-heights').each(function(){
var highestBox = 0;
$(this).children('[class*="col-"]').each(function(index, el){
if( $(el).height() > highestBox ) highestBox = $(el).height();
});
$(this).children('[class*="col-"]').css('height',highestBox);
});
the html would be like this...
<div class="row equal-heights">
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
</div>
see this pen...
Try taking out your
<h1 class="text-center"> Where do we cater to? </h1>
because one "row" is meant to add up to 12. Right now, you have this h1 in the row along with your two col-lg-6, which should be in a row of their own (6+6=12).
The h1 should be in its own row with its own col-lg-12, or whatever you might want.
Did you try using max-height:?
heres a fork of your code http://www.bootply.com/wVSqmPKERL
added another row to the columns and add the class of height-fix which just adds
a max-height attribute that you can set to whatever you need.
edit
You can also combine this with min-height if the images are to small
Here is a responsive grid with images. The image containers have equal height and the images themselves are vertically centered;
.grid {
margin-top: 25px;
display: flex;
flex-wrap: wrap;
}
.grid>[class*='col-'] {
display: flex;
flex-direction: column;
align-items: stretch;
margin-bottom: 15px;
}
.grid img {
margin: auto 0;
}
#media (max-width: 767px) {
.container {
max-width: 100%;
}
}
#media (max-width: 575px) {
.container {
max-width: 100%;
padding-left: 0;
padding-right: 0;
}
.posts-grid>[class*='col-'] {
padding-left: 5px;
padding-right: 5px;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="grid">
<div class="col-sx-6 col-md-4 col-lg-2">
<img src="http://placehold.it/350x300" class="img-responsive">
</div>
<div class="col-sx-6 col-md-4 col-lg-2">
<img src="http://placehold.it/350x450" class="img-responsive">
</div>
</div>
</div>