Leading images in flexbox containers - html

I've been building flexbox containers - actually rows of them - and in some there were leading images. To my surprise something unexpected happens every time I put in one row flexbox containers with leading images and containers of other type (where image doesn't come first).
HTML:
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bam</span>
</div>
<div class="container">
<span>Bom</span>
<span>Bom</span>
</div>
<div class="container">
<span>Bim</span>
<span>Bim</span>
</div>
CSS:
.container {
display: inline-flex;
background-color: tomato;
}
.image {
height: 28px;
}
Please see the following example: https://codepen.io/GuRuGu/pen/KeYGwv
I'd really like to know why it happens as I'm at a loss right now.

Alex's answer is probably the best one, as it's more flexible, but this is a quick alternative: if your images are always a fixed height (in the demo they are 28px) you can make the container elements the same height, and align them with vertical-align (because they are inline elements):
.container {
height: 28px;
vertical-align: bottom;
}

I don't know if it's a solution for you, maybe I misunderstood the issue. But in my opinion, if you want to align all containers, you can wrap all containers with a wrapper and add it a display : flex;. With that configuration, maybe you don't need a display: inline-flex on containers anymore. I let them on the code below, because I don't know what are your limitations.
Here is your code with that modification. I add a .supracontainer class on a div wrapper around your containers. You can control the vertical alignment by using align-items flexbox property on supracontainer class.
.container {
display: inline-flex;
background-color: tomato;
align-items: stretch;
}
.supracontainer{
display: flex;
}
.image {
height: 28px;
}
<h2>Leading images</h2>
<div class="supracontainer">
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bam</span>
</div>
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bom</span>
</div>
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bim</span>
</div>
</div>
<h2>Leading image & empty containers</h2>
<div class="supracontainer">
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bam</span>
</div>
<div class="container">
<span>Bom</span>
<span>Bom</span>
</div>
<div class="container">
<span>Bim</span>
<span>Bim</span>
</div>
</div>
<h2>Trailing image & empty containers</h2>
<div class="supracontainer">
<div class="container">
<span>Bam</span>
</div>
<div class="container">
<span>Bom</span>
</div>
<div class="container">
<span>Bim</span>
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
</div>
</div>
<h2>Leading images & trailing images</h2>
<div class="supracontainer">
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bam</span>
</div>
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bom</span>
</div>
<div class="container">
<span>Bim</span>
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
</div>
<div class="container">
<span>Bim</span>
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
</div>
</div>

You can change item order within a flex element using the order property:
.container {
display: inline-flex;
background-color: tomato;
align-items: stretch;
}
.supracontainer{
display: flex;
}
.image {
height: 28px;
order:0;
}
span{
order:1;
}
<h2>Leading images</h2>
<div class="supracontainer">
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bam</span>
</div>
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bom</span>
</div>
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bim</span>
</div>
</div>
<h2>Leading image & empty containers</h2>
<div class="supracontainer">
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bam</span>
</div>
<div class="container">
<span>Bom</span>
<span>Bom</span>
</div>
<div class="container">
<span>Bim</span>
<span>Bim</span>
</div>
</div>
<h2>Trailing image & empty containers</h2>
<div class="supracontainer">
<div class="container">
<span>Bam</span>
</div>
<div class="container">
<span>Bom</span>
</div>
<div class="container">
<span>Bim</span>
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
</div>
</div>
<h2>Leading images & trailing images</h2>
<div class="supracontainer">
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bam</span>
</div>
<div class="container">
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
<span>Bom</span>
</div>
<div class="container">
<span>Bim</span>
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
</div>
<div class="container">
<span>Bim</span>
<img class="image" src="https://res.cloudinary.com/gurugumawaru/image/upload/v1522765892/FCC_Game_Of_Life_resized_ddofex.png"/>
</div>
</div>

Related

How to center multiple images using Bootstrap?

