I have multiple images in a container and would like them to fit into one container but the problmem is that I can't change the width and the height as I'm currently making an app that makes it dynamic.
As you can see I put a red border for the container, the right border is missing.
This is what I got:
.container {
display: flex;
width: 450px;
height: 300px;
border: 2px solid red;
}
.container img {
max-width: 100%
}
<div class="container">
<img src="https://cdn.hasselblad.com/ec67f4db463750c394c4e720acedf6b506b55b48_x1d-ii-sample-01-web.jpg">
<img src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
<img src="https://www.fujifilmusa.com/products/digital_cameras/x/fujifilm_x20/sample_images/img/index/ff_x20_008.JPG">
</div>
How can I make these images fit inside the red border?
You can try this with flex: 1 CSS property.
.container {
display: flex;
width: 450px;
height: 300px;
border: 2px solid red;
}
.container div {
flex: 1;
}
.container img {
max-width:100%;
}
<div class="container">
<div>
<img src="https://cdn.hasselblad.com/ec67f4db463750c394c4e720acedf6b506b55b48_x1d-ii-sample-01-web.jpg">
</div>
<div>
<img src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg"> </div>
<div>
<img src="https://www.fujifilmusa.com/products/digital_cameras/x/fujifilm_x20/sample_images/img/index/ff_x20_008.JPG">
</div>
</div>
Here is how I would recommend doing this, you can keep all the images in one line, it's responsive, and calculated:
.section {
width: 100%;
margin: 0 auto;
}
.section .section-inner {
width: 100%;
max-width: 1248px;
margin: 0 auto; /* centers container */
padding: 10px; /* this combined with the margin in the divs will make the margins appear consistent */
box-sizing: border-box;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
flex-direction: row; /* sort into rows */
align-items: center;
justify-content: space-between;
text-align: center;
background-color: rgba(255,100,100,0.25);
}
.section .section-inner div {
width: calc((100% / 4) - 20px); /* calculate width for maximum accuracy | minus margin */
padding-top: calc((100% / 4) - 20px); /* calculate width for maximum accuracy | minus margin */
margin: 10px; /* space between boxes */
background-size: cover;
background-position: center;
}
#media screen and (min-width: 640px) and (max-width: 1023px) {
.section .section-inner div {
width: calc((100% / 2) - 20px); /* calculate width for maximum accuracy | minus margin */
padding-top: calc((100% / 2) - 20px); /* calculate width for maximum accuracy | minus margin */
}
}
#media screen and (max-width: 639px) {
.section .section-inner div {
width: 100%; /* calculate width for maximum accuracy | minus margin */
padding-top: 100%; /* calculate width for maximum accuracy | minus margin */
}
}
.image-one {
background-image: url(https://www.fairtrade.org.uk/~/media/FairtradeUK/Media%20Centre/Flowers.jpg?h=397&la=en&mw=760&w=760);
}
.image-two {
background-image: url(https://www.floraqueen.com/blog/wp-content/uploads/2017/11/Untitled-design-3-1.jpg);
}
.image-three {
background-image: url(https://www.elimaysflowers.co.uk/assets/uploads/elimays1.jpg);
}
.image-four {
background-image: url(https://cdn.mos.cms.futurecdn.net/AsaMYX4F9WRNVTLsARRAsR-1920-80.jpg);
}
<section class="section">
<div class="section-inner">
<div class="image-one"></div>
<div class="image-two"></div>
<div class="image-three"></div>
<div class="image-four"></div>
</div>
</section>
If you want them to keep scaling and not go responsive, remove the responsive CSS.
You can use this code
body {
margin: 0;
}
.container {
display: flex;
width: 450px;
height: 300px;
border: 2px solid red;
padding: 0px;
}
.container img {
max-width: 100%;
float: left;
width: 33.3%;
}
<div class="container">
<img src="https://cdn.hasselblad.com/ec67f4db463750c394c4e720acedf6b506b55b48_x1d-ii-sample-01-web.jpg">
<img src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
<img src="https://www.fujifilmusa.com/products/digital_cameras/x/fujifilm_x20/sample_images/img/index/ff_x20_008.JPG">
</div>
You can use this way.
.container {
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
width: 1170px;
border: 1px solid red;
}
<div class="container">
<img src="https://cdn.hasselblad.com/ec67f4db463750c394c4e720acedf6b506b55b48_x1d-ii-sample-01-web.jpg" width="304" height="236">
<img src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg" width="304" height="236">
<img src="https://www.fujifilmusa.com/products/digital_cameras/x/fujifilm_x20/sample_images/img/index/ff_x20_008.JPG" width="304" height="236">
</div>
You should add like this
.container {
display: flex;
width: 450px;
height: 300px;
border: 2px solid red;
}
.container div{
width:33.33%;
}
.container img {
max-width: 100%
}
<div class="container">
<div>
<img src="https://cdn.hasselblad.com/ec67f4db463750c394c4e720acedf6b506b55b48_x1d-ii-sample-01-web.jpg">
</div>
<div>
<img src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
</div>
<div>
<img src="https://www.fujifilmusa.com/products/digital_cameras/x/fujifilm_x20/sample_images/img/index/ff_x20_008.JPG">
</div>
</div>
Related
I have a container that contains 3 images. I want the image on the left to take up 100% of the container height, and the two on the right to take up 50% each and be stacked.
However when I set a max height on my gallery container of 400px, as the images are taller they fall out.
How can I fix this?
HTML
<div class="container">
<div class="gallery">
<div class="main-image">
<img src="https://via.placeholder.com/710x533" />
</div>
<div class="sub-image">
<img src="https://via.placeholder.com/357x266" />
<img src="https://via.placeholder.com/357x266" />
</div>
</div>
</div>
css
.container {
max-width: 1100px;
margin: 0px auto;
padding: 1rem;
}
.gallery {
display: flex;
flex-flow: row wrap;
max-height:400px;
border: 5px solid red;
};
.main-image {
img {
height: 100%;
}
flex: 1 0 66%;
min-height: 500px;
background-size: cover;
background-position: 50% 50%;
}
.sub-image {
flex: 1 0 33%;
flex-direction: column;
justify-content: stretch;
align-items: stretch;
height: 100%;
};
I have the code in codepen
https://codepen.io/roynev123/pen/abWPQyw
You can achieve what you're looking for with the following CSS:
.container {
max-width: 1100px;
margin: 0px auto;
padding: 1rem;
}
.gallery {
border: 5px solid red;
display: flex;
justify-content: space-around;
max-height: 400px;
}
img {
display: block;
height: 100%;
object-fit: cover;
object-position: center;
width: 100%;
}
.main-image {
flex-shrink: 0;
width: 66.67%;
}
.sub-image img {
height: 50%;
}
Basically, .gallery acts a a flexbox container, with .main-image having a static width.
I have structure like this:
img {
width: auto;
height: 200px;
}
.cards {
display: flex;
justify-content: center;
margin-top: 2em;
width: 80%;
flex-wrap: wrap;
}
.card {
margin: 1em;
box-shadow: 0 0 6px #000;
object-fit: cover;
}
.info {
padding: 1em;
border-top: none;
}
<div class='cards'>
<div class="card">
<img src="https://picsum.photos/id/1004/5616/3744" alt="1004" />
<div class="info">
<h3>Greg Rakozy</h3>
<div><small>https://unsplash.com/photos/SSxIGsySh8o</small></div>
</div>
</div>
</div>
on computers with long width image is rendered a little wrong.
how can I fix this so that it displays correctly, i.e. sticks to the '.card' block?
First you need to limit the width of you main container:
.cards {
display: flex;
justify-content: center;
margin-top: 2em;
width: 80%;
flex-wrap: wrap;
max-width: 1440px; /* whatever you desire */
margin-left: auto; /* center the container */
margin-right: auto; /* center the container */
}
Then each image should take 100% for it's container:
.card {
margin: 1em;
box-shadow: 0 0 6px #000;
object-fit: cover;
flex: 0 0 25%; /* each card will be 25% width */
}
img {
width: 100%;
height: 200px;
object-fit: cover;
}
Adding those to .card class
width: 100%;
height: auto;
Google how to make image responsive with css, it's not related to React.
.card {
width: 100%;
height: auto;
}
You can either put those images in a div.img-container and set the div width & height like this.
.img-container {
width: 100%;
height: // as you want;
}
and then put that image inside .img-container and set the image width to 100%.
.container {
width: 350px;
height 350px;
border: 2px solid black;
border-radius: 5px;
}
.container .img-container {
width: 100%;
height: 200px;
}
.container .img-container img {
width: 100%;
height: 100%;
}
.container .card-info {
width: 100%;
height: auto;
}
<div class="container">
<div class="img-container">
<img src="https://picsum.photos/200">
</div>
<div class="card-info">
<h5>Title</h5>
<small>Your link here</small>
</div>
</div>
and either set image width 100% and height auto.
HTML:
<div class="vertical-flexbox">
<div id="card">
<div id="image-wrapper">
<img src="assets/myImage.png" alt="picture">
</div>
</div>
</div>
CSS:
.vertical-flexbox {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
#card {
background-color: gray;
margin: 20px;
padding: 20px;
min-height: 0;
min-width: 0;
height: 80%;
display: flex;
}
#image-wrapper {
flex: 1;
overflow: hidden;
}
img {
height: 100%;
}
div#card element has dynamic height equals to 80% of body height.
Height of img should be decided by flexbox and its width should be based on height to maintain original aspect ratio
div#card width should be dependent on img width to make it fit.
How can I achieve this effect?
If .vertical-flexbox can be sized as height: 100vh; or some amount of px the image's object-fit: contain; should work as you need:
.vertical-flexbox {
height: 100vh; /* <-- here some 'hard' value */
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
#card {
background-color: gray;
margin: 20px;
padding: 20px;
min-height: 0;
min-width: 0;
height: 80%;
display: flex;
}
#image-wrapper {
flex: 1;
overflow: hidden;
height: 100%;
}
img {
height: 100%;
object-fit: contain; /* <-- to save aspect ratio */
}
<div class="vertical-flexbox">
<div id="card">
<div id="image-wrapper">
<img src="https://picsum.photos/600/1000" alt="picture">
</div>
</div>
</div>
I've developed the following layout using the Flexbox CSS model:
html {
box-sizing: border-box;
overflow-y: none;
}
*,
*:before,
*:after {
margin: 0;
padding: 0;
box-sizing: inherit;
}
#all {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
background: #03a9f4;
}
#app {
width: 500px;
background: #ffffff;
border: solid 6px yellow;
}
header {
display: flex;
justify-content: center;
align-items: center;
border: solid 4px blue;
}
#header-para-container {
flex: 1;
}
#header-image-container {
flex: 0.6;
background: #888;
}
#header-image-container > img {
display: block;
width: 100%;
height: auto;
}
section {
display: flex;
flex-wrap: wrap;
border: solid 4px tomato;
}
figure {
display: flex;
justify-content: center;
align-items: center;
flex-basis: 50%;
border: solid 4px green;
}
figure > img {
max-width: 80%;
height: auto;
}
<div id="all">
<div id="app-container">
<div id="app">
<header>
<div id="header-para-container">
<p>Hello, this is CSS flexbox layout. Thank you for visiting this page.</p>
</div>
<div id="header-image-container">
<img src="http://placekitten.com/g/300/300" />
</div>
</header>
<section id="grid-images">
<figure>
<img src="http://placekitten.com/g/300/300" />
</figure>
<figure>
<img src="http://placekitten.com/g/300/300" />
</figure>
<figure>
<img src="http://placekitten.com/g/300/300" />
</figure>
<figure>
<img src="http://placekitten.com/g/300/300" />
</figure>
</section>
</div>
</div>
</div>
https://jsfiddle.net/petebere/fsLpw1vb/.
Would you know how to reduce responsively the height of the images and the main display element (in yellow frame) as the viewport height decreases?
The idea is that the element in yellow frame should be always fully visible and there should be no need for vertical scrolling.
At the moment when the viewport height drops, the height of the yellow container stays constant. This is probably because the browser wants to keep the sizes that I've applied to the container of the header image:
#header-image-container {
...
flex: 0.6;
...
}
and to the containers of the grid images:
figure {
...
flex-basis: 50%;
...
}
I've produced this graphic to show what I'm trying to achieve when the window height is reduced:
The images aren't scaling because they aren't bound by a fixed height on their immediate containers.
For example, the first image in the layout (in the header section), has neither a parent (.header-image-container) nor a grandparent (header) with a defined height. Here's the actual code:
header {
display: flex;
justify-content: center;
align-items: center;
border: solid 4px blue;
}
#header-image-container {
flex: 0.6;
background: #888;
}
With the code above, the image has no need to be responsive. There is no height confinement.
Try this instead:
header {
display: flex;
justify-content: center;
align-items: center;
border: solid 4px blue;
height: 20vh; /* NEW */
}
#header-image-container {
flex: 0.6;
background: #888;
height: 100%; /* NEW */
}
#header-image-container > img {
/* display: block; */
/* width: 100%; */
/* height: auto; */
height: 100%; /* NEW */
}
revised demo
The same concept above applies to the #grid-images section:
section {
display: flex;
flex-wrap: wrap;
border: solid 4px tomato;
height: 60vh; /* NEW */
}
figure {
display: flex;
justify-content: center;
align-items: center;
flex-basis: 50%;
border: solid 4px green;
height: 50%; /* NEW */
}
figure > img {
/* max-width: 80%; */
/* height: auto; */
height: 100%; /* NEW */
}
revised demo
Additional notes:
In your original code you're trying to make your images responsive with width: 100%; height: auto (in the header section) and max-width: 80%; height: auto (in the grid section). This set-up (using percentage widths) is more suited for horizontal screen re-sizing. Your question seeks vertical re-sizing, so use percentage heights instead.
In your original layout, you may have noticed that when you reduce the screen height, the layout disappears at the top of the screen, with no access via vertical scroll. This is a known issue with flexbox. The solution is to use margin: auto on the centered flex item, instead of justify-content: center; align-items: center on the flex container. Full details here: Can't scroll to top of flex item that is overflowing container
Alright, so i have an element with the class called .price within the element .item as shown here. Now what I want is to have .price centered horizontally and it being pushed/pulled to the bottom border of its parent, like so.
now here comes the fun part which makes this hard:
pure css (no javascript/jquery)
it needs to be dynamic, so the width/height property of .price can fluctuate (see snippet)
here's how far i got:
.item {
width: 200px;
height: 400px; /* not static */
background: wheat;
}
.product-image {
margin: 0 auto;
width: 180px;
height: 300px;
background: lightskyblue;
}
.price {
margin: 0 auto;
width: 50px; /* not static */
height: 20px; /* not static */
background: indianred;
}
<div class="item">
<div class="product-image">
</div>
<div class="price">
</div>
</div>
Thanks in advance.
Here you go
.item {
width: 200px;
height: 400px;
/* not static */
background: wheat;
position: relative;
}
.product-image {
margin: 0 auto;
width: 180px;
height: 300px;
background: lightskyblue;
}
.price {
width: 50px;
/* not static */
height: 20px;
/* not static */
background: indianred;
position: absolute;
bottom: 8px;
left: 50%;
transform: translateX(-50%);
}
<div class="item">
<div class="product-image">
</div>
<div class="price">
</div>
</div>
You can achieve this with flexbox.
Steps:
Set your .item element as a flex-container with display: flex; and change its direction using flex-direction: column;
Since your flex-direction is set to column, you can use align-self: center; to horizontally center your .price element (now a flex-item), and finally use margin-top: auto; to position it to the bottom.
.item {
width: 200px;
height: 400px;
/* not static */
background: wheat;
display: flex;
flex-direction: column;
}
.product-image {
margin: 0 auto;
width: 180px;
height: 300px;
background: lightskyblue;
}
.price {
align-self: center;
margin-top: auto;
width: 50px;
/* not static */
height: 20px;
/* not static */
background: indianred;
}
<div class="item">
<div class="product-image">
</div>
<div class="price">
</div>
</div>
You should use relative positioning for your parent div and absolute in child divs.
My example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div class="item">
<div class="product-image"> </div>
<div class="price">
</div>
</div>
</body>
</html>
.item {
width: 500px;
height: 400px; /* not static */
background: wheat;
position:relative;
}
.product-image {
margin: 0 auto;
width: 200px;
height: 300px;
background: lightskyblue;
}
.price {
margin: 0 auto;
width: 50px; /* not static */
height: 20px; /* not static */
background: indianred;
position: absolute;
bottom: 0;
left: 50%;
}
Relative positioning on the parent container (item) and absolute positioning on the child containers.
.item {
width: 200px;
height: 400px; /* not static */
background: wheat;
position: relative;
}
.product-image {
margin: 0 auto;
width: 90%;
height: 75%;
background: lightskyblue;
}
.price {
margin: auto 39%;
width: 25%; /* not static */
height: 5%; /* not static */
background: indianred;
position: absolute;
bottom: 3%;
}
<div class="item">
<div class="product-image">
</div>
<div class="price">
</div>
</div>
Or instead of setting the margin you could just use the left or right properties of positioning to move it center. If you want price to be flush with the bottom just change the bottom percentage to '0'.
The other idea that comes to mind is creating another div container just for price and relatively positioning it at the bottom of your parent container, absolutely positioning the price div and than simply using text-align in the parent. Although I haven't actually tried this yet.