Fully responsiove images in 3 div's side by side - html

So I want these 3 div's to work side by side and the images to be responsive. when I try to set max-width or something it breaks up. Help me out on this one.
.fl {
display: flex;
flex-wrap: no-wrap;
height: 35%;
flex-direction: row;
}
.pic {
width: 34%;
}
.caro {
/* border: 2px solid black; */
height: 100%;
width: 100%;
display: block;
/* z-index: ; */
}
.caro img {
height: 100%;
width: 100%;
filter: brightness(0.5);
transition: all .3s;
}
.caro img:hover {
filter: brightness(1);
}
<div class="fl">
<div class="pic">
<img src="https://via.placeholder.com/1920x1080.jpg" alt="">
</div>
<div class="pic">
<img src="https://via.placeholder.com/1920x1080.jpg" alt="">
</div>
<div class="pic">
<img src="https://via.placeholder.com/1920x1080.jpg" alt="">
</div>
</div>

I would suggest two things:
Use calc to get a third with calc(100% / 3), instead of setting the width to a total of (3*34=) 102%.
Use object-fit: cover on the images to make them fit. I used different sizes on the images, so it's obvious that they are scaling.
Don't use all for transition, because that can cause janky animations due to the browser having to loop through all animatable properties. Always specify what you animate; it's easier to understand the code as well.
Using percentage for height doesn't automatically take the parent's height, so I had to improvise and use vh (viewport height).
body {
margin: 0px;
min-height: 100vh;
}
.fl {
display: flex;
flex-wrap: no-wrap;
height: 100vh;
max-height: 35vh;
}
.fl > .pic {
flex-basis: calc(100% / 3);
}
.caro img {
height: 100%;
width: 100%;
object-fit: cover;
filter: brightness(0.5);
transition: filter .3s;
}
.caro img:hover {
filter: brightness(1);
}
<div class="fl">
<div class="pic">
<img src="https://picsum.photos/100/300">
</div>
<div class="pic">
<img src="https://picsum.photos/50/80.jpg" alt="">
</div>
<div class="pic">
<img src="https://picsum.photos/100/100" alt="">
</div>
</div>

Related

Flexbox container slightly taller than its contents

