How does one repeat this 1px x 1px CSS background image on top of the HTML <img>
?
http://jsfiddle.net/hjv74tdw/1/
I came across CSS show div background image on top of other contained elements, however it seems to require an extra div. Ideally I wish I didn't have to use a div at all, but according to CSS Background-image over HTML img that's not really possible, at least not for a 100% width responsive scenario.
.test {
background: url(http://placehold.it/1/1) repeat;
position: relative;
z-index: 100;
width: 200px;
}
.test img {
width: 100%;
display: block;
position: absolute;
z-index: 50;
}
<div class="test">
<img src="http://lorempixel.com/400/400">
</div>
As the simplest solution. That is: Create a relative div that is placed in the flow of the page; place the base image first as relative so that the div knows how big it should be; place the overlays as absolutes relative to the upper left of the first image.
We can use the property to provide an overlay to the background image. We can use the background-blend-mode property after setting the background image. For example, create a div in HTML. In CSS, set the background image using the url() function and set the no-repeat and fixed values in the background property.
Basically, you put both of your images in the same container. Give the container a position that isn't static (in my example, relative). Then give the overlay image position: absolute and position it however you want using bottom and right . Show activity on this post.
We're applying a negative right margin on a floated left element that allows content to overlap. -100% is equal to the width of the container so it shifts the image to the left and allows the bottom image to render beneath it as if it's not in the DOM. Codepen here: HTML.
You could use a pseudo-element. In this example, the :after
pseudo element is absolutely positioned relative to the parent element. It takes the dimensions of the entire parent element, and the size of the parent element is determined by the size of the child img
element.
.test {
display: inline-block;
position: relative;
}
.test:after {
content: '';
position: absolute;
top: 0; right: 0;
bottom: 0; left: 0;
background: url(http://placehold.it/20x20/1) repeat;
}
<div class="test">
<img src="http://lorempixel.com/200/200">
</div>
As a side note, your example wasn't working for a couple of reasons. The parent element has a background image, but since the child element establishs a stacking context within the parent element, it's not possible for the parent's background image to appear above the child img
element (unless you were to completely hide the img
element). This is why the z-index
s weren't working as expected.
In addition, the img
element was absolutely positioned. In doing so, it is removed from the normal flow, and since it was the only child element, the parent element therefore collapses upon itself and it doesn't have any dimensions. Thus, the background image doesn't show up. To work around this, you would either have to set explicit dimensions on the parent element (height
/width
), or you could remove the absolute positioning on the child img
element.
Another way, is to set content
to empty and set a background, for example:
img {
background: url(...) center center no-repeat;
background-size: cover;
content: '';
display: block;
width: 800px;
height: 100px;
}
Here is a demo: http://jsfiddle.net/infous/effmea8x/
In this demo the first image is standard but with unproportional width and hight, the second one is with the background. The first one is scaled badly, the second one is OK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With