I have an image gallery and all of the images are squares. The images are in their divs and a big div with the display to flex to parent all the divs. I want the images to fill their parent div, but not make them bigger.
UPDATE: I saw on this website that the flex-grow property does NOT add to the (usable) width of the element. Go see the website for a further explanation, but that means I have to use something else than flex-grow to make my parent divs the width of the top div.
Also, I do not want a fixed amount of items on a line!! What I want is when you scale up the window, the elements get wider and wider until a point, where you then shrink the elements and add one from under to the line above.
Here is what I have:
Here is what I want:
Here is my code:
.ImagesContainer {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: .25rem;
margin-top: 1rem;
width: 100%;
}
.ImagesContainer img {
width: 20vw;
min-width: 200px;
margin: 5px;
transition-duration: 200ms;
}
.ImagesContainer div {
background-color: red;
flex-grow: 1;
}
<div class="ImagesContainer">
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
</div>
Does anybody have an idea?
UPDATE:
The method I was using (With flexbox) did not do what I wanted finally so I decided to use css grid and it did it perfectly!
Here is the new css (same html):
.ImagesContainer {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: .25rem;
width: 100%;
}
.ImagesContainer img {
width: 100%;
height: 100%;
}
.ImagesContainer a {
padding: 5px;
}
Thank you to everyone, your answers helped me understand css better!
You need to give width to image wrapper only and fit the image appropriately. If you want some part of your div be seen, you can add padding property to it.
By adding "object-fit: cover" property to your image you can scale it to fit its parent's width, and "object-position: center" in case your image is not square and a part of it gets cut.
.ImagesContainer {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: .25rem;
margin-top: 1rem;
width: 100%;
}
.ImagesContainer img {
transition-duration: 200ms;
width: 100%;
object-fit: cover;
object-position: center;
}
.ImagesContainer div {
background-color: red;
flex-grow: 1;
width: 20vw;
min-width: 200px;
padding: 5px;
}
<div class="ImagesContainer">
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
<div> <img src="https://picsum.photos/200"> </div>
</div>
Related
I am trying to create a split screen scroll.
I need to position the image from the first column exactly parallel to the image from the second column.
It has to start and finish with both images completly visible on the screen.
My problem is that when it starts and finishes the images are not aligned (a bit of one image is not visible).
It only works randomly on some resolutions.
The left column works with the default native browser scroll.
The second column which is fixed positioned is working by taking the the number of pixels scrolled then multiplied by minus one.
https://codepen.io/victoreugen2002/pen/QWdJzdB
$(window).on('scroll', function() {
$(".column-right").css('bottom', $(window).scrollTop() * -1);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="full-page">
<div class="column-left">
<div class="image">
<img src="https://picsum.photos/200/300">
</div>
<div class="image">
<img src="https://picsum.photos/200/300">
</div>
<div class="image">
<img src="https://picsum.photos/200/300">
</div>
</div>
<div class="column-right">
<div class="image">
<img src="https://picsum.photos/200/300">
</div>
<div class="image">
<img src="https://picsum.photos/200/300">
</div>
<div class="image">
<img src="https://picsum.photos/200/300">
</div>
</div>
</div>
<style>
.full-page .column-left {
margin-top: 0;
display: flex;
flex-direction: column;
justify-content: space-between;
width: 100%;
box-sizing: border-box;
}
.full-page .column-right {
justify-content: space-between;
width: 100%;
box-sizing: border-box;
padding-left: 30px;
padding-right: 30px;
width: 50%;
left: 50%;
position: fixed;
display: flex;
flex-direction: column-reverse;
bottom: 0;
}
.image {
width:496px;
}
.image img {
width: 100%;
}
</style>
I'm trying to create a 3-wide grid of equally sized images which rescale to fit the screen while maintaining a fixed height. I'm very new to html and CSS so I'm not sure how to go about resizing the images.
The basis of the html I have is:
.flexContainer {
display: flex;
height: 20vh;
}
.flexItem {
width: 33%;
height: 100%;
}
<div class="flexContainer">
<div class="flexItem">
<img src="...">
</div>
<div class="flexItem">
<img src="...">
</div>
<div class="flexItem">
<img src="...">
</div>
</div>
The questions I have are firstly am I structuring this all approximately correctly? I'm sort of just winging it up until now. And then also how do I go about styling the images to fit the box, so far everything I've tried has failed to alter the image size but that may be as I'm applying it to the wrong place or something silly.
First, remove width: 33%; on your flex items. Then add the following CSS:
.flexItem > img {
max-width: 100%;
}
See it working here:
.flexContainer {
display: flex;
justify-content: space-around;
height: 100%;
}
.flexItem {
width: 100%;
height: 100%;
text-align: center;
}
.flexItem > img {
max-width: 100%;
}
<div class="flexContainer">
<div class="flexItem">
<img src="https://dummyimage.com/300x300/000/fff">
</div>
<div class="flexItem">
<img src="https://dummyimage.com/300x300/000/fff">
</div>
<div class="flexItem">
<img src="https://dummyimage.com/300x300/000/fff">
</div>
</div>
Structure is fine. I made other style changes in your CSS but only because that is how I would do it. You can do it your way also.
I try to develop website for my portfolio. I want to do something like that :
But I've some problem with the 3 icons for Twitter/Instagram and Deviantart. To put the icons like that, I use the flexbox in html. Currently, I've that :
#conteneur {
display: flex;
justify-content: space-between;
}
<div id="conteneur">
<div class="element1">
<img src="https://zupimages.net/up/21/29/vbxh.png">
</div>
<div class="element2">
<img src="https://zupimages.net/up/19/51/4gl0.png">
</div>
<div class="element3">
<img src="https://zupimages.net/up/21/29/dbtc.png">
</div>
</div>
Test with CodePen here : https://codepen.io/lucbenedet/pen/yLbPMgK
But as you can see, the pictures are too large and when I try to put the pictures to a size of 35px like that :
#conteneur
{
display: flex;
justify-content: space-between;
width: 35px;
}
or
.element1
{
width: 35px;
}
The resize doesn't works...
Someone to show how to do that ?
You can update your CSS with following code.
#conteneur {
display: flex;
justify-content: space-between;
width: 150px;
padding: 10px;
}
#conteneur div img {
width: 35px;
height: 35px;
}
<div id="conteneur">
<div class="element1">
<img src="https://zupimages.net/up/21/29/vbxh.png">
</div>
<div class="element2">
<img src="https://zupimages.net/up/19/51/4gl0.png">
</div>
<div class="element3">
<img src="https://zupimages.net/up/21/29/dbtc.png">
</div>
</div>
You can have some space between icons when you maximize the width of the container
you can reset width on the flex-container to max-content (3 icones shouldnot overflow) and use gap to set a distance in between them.
You could use em for the gap and icon's width to have a coherent render.
Possible example:
#conteneur {
display: flex;
width: max-content;
align-items: center;
margin: auto;
gap: 1.6em;
}
#conteneur div img {
width: 3em;
}
<div id="conteneur">
<div class="element1">
<img src="https://zupimages.net/up/21/29/vbxh.png">
</div>
<div class="element2">
<img src="https://zupimages.net/up/19/51/4gl0.png">
</div>
<div class="element3">
<img src="https://zupimages.net/up/21/29/dbtc.png">
</div>
</div>
possible example from your codepen https://codepen.io/gc-nomade/pen/qBmVjBV
First things first, a fiddle: https://jsfiddle.net/2v1axtkz/3/
I have a flex grow box with a floating right image and am trying to add underneath a text box and contain them both within the parent box.
Reason for this is that there is an onclick event for the parent box, so I need both the image box and the text box contained within the same parent.
Right now it's not happening and I can't figure out why.
The markup is:
<div class="sections__container__outside" onclick="action();">
<div class="sections__container__box">
<img src="https://via.placeholder.com/150" class="image">
</div>
<div class="sections__container__box__title">Title</div>
</div>
As you can see from the image, the text is somehow outside the parent container even though it's inside of the div:
How do I fix this?
You have height: 100% on the .sections__container__box. That essentially pushes out the title container. I'm assuming you want the red in the background, so just removing that won't work.
You need to remove the float from the image and make that .sections__container__box display flex and then use the justify-content property to position the image.
.sections__container {
margin: 0 auto;
max-width: 630px;
display: flex;
flex-wrap: wrap;
align-content: stretch;
justify-content: space-between;
}
.sections__container__box {
background-color: #FF0030;
flex: 1 0 100%;
text-align: center;
width: 100% !important;
text-align: right;
display: flex;
justify-content: flex-end;
}
.sections__container__outside {
width: 40%;
flex-grow: 1;
margin: 10px;
}
.image {
display: block;
}
.sections__container__box__title {
margin-bottom: 20px;
}
<div class="sections__container">
<div class="sections__container__outside" onclick="action();">
<div class="sections__container__box">
<img src="https://via.placeholder.com/150" class="image">
</div>
<div class="sections__container__box__title">Title</div>
</div>
<div class="sections__container__outside" onclick="action();">
<div class="sections__container__box">
<img src="https://via.placeholder.com/150" class="image">
</div>
<div class="sections__container__box__title">Title</div>
</div>
<div class="sections__container__outside" onclick="action();">
<div class="sections__container__box">
<img src="https://via.placeholder.com/150" class="image">
</div>
<div class="sections__container__box__title">Title</div>
</div>
<div class="sections__container__outside" onclick="action();">
<div class="sections__container__box">
<img src="https://via.placeholder.com/150" class="image">
</div>
<div class="sections__container__box__title">Title</div>
</div>
</div>
I'm trying to have 1 image on the top with 2 images below it spanning half the width of the image on top. The problem I'm encountering are aligning issues with the 2 images in which they don't fit nicely below the top image but spaced perfectly to the sides. I'm trying to have the 2 images be below the top image perfectly aligned in the center with no spaces below. As I shrink the window, that is when it looks like I want it to on a full screen except without the spaces. I also want them perfectly aligned in the center of the screen.
https://jsfiddle.net/voryuja8/
.first {
display: flex;
}
.first img {
margin: auto;
max-width: 800px;
}
.second {
display: flex;
}
.second img {
margin: auto;
flex: 1;
max-width: 400px;
}
<div class="first">
<img src="https://sarahannephoto.files.wordpress.com/2015/01/devyn_015.jpg?w=1008" alt="">
</div>
<div class="second">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
</div>
Just remove margin:auto from the images and use justify-content: center in the flex containers.
.first {
display: flex;
justify-content: center;
}
.first img {
max-width: 800px;
}
.second {
display: flex;
justify-content: center;
}
.second img {
flex: 1;
max-width: 400px;
}
<div class="first">
<img src="https://sarahannephoto.files.wordpress.com/2015/01/devyn_015.jpg?w=1008" alt="">
</div>
<div class="second">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
</div>
This might be a bit better, as it doesn't rely on setting pixel values for anything: https://jsfiddle.net/voryuja8/1/
HTML:
<div class="second">
<div class="flex-child">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
</div>
<div class="flex-child">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
</div>
</div>
CSS:
img {
max-width: 100%;
}
.second {
display: flex;
}
.flex-child {
flex-grow: 1;
flex-shrink: 1;
}