I have a flex container with 4 columns/flex items, each containing an image of the same size/proportions with the max-width set to 100%. Columns have an overflow property set to hidden since I want to scale images on hover but don't want them to expand outside their column's "boundaries". However, the container's height is 382px while the image height is 379.5px leaving a subtle gap of 2.5px on the bottom when hovering image. Please refer to the modified screenshot below (red line marks the bottom edge of the container, second/lighter image is in hover state) as well as the code.
I've tried setting the flex container's align-items property to center and I've tried removing whitespace in HTML, among other things, to no avail. I am trying to wrap my head around what might be causing this discrapency. From what I understand, the height of elements in CSS is determined by their children so I don't get why it's 382px in the first place if the height of each image is 379.5px.
Any help would be greatly apprecaited, thank you!
P.S. I'm using Eric Meyer's CSS reset.
<div class="flex">
<div class="flex-item">
<img src="./img/img01.jpg" alt="Image">
</div>
<div class="flex-item">
<img src="./img/img02.jpg" alt="Image">
</div>
<div class="flex-item">
<img src="./img/img03.jpg" alt="Image">
</div>
<div class="flex-item">
<img src="./img/img04.jpg" alt="Image">
</div>
</div>
.flex {
display: flex;
column-gap: 1em;
}
.flex-item {
flex: 1;
overflow: hidden;
}
.flex-item > img {
max-width: 100%;
transform: scale(1);
transition: all 0.2s ease-in-out;
}
.flex-item:hover > img {
transform: scale(1.125);
}
All what you need, just put to the .flex-item class display: flex.
.flex-item {
display: flex; /* new line */
flex: 1;
overflow: hidden;
}
*,
::after,
::before {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.flex {
display: flex;
column-gap: 1em;
border: 1px solid red;
}
.flex-item {
display: flex; /* new line */
flex: 1;
overflow: hidden;
}
.flex-item > img {
max-width: 100%;
transform: scale(1);
transition: all 0.2s ease-in-out;
}
.flex-item:hover > img {
transform: scale(1.125);
}
<div class="flex">
<div class="flex-item">
<img src="https://plchldr.co/i/400x400?&bg=ece2d0&fc=000" />
</div>
<div class="flex-item">
<img src="https://plchldr.co/i/400x400?&bg=ece2d0&fc=000" />
</div>
<div class="flex-item">
<img src="https://plchldr.co/i/400x400?&bg=ece2d0&fc=000" />
</div>
<div class="flex-item">
<img src="https://plchldr.co/i/400x400?&bg=ece2d0&fc=000" />
</div>
</div>
Expample 2
.flex {
display: flex;
column-gap: 1em;
border: 1px solid red;
overflow: hidden;
}
img {
width: 100%;
height: 100px;
transition: all 0.2s ease-in-out;
}
.flex img:hover {
transform: scale(1.125);
}
<div class="flex">
<img src="https://plchldr.co/i/400x400?&bg=ece2d0&fc=000" />
<img src="https://plchldr.co/i/400x400?&bg=ece2d0&fc=000" />
<img src="https://plchldr.co/i/400x400?&bg=ece2d0&fc=000" />
<img src="https://plchldr.co/i/400x400?&bg=ece2d0&fc=000" />
</div>

How do I add a hover image transition without breaking my code?

I put some HTML/CSS together in order to customize a content block on Squarespace. 4 images floating inline. I want to hover on the image and for it to fade into another image, creating the illusion of zooming out. Everytime I try to implement the commands to do this, I break it. The new image isn't centered on the existing image and in some cases, finds a new position on the page. I want to keep the current structure intact.
Can someone please help me with adding the new code in order to achieve this?
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: Arial;
}
.header {
text-align: center;
padding: 32px;
}
.row {
display: -ms-flexbox;
/* IE10 */
display: flex;
-ms-flex-wrap: wrap;
/* IE10 */
flex-wrap: wrap;
padding: 0 4px;
}
.column {
-ms-flex: 25%;
flex: 25%;
max-width: 25%;
padding: 0 4px;
}
.column img {
margin-top: 8px;
vertical-align: middle;
width: 100%;
}
#media screen and (max-width: 800px) {
.column {
-ms-flex: 50%;
flex: 50%;
max-width: 50%;
}
}
#media screen and (max-width: 600px) {
.column {
-ms-flex: 100%;
flex: 100%;
max-width: 100%;
}
}
div.desc {
padding: 15px;
text-align: center;
}
<!-- Photo Grid -->
<div class="row">
<div class="column">
<img src="https://vivianeart.com/wp-content/uploads/2020/10/sd2_png.png" style="width:100%">
<div class="desc">Sarah Nordean</div>
</div>
<div class="column">
<img src="https://vivianeart.com/wp-content/uploads/2020/10/sct2_png.png" style="width:100%">
<div class="desc">Scott Everingham</div>
</div>
<div class="column">
<img src="https://vivianeart.com/wp-content/uploads/2020/10/sd3_png.png" style="width:100%">
<div class="desc">Serena Beaulieu</div>
</div>
<div class="column">
<img src="https://vivianeart.com/wp-content/uploads/2020/10/sd4_png.png" style="width:100%">
<div class="desc">Shawn Evans</div>
</div>
</div>
UPDATE: this is now an answer to how to make one image fade into another on the given code.
I found that using the given card-changing code caused flickering on hover so I have changed it slightly so that existing main code remains as is except each img is wrapped in a div. This has as background-image the image that will be faded into. Some CSS is added to make the transition to/from opacity on hover
Here is the complete code, as given in the question with the additions listed above:
<!DOCTYPE html>
<html>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: Arial;
}
.header {
text-align: center;
padding: 32px;
}
.row {
display: -ms-flexbox; /* IE10 */
display: flex;
-ms-flex-wrap: wrap; /* IE10 */
flex-wrap: wrap;
padding: 0 4px;
}
.column {
-ms-flex: 25%;
flex: 25%;
max-width: 25%;
padding: 0 4px;
}
.column img {
margin-top: 8px;
vertical-align: middle;
width: 100%;
}
#media screen and (max-width: 800px) {
.column {
-ms-flex: 50%;
flex: 50%;
max-width: 50%;
}
}
#media screen and (max-width: 600px) {
.column {
-ms-flex: 100%;
flex: 100%;
max-width: 100%;
}
}
div.desc {
padding: 15px;
text-align: center;
}
/* ADDED CSS */
div.background {
width:100%;
height:auto;
background-position: 0px 8px;
background-repeat:no-repeat no-repeat;
background-size:contain;
margin: 0;
padding: 0;
}
img { opacity : 1; transition:opacity 0.3s; }
img:hover { opacity : 0;}
</style>
<body>
<!-- Photo Grid -->
<div class="row">
<div class="column">
<div class="background" style="background-image:url(https://vivianeart.com/wp-content/uploads/2020/10/sct2_png.png);">
<img src="https://vivianeart.com/wp-content/uploads/2020/10/sd2_png.png" style="width:100%;">
</div>
<div class="desc">Sarah Nordean</div>
</div>
<div class="column">
<div class="background" style="background-image:url(https://vivianeart.com/wp-content/uploads/2020/10/sd3_png.png);">
<img src="https://vivianeart.com/wp-content/uploads/2020/10/sct2_png.png" style="width:100%">
</div>
<div class="desc">Scott Everingham</div>
</div>
<div class="column">
<div class="background" style="background-image:url(https://vivianeart.com/wp-content/uploads/2020/10/sd4_png.png);">
<img src="https://vivianeart.com/wp-content/uploads/2020/10/sd3_png.png" style="width:100%">
</div>
<div class="desc">Serena Beaulieu</div>
</div>
<div class="column">
<div class="background" style="background-image:url(https://vivianeart.com/wp-content/uploads/2020/10/sd2_png.png);">
<img src="https://vivianeart.com/wp-content/uploads/2020/10/sd4_png.png" style="width:100%">
</div>
<div class="desc">Shawn Evans</div>
</div>
</div>
</body>
</html>
This is the original text in this answer post asking for clarification.
This wasn't claiming to be an answer but was asking for clarification of some code which cannot be done in a comment.
Here is the code Al Taljana put in a comment - with background-colors put in by me in lieu of actual images. Could you verify that this is the effect you want - basically just changing one image for another? (Run the snippet and hover over the rectangle).
.card { width: 130px; height: 195px; background: url("images/card-back.jpg") no-repeat; display: inline-block; background-color:magenta; }
.card:hover { background: url("images/card-front.jpg") no-repeat; background-color: cyan; }
<div class="card">
</div>
Now, I am not too sure about what are the exact images that you will like to use, but here is a template of how the code for it to work.
img {
transition: all linear 0.7s;
}
.box {
position: relative;
}
img.hover-img {
position: absolute;
opacity: 0;
}
.box:hover img.hover-img {
opacity: 1;
}
<div class="box">
<img class="hover-img" src="https://www.redbridgenet.com/wp-content/uploads/html5.png" alt="Image name"
width="300" height="300">
<img src="https://wwwtalks.com/wp-content/uploads/2017/11/logo-2582748_640-300x300.png" alt="Image name" width="300" height="300">
</div>

