HappyHands31
HappyHands31

Reputation: 4101

Getting Overlay + Darker Overlay On Hover For List Item Images With CSS

Trying to recreate an overlay / overlay + hover effect.

In the code that works for the effect that I used earlier, the parent div gets the class thumbnail-row with the styles:

.thumbnail-row {
 display: flex;
 margin-top: 40px;
 box-shadow: 0px 0px 0px 4000px rgba(27, 61, 88, 0.5) inset;
}

.thumbnail-row div::after {
 position: relative;
 top: 0; left: 0; 
 width: 100%;
 height: 100%;   
 box-shadow: 0px 0px 0px 4000px rgba(27, 61, 88, 0.5) inset;
}

Applying that same technique to this code:

HTML:

<div class="container og-grid" id="og-grid">

<div class="headline">Portfolio</div>
<div class="tagline">Responsive websites for a wide variety of clients</div>

<li class="width-1-3 list-overlay">
<a href="http://thehamburgercollection.com/" data-largesrc="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/hamburger-thumbnail.jpg" data-title="The Hamburger Collecion" data-description="The Hamburger Collection is a custom WordPress theme made for displaying a unique collection of artwork. You're able to choose from a variety of buying options - posters or banners in different sizes - through the WooCommerce plugin.">
<img src="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/hamburger-thumbnail.jpg"/>
</a>
</li>

<li class="width-1-3 list-overlay">
<a href="http://yoyomoi.com/" data-largesrc="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/yoyomoi-thumbnail.jpg" data-title="YoYoMoi" data-description="YoYoMoi is a custom WordPress theme created to act as a portfolio for advertising content from the 90's. The idea was to showcase the content in a modern, responsive setting while maintaining some of the vintage flavor from the original site.">
<img src="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/yoyomoi-thumbnail.jpg"/>
</a>
</li>

<li class="width-1-3 list-overlay">
<a href="http://dogsondutymo.org/" data-largesrc="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/dogs-thumbnail.jpg" data-title="Dogs On Duty" data-description="Dogs on Duty was made with the Avada WordPress theme. By manipulating this theme, we were able to create a fully responsive website that includes a blog, store, and informational pages for promoting service-dog equipment.">
<img src="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/dogs-thumbnail.jpg"/>
</a>
</li>

<li class="width-1-3 list-overlay">
<a href="http://gatewaywebdesign.com/" data-largesrc="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/gateway-thumbnail.jpg" data-title="Gateway Web Design (Old)" data-description="The first website for my freelance business - Gateway Web Design was a custom WordPress theme, fully responsive with a built-in blog and store. Though big and bulky, this website's functionality remains extremely powerful."><img src="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/gateway-thumbnail.jpg"/>
</a>
</li>

<li class="width-1-3 list-overlay">
<a href="http://chameleonwebsolutions.com/" data-largesrc="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/chameleon-thumbnail.jpg" data-title="Chameleon Web Solutions" data-description="Chameleon Web Solutions was the original website for my freelance business. I used custom JavaScript on the homepage to loop through images of a Chameleon, in order to give the effect that it was changing colors. The idea was that, as a designer and developer, I can adapt to a client's environment, which still is true today."><img src="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/chameleon-thumbnail.jpg"/>
</a>
</li>

<li class="width-1-3 list-overlay">
<a href="http://adriennecoleman.drawyourpets.com/" data-largesrc="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/adrienne-thumbnail.jpg" data-title="Adrienne Levin Coleman" data-description="Komatsuna prairie turnip wattle seed artichoke mustard horseradish taro rutabaga ricebean carrot black-eyed pea turnip greens beetroot yarrow watercress kombu.">
<img src="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/adrienne-thumbnail.jpg"/>
</a>
</li>

<li class="width-1-3 list-overlay">
<a href="http://castaway.drawyourpets.com/" data-largesrc="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/castaway-thumbnail.jpg" data-title="Castaway Vacations" data-description="Komatsuna prairie turnip wattle seed artichoke mustard horseradish taro rutabaga ricebean carrot black-eyed pea turnip greens beetroot yarrow watercress kombu.">
<img src="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/castaway-thumbnail.jpg"/>
</a>
</li>

