A pseudo element needs content to display, even if it’s just an empty string. And without any “real” content it will need volume. To get volume (fill the container) use absolute positioning with 0 on all sides.
Yes, you can do it with a linear-gradient while using multiple background images.
A linear-gradient is considered an image so it can be used with either the background-image property or the shorthand background property.
You don’t need a pseudo element to hook the opacity to. When using a rgba color on your gradient the alpha parameter defines the opacity.
This can work, but strictly it’s not a semi-opaque background, but the illusion of one. If the underlying container has the same solid background colour as the gradient it works fine.
But if the underlying container has its own background image or pattern the illusion may fail, unless you can exactly match that background size and position in the div background.
For example, here the white gradient gived the image a tint, but there is no sign of the black/blue gradient underneath.
With the pseudo element you have true transparency with the underlying background showing through the div background.
So you can get away with the gradient method in some cases, but not all.
Also, just as a minor note: Generally speaking you’re not ‘adding’ opacity to an element - elements by default are fully opaque. What you’re actually doing is reducing opacity to increase its transparency.
Yes, I see what your saying. More than one BG image can cause ill effects.
It’s the same old problem from years ago. There are countless threads here at SP about “Setting Opacity without effecting child elements”. It’s always going to require an extra element for true transparency.
When using an rgba or hsla there is no color code for transparent. With rgba it must be a value between 0-255 which will always leave a hint of color when the opacity is set.
It seems to me that this could be easily resolved by the CSS guys writing the specs and the browser vendors. It may not be as easy to fix as it sounds though, it may have something to do with the “Z” layer in one way or another. The root of the problem is that opacity inherits to child elements but nobody wants it to inherit, and rightly so. Opaque text is unreadable.
It should be up to us to reset the child elements to inherit if we want them too. As it is with box-sizing.