I am generating a static site where the images and set within a responsive "picture frame" so the Mat is the same size on each side, regardless of the image’s aspect ratio. This is working well; however…
I want to ensure that the whole "picture frame" is visible in the browser window. Currently, the frame is sized to the width, meaning tall images scroll off the bottom.
Can someone look at my code and suggest how I dynamically resize the "picture frame" so it is 100% visible in the browser window?
There is a JSFiddle here – link
.frame {
position: relative;
width: 100%;
background: black;
box-shadow: 0 10px 7px -5px rgba(0, 0, 0, 0.3);
}
.mat {
position: absolute;
background: white;
box-shadow: 0px 0px 20px 0px rgba(0,0,0,0.5) inset;
}
.art {
position: absolute;
}
.art img {
width: 100%;
}
.art:after {
content: '';
display: block;
position: absolute;
top: 0;
width: 100%;
height: 100%;
box-shadow: 0px 0px 20px 0px rgba(0,0,0,0.5) inset;
}
<div class="frame" style="padding-bottom: 115.09%">
<div class="mat" style="top: 2.1721%; bottom: 2.1721%; left: 2.50%; right: 2.50%;">
<div class="art" style="top: 11.354%; bottom: 11.354%; left: 13.158%; right: 13.158%;">
<img src="https://cw109eu.uk/images/portfolios/recent/231106-44511-1.jpg" />
</div>
</div>
</div>
Thanks
2
Answers
We can add
width: 100%
&height: 100vh
toparent div
and addwidth: 100%
,height: 100%
&object-fit: cover
orobject-fit: contain
as per need.This is illustration. Check the link which I have provided and it will help you to understand better & if I have missed something or misunderstood then do share. Thank You.
Make the image the same size as its container (using grid layout is probably better than absolute positioning). You also don’t need that much nesting, just a border and a padding on the image.
Here’s a working snippet producing the result you want.
EDIT: Just in case I misunderstood and what you actually want is this: