1 image vs 2 images side by side - html

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>

Related

Can't figure how to properly center those images

Hello everyone, I'm trying to setup the main content of the homepage as shown in the image but can't really figure a few things.
Somehow everything I try results in the image to overflow the container and be as big as the page. I don't want to set a fixed size for the image, but rather have it proportional to the view height and width
This is my code right now:
<section class="main">
<div class="main-left">
<div class="container">
<img src="assets/images/wine.png">
</div>
</div>
<div class="main-right">
<div class="container">
<img src="assets/images/oil.png">
</div>
</div>
</section>
.main {
display: flex;
background-color: #f1eee9;
height: 100%;
}
.main-left, .main-right {
display: flex;
flex-direction: row;
}
.main-left {
background-color: #111;
width: 50%;
}
.main-right {
background-color: #1f1f1f;
width: 50%;
}
.container {
display: flex;
justify-content: center;
align-items: center;
width: 80vw;
height: 80vw;
}
.container img {
display: block;
width: 100%;
height: 100%;
}
I haven't yet added the text so it would be REALLY helpful if you could suggest how to do that as well..
You should use : object-fit: cover;
which is documented here : https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
With your exemple I made that (changed container height to 80vh and not vw)
.main-left, .main-right {
display: flex;
flex-direction: row;
}
.main-left {
background-color: #111;
width: 50%;
}
.main-right {
background-color: #1f1f1f;
width: 50%;
}
.container {
width: 80vw;
height: 80vh;
background-color: blue;
}
.container img {
display: block;
width: 100%;
height: 100%
object-fit: cover;
}
You can optimize your code like below it got correct:
<section class="main">
<div class="main-left">
<img src="assets/images/wine.png">
</div>
<div class="main-right">
<img src="assets/images/oil.png">
</div>
</section>
.main {
display: flex;
flex-direction: row;
}
.main-left , .main-right{
flex: 1;
}
.main-left {
background-color: #111;
}
.main-right {
background-color: #1f1f1f;
}
.main > div img {
display: block;
width: 100%;
height: 100%
object-fit: cover;
}

How can i make my box of image responsive using HTML and CSS

I'm trying to build a simple HTML and CSS card, I have a container that contains box-image and body-card, and the design work perfectly on the desktop version but when I arrived at my breakpoint of media query my image it not shrinking with the container, I don't want the image to pass the container when I change the width of the viewport. I'm looking for the best practice :D
HTML
<section class="card">
<div class="card-container">
<div class="box-img">
<img src="./images/drawers.jpg" alt="decor"/>
</div>
<div class="body-card">
<h1> Shift the overall look and feel by adding these wonderful
touches to furniture in your home</h1>
<p>Ever been in a room and felt like something was missing? Perhaps
it felt slightly bare and uninviting. I’ve got some simple tips
to help you make any room feel complete.</p>
<div class="author">
<img src="./images/avatar-michelle.jpg" alt="">
<div class="author-info">
<h4>Michelle Appleton</h4>
<small>20 April 2021</small>
</div>
</div>
</div>
</div>
</section>
CSS
body {
font-family: "Manrope", sans-serif;
margin: auto;
background-color: hsl(210, 46%, 95%);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.card-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
max-width: 1140px;
width: 95vw;
background-color: #fff;
border-radius: 15px;
}
.box-img {
position: relative;
width: 540px;
height: 442px;
}
.box-img img {
height: 100%;
width: 100%;
object-fit: cover;
overflow: hidden;
border-radius: 15px 0px 0px 15px;
}
.body-card {
position: relative;
padding: 2rem 4rem 2rem 3rem;
max-width: 600px;
}
MediaQuery
#media (max-width: 992px) {
.card-container {
flex-direction: column-reverse;
width: 65vw;
}
.box-img {
width: 602px;
height: 308px;
}
}
This is the problem
Just set your width to 100%?
body {
font-family: "Manrope", sans-serif;
margin: auto;
background-color: hsl(210, 46%, 95%);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.card-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
max-width: 1140px;
width: 95vw;
background-color: #fff;
border-radius: 15px;
}
.box-img {
position: relative;
width: 540px;
height: 442px;
}
.box-img img {
height: 100%;
width: 100%;
object-fit: cover;
overflow: hidden;
border-radius: 15px 0px 0px 15px;
}
.body-card {
position: relative;
padding: 2rem 4rem 2rem 3rem;
max-width: 600px;
}
#media (max-width: 992px) {
.card-container {
flex-direction: column-reverse;
width: 65vw;
}
.box-img {
width: 100%;
}
}
<section class="card">
<div class="card-container">
<div class="box-img">
<img src="https://images.unsplash.com/photo-1531171596281-8b5d26917d8b?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80" alt="decor"/>
</div>
<div class="body-card">
<h1> Shift the overall look and feel by adding these wonderful
touches to furniture in your home</h1>
<p>Ever been in a room and felt like something was missing? Perhaps
it felt slightly bare and uninviting. I’ve got some simple tips
to help you make any room feel complete.</p>
<div class="author">
<img src="./images/avatar-michelle.jpg" alt="">
<div class="author-info">
<h4>Michelle Appleton</h4>
<small>20 April 2021</small>
</div>
</div>
</div>
</div>
</section>
If you want the image to have the same size as the card then use width:100%;. This will change the width of the image to the width of the container.
What you have to change is written below ->
#media (max-width: 992px) {
.card-container {
flex-direction: column-reverse;
width: 65vw;
}
.box-img {
width: 602px; <--- Remove
width:100%; <---- Add
height: 308px;
}
}
You're almost there, but you need to make a few small changes:
your section really ought to have an explicit display declaration
you could give section a display: block but it makes more sense simply to move display: flex; justify-content: center; align-items: center; down from body to section
in a couple of places you've used width for elements with a flex-parent - but the whole idea is that these flex-children elements should have flexible dimensions, so use flex: [expand tendency] [shrink tendency] [start-width] instead
Working Example:
N.B. Use the Full Page link to see the full effect
body {
font-family: "Manrope", sans-serif;
margin: auto;
background-color: hsl(210, 46%, 95%);
min-height: 100vh;
}
section.card {
display: flex;
justify-content: center;
align-items: center;
}
.card-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
background-color: #fff;
border-radius: 15px;
}
.box-img {
position: relative;
display: block;
flex: 0 1 540px;
max-height: 442px;
}
.box-img img {
height: 100%;
width: 100%;
object-fit: cover;
overflow: hidden;
border-radius: 15px 0px 0px 15px;
}
.body-card {
position: relative;
padding: 2rem 4rem 2rem 3rem;
max-width: 600px;
}
#media (max-width: 992px) {
.card-container {
flex-direction: column-reverse;
width: 65vw;
}
.box-img {
flex: 0 1 602px;
max-height: 308px;
}
}
<section class="card">
<div class="card-container">
<div class="box-img">
<img src="https://picsum.photos/540/442" alt="decor"/>
</div>
<div class="body-card">
<h1> Shift the overall look and feel by adding these wonderful
touches to furniture in your home</h1>
<p>Ever been in a room and felt like something was missing? Perhaps
it felt slightly bare and uninviting. I’ve got some simple tips
to help you make any room feel complete.</p>
<div class="author">
<img src="./images/avatar-michelle.jpg" alt="">
<div class="author-info">
<h4>Michelle Appleton</h4>
<small>20 April 2021</small>
</div>
</div>
</div>
</div>
</section>
Further Reading:
https://developer.mozilla.org/en-US/docs/Web/CSS/flex
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Images falling outside container with max height

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.

