Look I’ll give you two options.
The result will be this, although they are two different techniques the visual result is the same:
Option 1
Like you said you couldn’t do with a div
inside the other I made this model so you could study it and see that yes it is perfectly possible to make the effect using a div
inside the other.
You just need the div
father has position:relative
and the div
daughter be position:absolute
, in this way the reference of the child is within the scope of the father. In your case how you want to make the effect on a link <a>
I put a div
within the <a>
and I named the postions
.
Follow the example of the code.
a {
display: flex;
position: relative;
width: 300px;
height: 100px;
justify-content: center;
align-items: center;
background-image: url(https://picsum.photos/300/100);
background-size: cover;
color: #f00;
font-size: 2rem;
text-decoration: none;
text-shadow: 0 0 2px black;
}
a div {
transition: background-color 300ms;
}
a:hover div {
position: absolute;
width: 100%;
height: 100%;
background-color: rgba(43, 80, 142, 0.9);
}
a span {
position: relative;
z-index: 2;
}
<a href="#">
<div></div>
<span>Lorem, ipsum dolor.</span>
</a>
Option 2
That option is even simpler than the first, I would particularly opt for it.
Here what I did was use a box-shadow
that instead of growing out it grows inside the element, to make the box-shadow
grow inside you need to use a inset
and a great value of spread-radius
you can read more about this property here. https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow
Follows the code
a {
display: flex;
position: relative;
width: 300px;
height: 100px;
justify-content: center;
align-items: center;
background-image: url(https://picsum.photos/300/100);
background-size: cover;
color: #f00;
font-size: 2rem;
text-decoration: none;
text-shadow: 0 0 2px black;
transition: box-shadow 300ms;
}
a:hover {
box-shadow: inset 0 0 0 10000px rgba(43, 80, 142, 0.9);
}
<a href="#">
Lorem, ipsum dolor.
</a>
It worked, but the text I put is behind the background-color, and the transition doesn’t work
– Miriam
@Miriam this text issue is simple to solve, you can create a new tag for the text and manipulate the z-index of this content. Take a look at this idea https://jsfiddle.net/gferreiraa/u2ovbf9e/1/
– Getulio Rafael Ferreira