I have a JavaScript slideshow that displays images of various dimensions in a browser window. It uses object-fit: scale-down
to resize the image to fit the container. I’d like to add a border to the image, but when I apply the style border: 2px solid black
to the <img>
element, it seems to surround what would be the image in its original, unscaled size.
Is there a way, either with CSS or with JavaScript, to apply the border to the resized image?
const duration = 2000; // time (msec) to display each slide
const sizes = [
[4000, 500],
[1000, 4000],
[600, 400],
[100, 200]
];
const sleep = ms => new Promise(r => setTimeout(r, ms));
let size_index = 0;
function show_slides(duration) {
let this_duration = duration;
const my_parent = document.querySelector('#slide-div');
if (size_index == sizes.length) {
size_index = 0;
}
let w = sizes[size_index][0];
let h = sizes[size_index][1];
++size_index;
let my_randomizer = `https://placehold.co/${w}x${h}?text=${w}+x+${h}npx`;
fetch(my_randomizer)
.then(my_response => my_response.blob())
.then(my_blob => {
let my_url = URL.createObjectURL(my_blob);
sleep(this_duration)
.then(() => {
URL.revokeObjectURL(my_parent.querySelector('img').src);
my_parent.querySelector('img').src = my_url;
show_slides(duration);
});
})
.catch(my_error => console.error('Error: ', my_error));
}
html {
height: 100%;
width: 100%;
}
body {
/* prevent body from displacing */
margin: 0;
/* body should perfectly superimpose the html */
height: 100%;
width: 100%;
}
.outer-div {
display: flex;
flex-flow: column;
height: 100%;
/* Now create left/right margins */
margin: 0 0.5em;
}
.inner-fixed-div {
margin-top: 0.5em;
}
.inner-remaining-div {
margin-bottom: 1em;
flex-grow: 1;
/* hints the contents to not overflow */
overflow: hidden;
}
.picture-div {
/* force the div to fill the available space */
width: 100%;
height: 100%;
}
.picture-div-img {
/* force the image to stay true to its proportions */
width: 100%;
height: 100%;
/* and force it to behave like needed */
object-fit: scale-down;
object-position: center;
/* does not properly enclose image: */
border: 2px solid black;
}
<!DOCTYPE html>
<html lang="en">
<!-- Self-contained slideshow demo -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body onload="show_slides(duration);">
<div class="outer-div">
<div class="inner-fixed-div">
<h1>Lorem Epsom</h1>
</div>
<div class="inner-remaining-div">
<!-- This div will hold the <img> -->
<div id="slide-div" class="picture-div">
<!-- Placeholder <img> element for slides -->
<img class="picture-div-img">
</div>
</div>
<!-- end of inner-remaining-div -->
</div>
<!-- end of outer-div -->
</body>
</html>