CSS properties allow authors to specify the foreground color and background of an element. Backgrounds may be colors or images. Background properties allow authors to position a background image, repeat it, and declare whether it should be fixed with respect to the viewport or scrolled along with the document.
The 'color' property 'color' properties allow to set colors for the text content of an element. The following table shows the necessary detail of the 'color ' property. This stylesheet states that all the elements under body shall be of navy color, but if it am list item (li), then it shall be maroon.
Say you want to put an image or two on a webpage. One way is to use the background-image CSS property. This property applies one or more background images to an element, like a <div> , as the documentation explains.
Foreground images Foreground images (also called inline images) uses the HTML <img> tag to embedded images in a page. The tag itself is a single self-closing tag and uses the following HTML syntax: <img src="girl_with_laptop.jpg" width="150" height="246" alt="girl with a laptop" />
To achieve a "foreground image" (without extra HTML code), you can use a pseudo-element (::before
/ :before
) plus the CSS pointer-events
. The last property is needed so that the user can actually click through the layer "as if it did not exist".
Here's an example (using a colour whose alpha channel is 50% so that you can see that the real elements can actually be focused). http://jsfiddle.net/JxNdT/
#cont {
width: 200px;
height: 200px;
border: 1px solid #aaa; /*To show the boundaries of the element*/
}
#cont:before {
position: absolute;
content: '';
background: rgba(0,0,0, 0.5); /*partially transparent image*/
width: 200px;
height: 200px;
pointer-events: none;
}
<div id="cont">
Test<br>
<input type="text" placeholder="edit">
</div>
PS. I picked the ::before
pseudo-element, because that naturally leads to the correct positioning. If I pick ::after
, then I have to add position:relative;
to the real element (#cont
), and top:0;left:0;
to the pseudo-element (::after
).
PPS. To get the foreground effect on elements without a fixed size, an additional element is needed. This wrapper element requires the position:relative;display:inline-block;
styles. Set the width
and height
of the pseudo-element to 100%
, and the pseudo-element will stretch to the width and height of the wrapper element. Demo: http://jsfiddle.net/JxNdT/1/.
You can use this css
#yourImage
{
z-index: 1;
}
NOTE
Set the z-index
to index greater the the z-index
of the element over which you are putting the image.
If you have not specified any z-index
then 1
would do the work.
You can also set z-index
to -1
,in that case the image would always be at background
!
If you need a white-transparent foreground
This is for future visitors like me who are considering adding a white-transparent foreground to an element to communicate that it's hidden / disabled for instance. You can often achieve your goal by simply lowering the opacity
below 1:
.is-hidden {
opacity: 0.5;
}
visible
<span class="is-hidden">hidden</span>
visible
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