I'm using Bootstrap and trying to make 3 images to be centered. For example, red color boxes are how my images look like, and I want to make it look like blue color boxes:
I tried getting rid of the margin and padding, but I still want some space between the images. Also I tried using { display: table; }, display: table-cell; vertical-align: middle; but somehow it does not do anything to my images... Is there a way to get images to the center with some padding to them in Bootstrap? Below is my code:
<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">
.row {
display: flex;
align-items: center;
justify-content: center;
}
.column {
flex: 33.33%;
padding: 50px;
width: 100%;
}
<div class="album text-center">
<div class="row">
<div class="column">
<img src="images/tour1.jpeg" alt="one" style="width:100%">
</div>
<div class="column">
<img src="images/tour2.jpg" alt="two" style="width:100%">
</div>
<div class="column">
<img src="images/tour3.jpeg" alt="three" style="width:100%">
</div>
<div class="column">
<img src="images/tour1.jpeg" alt="one" style="width:100%">
</div>
<div class="column">
<img src="images/tour2.jpg" alt="two" style="width:100%">
</div>
<div class="column">
<img src="images/tour3.jpeg" alt="three" style="width:100%">
</div>
</div>
</div>
Thank you!
Bootstrap 4 has justify-content-around class which does the job. Consider to use narrower container If you want to obtain smaller gap between images.
PS: You should avoid using col class within image wrappers.
<div class="container">
<div class="row justify-content-around">
<div class="colx"><img src="https://place-hold.it/200x400" alt="" class="img-fluid"></div>
<div class="colx"><img src="https://place-hold.it/200x400" alt="" class="img-fluid"></div>
<div class="colx"><img src="https://place-hold.it/200x400" alt="" class="img-fluid"></div>
</div>
</div>
JSFiddle
Just add this class "justify-content-around".
<div class="album">
<div class="row justify-content-around">
<div class="column">
<img src="images/tour1.jpeg" alt="one" style="width:100%">
</div>
<div class="column">
<img src="images/tour2.jpg" alt="two" style="width:100%">
</div>
<div class="column">
<img src="images/tour3.jpeg" alt="three" style="width:100%">
</div>
<div class="column">
<img src="images/tour1.jpeg" alt="one" style="width:100%">
</div>
<div class="column">
<img src="images/tour2.jpg" alt="two" style="width:100%">
</div>
<div class="column">
<img src="images/tour3.jpeg" alt="three" style="width:100%">
</div>
</div>
</div>

Center images in a <section>

I have a <section> tag in which there are 3 images. How can I center them for fullscreens?
The size of my screen is 22.9 ".
here is the code:
<section class="gallery">
<div class="container-fluid">
<div class="row">
<div class="fw mix-container home-gallery">
<div class="mix landscape portrait">
<div class="item-img">
<img src="images/parfum-aux-agrumes-002.jpg""/>
</div>
</div>
<div class="mix landscape portrait">
<div class="item-img">
<img src="images/parfum-aux-agrumes-003.jpg""/>
</div>
</div>
<div class="mix landscape portrait">
<div class="item-img">
<img src="images/parfum-aux-agrumes-004.jpg""/>
</div>
</div>
</div>
</div>
</div>
Thank you for your answers
Best regards.
I have included a code snippet below that will run.
Essentially you make the images inline-block so they sit next to each other.
Then you can set the images parent container to be left 50%, and offset it with transform:translate();
You can change the width of the images as needed but the same logic should still apply.
.mix {
width: 200px;
height: 100px;
border: black solid 1px;
display: inline-block;
}
.mix-container {
position: relative;
display: inline-block;
left: 50%;
transform: translate(-50%, 0);
}
<section class="gallery">
<div class="container-fluid">
<div class="row">
<div class="fw mix-container home-gallery">
<div class="mix landscape portrait">
<div class="item-img">
<img src="images/parfum-aux-agrumes-002.jpg"/>
</div>
</div>
<div class="mix landscape portrait">
<div class="item-img">
<img src="images/parfum-aux-agrumes-003.jpg"/>
</div>
</div>
<div class="mix landscape portrait">
<div class="item-img">
<img src="images/parfum-aux-agrumes-004.jpg"/>
</div>
</div>
</div>
</div>
</div>
</section>

Horizontally aligning elements in a div

I am trying to align three elements in a div in such a way that the image is inline with h1 while the p tag is beneath the h1 tag.
.valign{
position:relative;
top: 50%;
transform: translateY(-50%);
vertical-align: middle;
}
.banner{
position: relative;
height: 100vh;
width:100vw;
background: #eee;
}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="banner" style="display:flex">
<div class="container">
<div class="valign">
<img src="img/logo.png" style="height: 150px;width: 150px">
<h1>Anirudh Sharma</h1>
<p>This is my portfolio</p>
</div>
</div>
</div>
</div>
</div>
Here is an image for reference:
This is how the elements must be arranged
This is how the elements are looking right now:
This is a screenshot of the webpage
Any help would be appreciated.
Try this
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="banner" style="display:flex">
<div class="container">
<div class="valign">
<div class="float-xs-left float-sm-left float-md-left">
<img src="img/logo.png"></div>
<div class="float-xs-left float-sm-left float-md-left">
<h1>Anirudh Sharma</h1>
<p>This is my portfolio</p></div>
</div>
</div>
</div>
</div>
</div>
the class value for float is based on bootstrap 4
Try this html
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="banner" style="display:flex">
<div class="container">
<div class="valign">
<div style="float:left">
<img src="img/logo.png" style="height: 150px;width: 150px"></div>
<div style="float:left">
<h1>Anirudh Sharma</h1>
<p>This is my portfolio</p></div>
</div>
</div>
</div>
</div>
</div>
Use floats, Bootstrap provides pull-left and pull-right helper classes, so use it on the two parts of your layout to have them side-by-by.
You need to add some margins here and there to make it look good.
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="banner" style="display:flex">
<div class="container">
<div class="valign">
<div class="row">
<div class="pull-left">
<img src="img/logo.png" style="height: 150px;width: 150px">
</div>
<div class="pull-left">
<h1>Anirudh Sharma</h1>
<p>This is my portfolio</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
https://jsfiddle.net/n8o1w3cq/