<li class="width-1-3 list-overlay" id="last-image">
<a href="http://drawyourpets.com/" data-largesrc="http://new.gatewaywebdesign.com/wp-content/uploads/2017/06/drawyourpets_thumbnail.jpg" data-title="Draw Your Pets" data-description="Draw Your Pets LLC is a small business dedicated to turning photographs of pets into drawings. These high quality pencil drawings capture every detail of your beloved dog, cat, or horse, and present you with various framing and product options.">
<img src="http://new.gatewaywebdesign.com/wp-content/uploads/2017/07/drawyourpets_thumbnail.jpg"/>
</a>
</li>
</div>

CSS:

.list-overlay {
box-shadow: 0px 0px 0px 4000px rgba(27, 61, 88, 0.5) inset;
z-index:2;
}

.list-overlay::after {
width: 100%;
height: 100%;   
box-shadow: 0px 0px 0px 4000px rgba(27, 61, 88, 0.5) inset;
}

.list-overlay:hover {
visibility: visible;
opacity: 1;
}

gives me the current result (not working).

When I tried to wrap the <li>'s in divs, it caused each image to be on its own row (which I of course don't want).

Also, tried adding z-index: 2; to .list-overlay::after, but the box shadow is still beneath the image. Since I'm using bootstrap for this site, I also tried wrapping all <li>'s in a container, then wrapping three <li>'s in a row at a time and assigning them bootstrap column-widths (col-md-4 col-sm-12), but that didn't work either.

The first step is just to get the box shadow over the images at 50% opacity, then to make that shadow 80% when you hover over it. Since wrapping the <li>'s in divs is throwing off the 3-column layout, is there a way to get a box shadow over an <li> with the way I have it set up?

Thanks!

Upvotes: 1

Views: 685

Answers (1)

I haz kode
I haz kode

Reputation: 1635

I am able to create this effect using a combination of the pseudo-element :after and the :hover state.

The effect is as follow:

The pseudo-element :after is hidden by default using opacity: 0 but positioned "on top" of the image. It has a semi-transparent background with the color you specified in the example rgba(27, 61, 88, .9);

When the user triggers the hover state you use :hover you bring the pseudo-element :after into display using opacity: 1

Adding transition: all ease 1s; to the pseudo-element :after makes the transition from opacity: 0 to opacity: 1 happen over the duration of 1 second instead of being instantaneous.

/* Start demonstration backgrounds and alignment fluff */
body {
  background: #131418;
  text-align: center;
  margin: 1em auto;
}

.my-image-parent {
  display: inline-block;
  width: 300px;
  height: 300px;
  line-height: 300px;
  text-align: center;
  font-size: 0;
}

.bg1 {background: url(https://unsplash.it/800/799);}
.bg2 {background: url(https://unsplash.it/799/800)}
.bg3 {background: url(https://unsplash.it/800/799);}
.bg4 {background: url(https://unsplash.it/801/801);}
.bg5 {background: url(https://unsplash.it/802/800);}
.bg6 {background: url(https://unsplash.it/800/802);}

.my-image {
  width: auto;
  height: 100%;
  vertical-align: middle;
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
}
/* End demonstration backgrounds and alignment fluff */


/* Start Essential CSS for the effect */
.my-image {
  position: relative;
}

.my-image:after {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background: rgba(27, 61, 88, .9);
  opacity: 0;
  transition: all ease 1s;
}

.my-image:hover:after {
  opacity: 1;
  cursor: pointer;
}
/* End Essential CSS for the effect */
<div class="my-image-parent">
  <div class="my-image bg1"></div>
</div>

<div class="my-image-parent">
  <div class="my-image bg2"></div>
</div>

<div class="my-image-parent">
  <div class="my-image bg3"></div>
</div>

<div class="my-image-parent">
  <div class="my-image bg4"></div>
</div>

<div class="my-image-parent">
  <div class="my-image bg5"></div>
</div>

<div class="my-image-parent">
  <div class="my-image bg6"></div>
</div>

Upvotes: 3

Related Questions