CSS - Using a pseudo-element for an image mask - css

I'm showing an image on a page and I wish to add a mask to achieve a specific border-and-corner effect.
To do this I was hoping to use a pseudo-element in the following manner:
img
{
height: 58px;
left: 0;
position: absolute;
top: 0;
width: 58px;
z-index: 1;
&:after
{
background-image: url(images/frame.gif);
content: " ";
height: 58px;
left: 0;
position: absolute;
top: 0;
width: 58px;
z-index: 2;
}
}
But the mask image never shows up. I've also tried adding a negative left- and top-margin to 'pull' the pseudo-element back over the <img> but still nothing.
Is there a glaring error in my CSS or does the problem lie with an inherent limit to pseudo-elements?

By default the img tag is an inline element and it is not a container to add an after pseudo class. I'll suggest the following code:
div.container {
img {
display: block;
height: 58px;
left: 0;
position: absolute;
top: 0;
width: 58px;
z-index: 1;
}
&:after {
display: block;
background-image: url(images/frame.gif);
content: " ";
height: 58px;
left: 0;
position: absolute;
top: 0;
width: 58px;
z-index: 2;
}
}
Notice that the pseudo class is also a block element.

Related

Background in ::before hides the content

Consider:
<div class="auth-form-frame">
Some very long text
</div>
If I give a background color to .auth-form-frame::before, the text becomes invisible:
.auth-form-frame {
position: relative;
width: 50rem;
height: 25rem;
color: #000;
font-size: 10rem;
&:before {
content: "";
background-size: cover;
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
background-color: green; // This creates the problem
}
> * {
z-index: 100;
}
}
The full example appears in jsbin.
Why does this happen? How can I make the content visible despite the background color of the pseudo-element of <div>?
You need to set the z-index of the before pseudo-element:
&:before {
content: "";
background-size: cover;
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
background-color: green; // This creates the problem
z-index: -1; /* ADD THIS */
}
> * {
z-index: 100;
}
}
While the pseudo element is sort of content of its 'owning' element you can position the owner and before and after pseudo elements relative to each other z-indexwise.

Bootstrap trying to put an image on the container :after & :before

I'm trying to put an image on the right and left edges of the container's with the :before and :after.
But for some reason the containers totally .... up when I add a position absolute to the before and afters...
It has probably something to do with the standard bootstrap before and afters on the containers
Any idea how I can go around this?
.header,
.spotlight,
.main,
.footer {
>.container {
position: relative;
&:before {
content: " ";
position: absolute;
background: url("../img/right.png");
height: 100%;
display: block;
width: 6px;
right: -6px;
z-index: 999999;
top: 0;
}
&:after {
content: " ";
position: absolute;
background: url("../img/left.png");
height: 100%;
display: block;
width: 6px;
left: -6px;
z-index: 999999;
top: 0;
}
}
}
Example how the shadow(image) should look

Psuedo element to display behind parent divs text but in front of parent div

I have a psuedo element which is a pink background inside a div like so:
<div>
Hello
</div>
div {
background-color: green;
display: inline-block;
padding: 20px;
position: relative;
}
div::after {
content: "";
background-color: pink;
display: block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
Also here is a codepen to show what I mean: http://codepen.io/acha5066/pen/oXVPzg I want the pink background in front of the green background but behind the text Hello. How can I achieve this?
div {
background-color: green;
display: inline-block;
padding: 20px;
position: relative;
z-index: 0 /* we etablish a new stacking context */
}
div::after {
content: "";
background-color: pink;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: -1
}
<div>
Hello
</div>
A good article that will help you to understand the stacking context: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context

Z Index a pseudoelement behind it's parent

I'm trying to z index an element behind it's parent but it isn't working.
Here's my pen:
http://codepen.io/Tiger0915/pen/OPXway
and my SCSS:
div {
width: 400px;
height: 250px;
position: relative;
background: grey;
margin: 100px auto;
z-index: 5;
&:after {
content: ":after";
position: absolute;
width: 100%;
height: 100%;
top: -20px;
right: -70px;
background: lightgrey;
z-index: 4;
}
}
how do I get my :after to appear behind my parent div?
I think I figured it out. Like ajp15243 said, I can't position a child element behind a parent element.
So I ended up creating 2 different pseudoelements, a :before and an :after, both of which appear behind the other children of my div (using negative z indexes), and I can put the after at a lower z index than the before to get the effect I wanted.
div {
width: 400px;
height: 250px;
position: relative;
margin: 100px auto;
z-index: 5;
&:before {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 0;
right: 0;
background: grey;
z-index: -1;
}
&:after {
content: ":after";
position: absolute;
width: 100%;
height: 100%;
top: -20px;
right: -70px;
background: lightgrey;
z-index: -2;
}
}
Here's the pen:
http://codepen.io/Tiger0915/pen/XJKBoq

keeping image contained within another image.

How can I keep a close button contained inside an image even is I change the img size using jquery
#full_image {
width: 200px;
height: 200px;
left: 0px;
top:10px;
position: relative;
opacity: 1;
z-index: 9;
}
#full_image img {
left: 20px;
width: 339px;
height: 211px;
position: relative;
overflow: hidden;
}
#full_image .close{
background: url("http://www.sobral.ce.gov.br/saudedafamilia/close.jpg") no-repeat;
top: 5px;
right: 0px;
cursor: pointer;
height: 29px;
opacity: 1;
position: absolute;
width: 29px;
z-index: 999;
}
<div id="full_image"><img src="http://coe.berkeley.edu/forefront/fall2005/images/woz1.jpg" />
<span> </span>
</div>
JSFIDDLE
Use the following CSS:
#full_image {
position: relative;
}
#full_image span {
position: absolute;
top: 0;
right: 0;
}
This tells the span to be a positioned relatively to #full_image, and stick to top right of it.
Hope this helps :)
You don't specify what "close button contained inside an image" means. I would have tried to put my image and button like this:
HTML:
<div>
<img id="myImage"></img>
<button id="myButton" />
</div>
CSS:
#myImage {
position: relative;
}
#myButton {
position: absolute;
top: ???;
left: ???;
}
jQuery:
You have to calculate where to place your button (what you should give the CSS-attributes for f ex top: and left: )

Resources