Overflow images in y index only

I have a mini-gallery of images which I want to use to toggle content depending on the image clicked. When hovering on the image it scales up slightly. My problem is that when scaling I want to hide the overflow off the page but still show the overflow in the y-direction. I have tried overflow-x: hidden, overflow-y: visible but the y-overflow only appears at the bottom and scrollbars appear.
#images {
height: 70vh;
width: 100%;
display: flex;
flex-wrap: wrap;
overflow-x: hidden;
overflow-y: visible;
}
#images .image {
width: 50%;
height: 35vh;
transition: 0.5s;
cursor: pointer;
z-index: 1;
}
#images .image:hover {
transform: scale(1.1);
z-index: 10;
}
#images .image img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div id="images">
<div class="image">
<img src="image.jpg">
</div>
<div class="image">
<img src="image.jpg">
</div>
<div class="image">
<img src="image.jpg">
</div>
<div class="image">
<img src="image.jpg">
</div>
</div>
Is there a way to make these act as it would with no overflow stated without a scrollbar appearing in the x direction?
You need to add scaling property to image instead of outer div.
#images {
height: 70vh;
width: 100%;
display: flex;
flex-wrap: wrap;
overflow-x: hidden;
overflow-y: visible;
}
#images .image {
width: 49%;
height: 35vh;
transition: 0.5s;
cursor: pointer;
z-index: 1;
overflow: hidden;
position: relative;
border:1px solid #000;
}
#images .image:hover img {
transform: scale(1.1);
z-index: 10;
position: absolute;
left: 0;
transition: .2s;
}
#images .image img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div id="images">
<div class="image">
<img src="https://dummyimage.com/600x400/ffffff/000000">
</div>
<div class="image">
<img src="https://dummyimage.com/600x400/ffffff/000000">
</div>
<div class="image">
<img src="https://dummyimage.com/600x400/ffffff/000000">
</div>
<div class="image">
<img src="https://dummyimage.com/600x400/ffffff/000000">
</div>
</div>
Remove overflow css from the images and add it to the body:
body {
width: 100%;
overflow: hidden;
}

