CSS background image on top of
How does one repeat this 1px x 1px CSS background image on top of the HTML
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.
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
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-indexs 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 (
width), or you could remove the absolute positioning on the child