I have a div the size of which may differ from time to time.
I set a background image with fixed size to that div. But I need to fit the background image to div at any size.
How could be possible through CSS.
Thanks in advance!
The only way to change a size of an image with CSS is when it's an <img>
element.
I mean that you can do something like that:
img#myBG { width:200px; height:100px; }
If you need it to be a background, you should use 'z-index' and put your img
under a the element that holds the content.
Something like this:
#holder {
position: relative;
width: 200px;
height: 100px;
border: 1px solid #F00;
}
#holder div {
position: absolute;
top: 0;
left: 0;
z-index: 2;
}
img#myBG {
width: 200px;
height: 100px;
position: absolute;
top: 0;
left: 0;
z-index: 1;
object-fit: cover;
}
<div id="holder">
<img id="myBG" src="https://placehold.co/600x400" />
<div>my content here</div>
</div>