Centering flex children without extra space

MWE:
https://jsfiddle.net/zjgc9dfx/
Suppose I've got this layout:
<div class="slider">
<span>button here</span>
<div class="slide">
<img src="https://loremflickr.com/1000/1000" alt="Bacn">
</div>
<span>button here</span>
</div>
The slider is a flex container. The two buttons are inline-block, and I want the slide img to be limited to the viewport's height without extra space.
How can I achieve this?
* {
margin: 0;
padding: 0;
}
.slider {
display: flex;
justify-content: center;
align-items: center;
background-color: red;
}
.slider .slide img {
width: 100%;
max-height: 100%;
}
<div class="slider">
<span>asd</span>
<div class="slide">
<img src="https://loremflickr.com/1000/1000" alt="Bacn">
</div>
<span>asd</span>
</div>
Maybe something like this will work:
.slider {
display: flex;
align-items: center;
height: 100vh;
background-color: red;
}
.slider>span {
flex-shrink: 0; /* disables shrinking feature */
}
.slider>.slide {
flex: 1; /* consume all free space */
height: 100%;
}
.slider .slide img {
width: 100%;
max-height: 100%;
object-fit: cover;
vertical-align: bottom; /* https://stackoverflow.com/a/31445364/3597276 */
}
* {
margin: 0;
padding: 0;
}
<div class="slider">
<span>button here</span>
<div class="slide">
<img src="https://loremflickr.com/1000/1000" alt="Bacn">
</div>
<span>button here</span>
</div>
jsFiddle demo

How to make two images responsive with fixed height and plain CSS?