[CSS Challenge]: Creating a resizable textbox that can stretch vertically and horizontally without warping corner elements

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

align two divs side by side with floating (responsive)

how can I make this container responsive so the text and the img automatically become block elements. I tried it out with flex direction but for someway it doesnt work. Can someone correct my code if necessary and suggest me a media query rule for the responsive design
<div class="top">
<h1>Welcome</h1>
<div class="portrait">
<img src="https://pixy.org/images/placeholder.png" alt="">
<h2>XXXXXXXXXX</h2>
</div>
</div>
.top h1{
display: flex;
align-items: center;
justify-content: center;
flex-grow: 1;
background-color: black;
height: 20vw;
margin-top: 0;
font-size: 5vw;
color: white;
text-shadow: 5px 5px rgb(142, 135, 136);
}
.top img {
width: 20vw;
}
thanks in advance
I think this is what you are after. display: flex; is very powerful property and useful when it comes to take up rest of the space and centering.
Modification
here is a demo, I would not suggest this approach with using max-width as it's not "mobile-first". But if this is what you want for this project then ok.
.container {
display: flex;
flex-direction: row;
background-color: deepskyblue;
}
#img {
width: 140px;
height: 140px;
}
#text {
display: flex;
align-items: center;
justify-content: center;
flex-grow: 1;
background-color: deeppink;
min-height: 100px;
}
#media screen and (max-width: 700px) {
.container {
flex-direction: column;
}
#img {
width: 100%;
height: auto;
}
}
.container {
display: flex;
background-color: deepskyblue;
}
#img {
width: 140px;
height: 140px;
}
#text {
display: flex;
align-items: center;
justify-content: center;
flex-grow: 1;
background-color: deeppink;
}
<div class="container">
<img id="img" src="https://www.archlinux.org/static/vector_tux.864e6cdcc23e.png" />
<div id="text">text on the left, next to the img</div>
</div>
Ok, well so here it is if I understood well what you are trying to accomplish. Correct me or update your question if I am wrong!
#img{
width: 200px;
height: 150px;
float: left;
}
#text{
overflow: hidden;
}
<div class="container">
<img id="img" src="https://www.archlinux.org/static/vector_tux.864e6cdcc23e.png"/>
<div id="text">text on the left, next to the img</div>
</div>