fishera
fishera

Reputation: 799

Opacity for div but not for text inside - CSS

Here is my code:

.test,
.test-link {
  position: relative;
}
.test-link > a,
.test-link img {
  display: block;
  width: 100%;
}
.overlay,
.overlay:before {
  opacity: 0;
  position: absolute;
  z-index: 2;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  text-align: center;
}
.overlay:before {
  content: '';
  z-index: auto;
  background-color: #FFF;
  opacity: 0.75;
}
.test:hover .overlay {
  opacity: 1;
}
<div class="test">
  <div class="test-link">
    <a href="#">test</a>
    <div class="overlay">
      <div class="sub-title">
        <span>
                                  test test test ets
                                    </span>
      </div>

    </div>
  </div>
</div>

I have created a link which, when hovered, displays text and fades itself to a lower opacity. My problem is that the text, which is contained within the element I want to fade, also fades and I would like to keep it at 100% opacity. How to achieve that? Any suggestions?

Upvotes: 0

Views: 387

Answers (1)

slashsharp
slashsharp

Reputation: 2833

Create a pseudo element, opaque that pseudo element instead of the parent.

DEMO

.parent:before {
    content: '';
    background-color: red;
    opacity: 0.5;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    display: none;
}
.parent {
    position: relative;
    z-index: 1;
}
.parent:hover:before {
    display: block;
} 
.text span {
  display: none;
  margin-left: 10px;
}
.parent:hover .text span {
  display: inline;
}
<div class="parent">
    <div class="text">
        <p>This is a text <span>Hover text</span></p>
    </div>
</div>

Upvotes: 1

Related Questions