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.
Related
I've been trying to get familiar with grid while making the sidebar, and I encountered the problem where my grid items/children aren't equal to each-other in height even though they're supposed to be the same size.
* {
margin: 0;
padding: 0;
}
.mainContainer {
height: 500px;
gap: 30px;
display: grid;
grid-row-template: repeat(auto-fill, 1fr);
background-color: black;
color: white;
justify-items: center;
align-items: start;
}
.mainContainer div {
display: grid;
align-content: center;
justify-content: center;
width: 60%;
border: 1px solid yellow;
height: 60%;
border-radius: 10px;
}
.mainContainer img {
height: 50%;
width: 100%;
object-fit: contain;
}
<div class="mainContainer">
<div> <img src="https://pbs.twimg.com/profile_images/1455185376876826625/s1AjSxph_400x400.jpg"> </div>
<div> Box 1 </div>
<div> Box 2 </div>
<div> Box 3 </div>
</div>
Focusing on the image
.mainContainer img{
height: 30px; // set it to any size
object-fit: contain;
}
I think you should use px instead of %
You can use this, it is done without grid, but with a flex-column.
* {
margin: 0;
padding: 0;
}
.mainContainer{
height: 500px;
display: flex;
flex-direction: column;
align-items: center;
gap: 30px;
background-color: black;
color: white;
}
.mainContainer > * {
height: 25%;
width: 60%;
border: 1px solid yellow;
/* center image / text in children boxes */
display: flex;
justify-content: center;
align-items: center;
}
.mainContainer img {
object-fit: contain;
}
Can some please tell me how to add 1 image side-by-side as shown in this image, I mean with complete responsiveness
.container {
margin: auto;
width: 78vw;
height: 50vh;
background-color: aqua;
border-radius: 10%;
display: flex;
flex-direction: row;
justify-content: space-between;
flex-wrap: wrap;
}
.big-image {
background: url("https://media.istockphoto.com/photos/nothing-better-than-sliding-into-bed-with-a-good-read-picture-id929998552?b=1&k=20&m=929998552&s=170667a&w=0&h=GkJP5lHXoyVLYDIa86G6stcyEvoaAgN7xBFqgrc3QH8=");
width: 65%;
height: auto;
background-size: contain;
background-repeat: no-repeat;
}
.twoSmallImages {
display: flex;
flex-direction: column;
justify-content: start;
width: 33%;
height: 100%;
}
.img1 {
background: url("https://media.istockphoto.com/photos/nothing-better-than-sliding-into-bed-with-a-good-read-picture-id929998552?b=1&k=20&m=929998552&s=170667a&w=0&h=GkJP5lHXoyVLYDIa86G6stcyEvoaAgN7xBFqgrc3QH8=");
width: 100%;
height: 45%;
background-size: contain;
background-repeat: no-repeat;
margin: 2px auto;
background-color: #fff;
}
.img2 {
background: url("https://media.istockphoto.com/photos/nothing-better-than-sliding-into-bed-with-a-good-read-picture-id929998552?b=1&k=20&m=929998552&s=170667a&w=0&h=GkJP5lHXoyVLYDIa86G6stcyEvoaAgN7xBFqgrc3QH8=");
width: 100%;
height: 45%;
background-size: contain;
background-repeat: no-repeat;
margin: 2px auto;
background-color: brown;
}
<div class="container">
<div class="big-image"></div>
<div class="twoSmallImages">
<div class="img1"></div>
<div class="img2"></div>
</div>
</div>
First ask correct question, then use this code and change your customize.. and
.box{
display: grid;
grid-template-columns: 2fr 1fr;
grid-template-rows:1fr 1fr;
width: 600px;
background:red;
gap:1rem;
padding: 1rem
}
.img{
width:100%;
height:100%
}
.img01{
grid-column:1/2;
grid-row:1/3
}
.img02{
grid-column:2/3;
grid-row:1/2
}
.img03{
grid-column:2/3;
grid-row:2/3
}
<div class="box">
<img class="img img01" src="https://picsum.photos/id/237/600/400" title="" alt="">
<img class="img img02" src="https://picsum.photos/id/238/600/400" title="" alt="">
<img class="img img03" src="https://picsum.photos/id/239/600/400" title="" alt="">
</div>
You can use display flex or grid. In this example I have used flex. If you want to use grid then let me know. The code is responsive so click on full page to see the layout that you have requested.
.container {
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
gap: .5rem;
}
.fragment {
width: 100%;
display: flex;
}
.fragment:nth-child(2) {
flex-direction: column;
gap: .5rem;
}
.image {
height: 100%;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
#media only screen and (min-width: 1024px) {
.container {
flex-direction: row;
}
.fragment:nth-child(2) {
flex-direction: column;
gap: .5rem;
}
.image {
height: 50%;
}
}
<div class="container">
<div class="fragment">
<img src="https://media.istockphoto.com/photos/nothing-better-than-sliding-into-bed-with-a-good-read-picture-id929998552?b=1&k=20&m=929998552&s=170667a&w=0&h=GkJP5lHXoyVLYDIa86G6stcyEvoaAgN7xBFqgrc3QH8=" alt="image" />
</div>
<div class="fragment">
<div class="image">
<img src="https://media.istockphoto.com/photos/smart-student-learning-using-internet-and-headphones-picture-id1128717611" alt="image" />
</div>
<div class="image">
<img src="https://media.istockphoto.com/photos/shopping-online-concept-shopping-service-on-the-online-web-with-by-picture-id1133980246" alt="image">
</div>
</div>
</div>
This layout looks like a grid.
You can specify the grid template areas so that the big image covers basically two thirds of the width and the smaller images one third.
This snippet sets the size of the background images to cover so that the full grid looks like in the picture given in the question whatever the viewport size. However, this will of course not always show complete images and if it is important to have complete images whatever the various aspect ratios then go back to using contain though you will sometimes get space either top and bottom or at the sides of an image.
Note that the HTML and the CSS have been simplified to make things clearer.
.container {
margin: auto;
width: 78vw;
height: 50vh;
background-color: aqua;
border-radius: 10%;
display: grid;
grid-template-areas:
'a a b'
'a a c';
grid-gap: 1vw;
}
.container div {
background-size: cover;
background-position: center center;
}
.big-image {
background: url("https://media.istockphoto.com/photos/nothing-better-than-sliding-into-bed-with-a-good-read-picture-id929998552?b=1&k=20&m=929998552&s=170667a&w=0&h=GkJP5lHXoyVLYDIa86G6stcyEvoaAgN7xBFqgrc3QH8=");
grid-area: a;
}
.img1 {
background: url("https://media.istockphoto.com/photos/nothing-better-than-sliding-into-bed-with-a-good-read-picture-id929998552?b=1&k=20&m=929998552&s=170667a&w=0&h=GkJP5lHXoyVLYDIa86G6stcyEvoaAgN7xBFqgrc3QH8=");
background-color: #fff;
grid-area: b;
}
.img2 {
background: url("https://media.istockphoto.com/photos/nothing-better-than-sliding-into-bed-with-a-good-read-picture-id929998552?b=1&k=20&m=929998552&s=170667a&w=0&h=GkJP5lHXoyVLYDIa86G6stcyEvoaAgN7xBFqgrc3QH8=");
background-color: brown;
grid-area: c;
}
<div class="container">
<div class="big-image"></div>
<div class="img1"></div>
<div class="img2"></div>
</div>
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.
I am creating a resizable textbox that can stretch vertically and horizontally without warping graphical corner elements. To do so, I am using three vertical sections (top, center, bottom) and three horizontal sections (left, middle, right) within the top and bottom vertical sections. This way, the 'top-middle' and 'bottom-middle' sections can stretch horizontally and the center section can stretch vertically & horizontally, while the corner sections (top-left, top-right, bottom-left..) stay the same width and height to avoid warping.
The problem is: positioning elements so that they line up with one another. Specifically, I seem to be getting some cut-off on the right sides of my corner elements.
Here's a screenshot of the issue:
https://postimg.cc/Xp4dRDrQ
Here is the HTML:
<div className='textbox-container'>
<div className='top-block'>
<div className='left-block' />
<div className='middle-block'>
<p>
Top Block
</p>
</div>
<div className='right-block' />
</div>
<div className='center-block'>
<p>
Center Block
</p>
</div>
<div className='bottom-block'>
<div className='left-block' />
<div className='middle-block'>
<p>
Bottom Block
</p>
</div>
<div className='right-block' />
</div>
</div>
And here is the CSS I am using:
.textbox-container {
display: flex;
flex-flow: column;
align-items: center;
width: 75%;
margin: 0 auto;
}
.top-block {
display: inline-flex;
flex-direction: row;
float: left;
width: 100%;
height: 80px;
margin:0 auto;
margin-top: 10vh;
}
.top-block .left-block {
background-image: url('/src/images/Textbox-Top-Left.png');
background-repeat: no-repeat;
background-size: cover;
width: 150px;
height: 80px;
margin:0 auto;
}
.top-block .middle-block {
width: 100%;
height: 80px;
line-height: 80px;
text-align: right;
vertical-align: middle;
background-image: url('/src/images/Textbox-Top-Middle.png');
color: #fff;
margin:0 auto;
}
.top-block .right-block {
float:right;
background-image: url('/src/images/Textbox-Top-Right.png');
background-repeat: no-repeat;
background-size: cover;
width: 150px;
height: 80px;
margin:0 auto;
}
.center-block {
display: flex;
flex-flow: column;
align-items: center;
justify-content: center;
width: 100%;
height: 800px;
background-image: url('/src/images/Textbox-Center.png');
background-repeat: repeat-y;
background-size: contain;
margin: 0;
}
.bottom-block {
display: inline-flex;
flex-direction: row;
float: left;
width: 100%;
height: 80px;
margin-bottom: 20vh;
}
/* Close but no cigar with magic numbers in bot sections: */
.bottom-block .left-block {
width: 220px;
height: 80px;
background-image: url('/src/images/Textbox-Bottom-Left.png')
}
.bottom-block .middle-block {
width: 100%;
line-height: 80px;
text-align: right;
padding-right: 5vw;
vertical-align: middle;
height: 80px;
background-image: url('/src/images/Textbox-Bottom-Middle.png');
color: #fff;
margin: none;
}
.bottom-block .right-block {
float:right;
width: 220px;
height: 80px;
background-image: url('/src/images/Textbox-Bottom-Right.png')
}
Fiddle (currently not working):
https://jsfiddle.net/edmundw/6xku4qwa/6/
Fiddle collaborate invite:
https://jsfiddle.net/edmundw/6xku4qwa/4/#&togetherjs=4VMz2rGNAo
Any solutions would be greatly appreciated as I am stumped.
Many thanks for reading this far,
Betty.
Hm, I got something working by using actual img elements (which have an inherent width and height) for the individual sections, along with flexbox (specifically flex-basis, flex-grow and flex-shrink).
The only problem I can see is that the center element's background's borders are blurry. Not sure how to fix that, but other than that, it works. No border cutoff.
* {
padding: 0px;
margin: 0px;
box-sizing: border-box;
}
.textbox-container {
width: min-content;
height: auto;
overflow: hidden;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: center;
}
.textbox-vertical-block {
width: 100%;
max-width: 100%;
height: 80px;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: center;
}
.textbox-vertical-block>* {
width: auto;
height: 100%;
object-fit: fill;
}
.textbox-vertical-block>*:nth-child(2) {
flex-basis: auto;
flex-grow: 1;
flex-shrink: 0;
}
.textbox-center-block {
height: auto;
align-items: stretch;
background-image: url(https://i.postimg.cc/d3p7Nt38/Textbox-Center.png);
background-size: 100% 100%;
background-repeat: repeat;
}
.textbox-center-block.textbox-vertical-block>* {
height: initial;
}
.textbox-center-block>.textbox-block {
width: 6px;
}
.textbox-center-block>textarea {
width: auto;
height: auto;
flex-basis: auto;
flex-grow: 1;
flex-shrink: 1;
margin: 16px;
padding: 16px;
}
<div class="textbox-container">
<div class="textbox-vertical-block">
<img class="textbox-block" src="https://i.postimg.cc/jDn9G5wH/Textbox-Top-Left.png" />
<img class="textbox-block" src="https://i.postimg.cc/qtfY0Ty9/Textbox-Top-Middle.png" />
<img class="textbox-block" src="https://i.postimg.cc/8FhYz0bs/Textbox-Top-Right.png" />
</div>
<div class="textbox-vertical-block textbox-center-block">
<img class="textbox-block" src="https://i.postimg.cc/xcrz8TS0/Textbox-Center-Right.png" />
<textarea></textarea>
<img class="textbox-block" src="https://i.postimg.cc/xcrz8TS0/Textbox-Center-Right.png" />
</div>
<div class="textbox-vertical-block">
<img class="textbox-block" src="https://i.postimg.cc/kD70BqzC/Textbox-Bottom-Left.png" />
<img class="textbox-block" src="https://i.postimg.cc/CdJW89pq/Textbox-Bottom-Middle.png" />
<img class="textbox-block" src="https://i.postimg.cc/dL1gjnJS/Textbox-Bottom-Right.png" />
</div>
</div>
EDIT #1: Removed the blur by using the same method used in the top and bottom sections (extra border and center images (the center image is 1x1 though, so could be easily replaced by background-color)). More flexbox foolery too
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>