Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Michael Lawinger
Michael Lawinger
33,581 Points

Blur Only Background

I am trying to create a background image opacity/blur hover effect. I have the image as a background in the CSS, but it also blurs and fades the text. How do I preserve the text while only fading the background-image.

This Is My HTML & CSS:

<section id="shows">
   <h2 class="show-dates"> <a href="tour.html"> Show Dates </a> </h2>
</section>
.show-dates
{background-image: url("guitar1.jpg");
background-position: 50% 5%;
background-size: cover;
padding: 5em;
transition: all .6s;}

.show-dates:hover
{opacity: .6;
filter: blur(.5px);}

4 Answers

Rich Zimmerman
Rich Zimmerman
24,063 Points

You're selecting the "show-dates" class, which is the class of your h2 element, so it will affect all of the contents of your h2 element.

Unfortunately there isn't a "background opacity" option in CSS. The workaround I've used for this was creating a separate div element and essentially layering it over the existing element where you want it to blur. You can do this with the z-index option in css, basically putting the image you want to blur behind your h2 element - similar to setting it as a background image, but allowing you to target them separately.

so your html would look something similar to this

<div id="section">
  <div id="background">
    <img src="guitar1.png">
  </div>
  <section id="shows">
     <h2 class="show-dates"> <a href="tour.html"> Show Dates </a> </h2>
  </section>
</div>

And your css would include

#section {
  position: relative;
}

#background {
    position: absolute;
    left: 0;
    top: 0;
    z-index: -1;
    transition: all .6s;
}

#background:hover {
  opacity: .6;
  filter: blur(.5px);
}
Michael Lawinger
Michael Lawinger
33,581 Points

It technically works but it creates other problems such as displaying the full image and all other sections intrude into the section get blurred.

Rich Zimmerman
Rich Zimmerman
24,063 Points

You'll probably have to experiment with and set width/height/margins and stuff, but this should be a good starting point at least.

Hi Michael,

You could use the ::before pseudo element and apply your background image to that. The pseudo element should be sized the same as the h2 and absolutely positioned. You can then set a z-index of -1 so it appears below the h2. This way you give off the appearance that you're affecting only the background of the h2.

The h2 should have a position of relative.

This is untested but something like the following:

.show-dates {
  position: relative;
  padding: 5em;
}

.show-dates::before {
  position: absolute;
  z-index: -1;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  content: "";
  background-image: url("guitar1.jpg");
  background-position: 50% 5%;
  background-size: cover;
  transition: all .6s;
}

.show-dates:hover::before {
  opacity: .6;
  filter: blur(.5px);
}

You may have to make some adjustments with padding and/or positioning but hopefully this gets you close.

Use a single colon for before if you have to support IE8

Michael Lawinger
Michael Lawinger
33,581 Points

Thanks guys, with the advice and the help of my web mentor I was able to get it working!