Avoid images to stack on mobile site

I just started learning coding and I decided to design my website using squarespace and www.rociodelfa.com.
My main problem is that on my inspiration page I have a gallery of images that need to follow a cohesive order and I want to fix the amount of images per line but it is not the same for every line.
When opened on a mobile device the images are stacked. How do I change that?
Check the code I have attached.
* {
box-sizing: border-box;
}
.wrapper {
width: 20%; /* Let's assume you want 5 pictures in a row, so 100%/5 = 20% */
float: left;
padding: 10px;
}
.wrapper img {
width: 100%;
}
<div class="wrapper">
<img src="http://placehold.it/300x300">
</div>
<div class="wrapper">
<img src="http://placehold.it/300x300">
</div>
<div class="wrapper">
<img src="http://placehold.it/300x300">
</div>
<div class="wrapper">
<img src="http://placehold.it/300x300">
</div>
<div class="wrapper">
<img src="http://placehold.it/300x300">
</div>
<div class="wrapper">
<img src="http://placehold.it/300x300">
</div>
<div class="wrapper">
<img src="http://placehold.it/300x300">
</div>
<div class="wrapper">
<img src="http://placehold.it/300x300">
</div>
<div class="wrapper">
<img src="http://placehold.it/300x300">
</div>
<div class="wrapper">
<img src="http://placehold.it/300x300">
</div>

making images appear in uniform sizes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my website, I have a gallery which displays user uploaded images. The problem is , since we dont have a control over the dimensions of the images uploaded by the user, different images are appearing in different dimensions . What is the best approach to make the images appear in uniform sizes using html? or is it best done in the jquery/javascript?
If you don't want to crop your images server-side but still want some level of uniformity then you could use the max-width and max-height properties in CSS and a small snippet of jQuery to do the job for you. This won't change the original image, but will shift it within it's container so it is centered but fills the frame.
You then need to decide the width and height that all thumbnails will be and they will overflow their boundaries centrally aligned and ultimately uniform.
$(window).on("load", function() {
orientateGalleryImages($("#contentGallery").children().children().children("img"));
});
function orientateGalleryImages(images) {
images.each(function() {
var thisImage = jQuery(this);
if(thisImage.height() > thisImage.width()) {
thisImage.addClass("portrait");
} else if(thisImage.width() > thisImage.height()) {
thisImage.addClass("landscape");
} else {
thisImage.addClass("square");
}
});
}
.galleryArea {
background: rgba(0,0,0,0.7);
padding: 10px;
overflow: hidden;
}
.galleryArea .imageWrapper {
position: relative;
width: 10%;
padding-top: 10%;
overflow: hidden;
float: left;
}
.galleryArea .imagePosition {
position: absolute;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
}
.galleryArea .imagePosition img {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.galleryArea .imagePosition img.landscape {
max-height: 50%;
height: 50%;
}
.galleryArea .imagePosition img.portrait {
max-width: 50%;
width: 50%;
}
.galleryArea .imagePosition img.square {
max-width: 50%;
max-height: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="contentGallery" class="galleryArea">
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x250">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/150x400">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x150">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/150x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x150">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/150x400">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x150">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/150x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/150x400">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x150">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/150x400">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x150">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x300">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/300x150">
</div>
</div>
<div class="imageWrapper">
<div class="imagePosition">
<img src="http://placehold.it/150x300">
</div>
</div>
</div>
DEMO
HTML:
The HTML is a little long winded, but it is required to offset the image inside the parent.
JS:
The JavaScript looks at the longest edge of the image and adds a class to it of either Portrait, Landscape or Square. These classes specify either a max-width, max-height or both depending on the orientation.
One way of doing this is wrap all images in a 'div'.
HTML :
<div class="img_parent">
<img />
</div>
<div class="img_parent">
<img />
</div>
CSS :
.img_parent{ width:100px; height:100px; overflow:hidden; display:inline-block;}
.img_parent img{ width:100%;}
The solution to this, is to give them fixed heights in css. (example img{ height:100px; } )
When users upload the images, make it automatically crop that image to a specific size eg. 400px x 400px
The demo below is just a crop idea, but you can modify the code to make it automatically crop it to a size of eg 400px x 400px
Croppic example
This is just a thought. Hope you come right.