I want to make the two images responsive with a fixed height similar to here.
I created the jsfiddle
Somehow the line break is added too soon. I tried to add width=auto however this did not work. Moreover it would be good if after a certain width it would become a single column.
It's not super clear what you're asking but there are a few ways to make images responsive with a fixed height and fluid width.
The easiest of which is to use the object-fit: cover rule. Try adding object-fit: cover to your .image element.
https://www.w3schools.com/css/css3_object-fit.asp
This should force it to fill its container without warping its dimensions.
If you want to build a 2-column grid, you need to set the width of the containers using calc, and remove the margin-right on all the even containers.
.container {
width: calc(50% - 10px);
margin-right: 20px;
float: left;
}
.container:nth-child(2n) {
margin-right: 0;
}
.image {
object-fit: cover;
height: 412px;
}
I modified your js fiddle here: https://jsfiddle.net/hnxz7co9/34/
Actually When You A image tell display block and height some value then you most declared
Width: auto
when you tell an image width some value then tell height auto.
when you break this option your image see stretch, you want one side fee height or width.
body {
font-family: sans-serif;
margin: auto;
max-width: 1280px;
}
.site-content {
position: relative;
}
.max-column {
max-width: 1200px;
margin: 0 auto;
padding: 0 40px;
}
.container {
width: 42%;
height: 412px;
display: inline-block;
overflow: hidden;
margin-bottom: 20px;
margin-right: 20px;
line-height: 0px;
position: relative;
}
.image {
opacity: 1;
display: block;
height: 412px;
width: auto;
transition: .5s ease;
backface-visibility: hidden;
}
.middle {
transition: .5s ease;
opacity: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center;
}
.container:hover .image {
-webkit-filter: brightness(70%);
-moz-filter: brightness(70%);
-o-filter: brightness(70%);
-ms-filter: brightness(70%);
filter: brightness(70%);
opacity: 1.0;
-webkit-transform: scale(1.1);
transform: scale(1.1);
}
.container:hover .middle {
opacity: 1;
}
.text {
color: white;
font-size: 16px;
padding: 16px 32px;
}
<div class="site-content max-column">
<div class="container">
<img src="https://images.freeimages.com/images/large-previews/10f/autumn-1-1382513.jpg" alt="Avatar" class="image">
<div class="middle">
<div class="text">
<h1>Works</h1>
<p> Is the music</p>
</div>
</div>
</div>
<div class="container">
<img src="https://images.freeimages.com/images/large-previews/e01/lrt-interior-1626389.jpg" alt="Avatar" class="image">
<div class="middle">
<div class="text">
<h1>Works</h1>
<p> Is the music</p>
</div>
</div>
</div>
<div class="container">
<img src="https://images.freeimages.com/images/large-previews/035/young-golden-retriever-1404848.jpg" alt="Avatar" class="image">
<div class="middle">
<div class="text">
<h1>Works</h1>
<p> Is the music</p>
</div>
</div>
</div>
</div>
==thanks==
The reason that your images don't go two to a line is that you haven't allowed enough room for them to do that. You have set your image width at 48%. If you add up all the horizontal margins and horizontal padding outside of your images, you'll find that they add up to more than four percent of the available width, so your second image gets bounced to the next line. I found in your jsfiddle that changing the image width to 47%, or changing your right margin to 10px, or changing your right margin to 1% allowed two images per row.
If you express a width as a percentage and your padding and margins as pixels, you have to calculate the total pixels involved and make sure that there's enough room for two images to fit on a line. To do this, you can use the calc() function as in Daniel Bernardi's answer, or if you want the margins to resize along with your image size, you can set them as percentages rather than hard pixel values.
As for changing to one image per row on smaller screens, I would start by reading about #media. This allows you to set up different CSS rules for screens of different sizes. So, if your screen is below a certain size, set the width of the image to 100% minus padding and margins.
you have to use background image instead of image
<div class="site-content max-column">
<div class="container">
<div class="image img1"></div>
<div class="middle">
<div class="text">
<h1>Works</h1>
<p> Is the music</p>
</div>
</div>
</div>
<div class="container">
<div class="image img2"></div>
<div class="middle">
<div class="text">
<h1>Works</h1>
<p> Is the music</p>
</div>
</div>
</div>
<div class="container">
<div class="image img3"></div>
<div class="middle">
<div class="text">
<h1>Works</h1>
<p> Is the music</p>
</div>
</div>
</div>
</div>
body {
font-family: sans-serif;
margin: auto;
max-width: 1280px;
}
.site-content {
position: relative;
}
.max-column {
max-width: 1200px;
margin: 0 auto;
padding: 0 40px;
}
.container {
width: 48%;
height: 412px;
display: inline-block;
overflow: hidden;
margin-bottom: 20px;
margin-right: 20px;
line-height: 0px;
position: relative;
}
.image {
opacity: 1;
display: block;
height: 412px;
width: 100%;
transition: .5s ease;
backface-visibility: hidden;
}
.middle {
transition: .5s ease;
opacity: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center;
}
.container:hover .image {
-webkit-filter: brightness(70%);
-moz-filter: brightness(70%);
-o-filter: brightness(70%);
-ms-filter: brightness(70%);
filter: brightness(70%);
opacity: 1.0;
-webkit-transform: scale(1.1);
transform: scale(1.1);
}
.container:hover .middle {
opacity: 1;
}
.text {
color: white;
font-size: 16px;
padding: 16px 32px;
}
.img1{
background: url('https://images.freeimages.com/images/large-previews/10f/autumn-1-1382513.jpg');
background-size: cover;
background-position: center;
}
.img2{
background: url('https://images.freeimages.com/images/large-previews/e01/lrt-interior-1626389.jpg');
}
.img3{
background: url('https://images.freeimages.com/images/large-previews/035/young-golden-retriever-1404848.jpg');
background-size: cover;
background-position: center;
}
demo
Play around with the values and you will see how effects elements.
h1 {
padding: 80px;
text-align: center;
}
.image {
width: 450px;
height: 250px;
border: 1px black solid;
}
.grid_img {
display: grid;
grid-template-columns: repeat(2, minmax(500px, 1fr));
/* you need SCSS for this:
#media (max-width: 990px) {
.grid_cards {
grid-template-columns: repeat(1, minmax(200px, 1fr));
}
*/
<div class="grid_img">
<div class="image">
<h1>Some Image here</h1>
</div>
<div class="image">
<h1>Some Image here</h1>
</div>
<div class="image">
<h1>Some Image here</h1>
</div>
<div class="image">
<h1>Some Image here</h1>
</div>
<div class="image">
<h1>Some Image here</h1>
</div>
<div class="image">
<h1>Some Image here</h1>
</div>
<div class="image">
<h1>Some Image here</h1>
</div>
<div class="image">
<h1>Some Image here</h1>
</div>
</div>