Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Background Overlay

Featured Replies

For some odd reason no matter what I try I can not get my background to overlay.

 

opacity just seems to fade the buttons not the actual background..

 

The idea is, On hover the background goes darker (you can just see it, so opacity 0.75 would be fine. I just cant get the thing to do it.

 

The code

$template = '
	<div class="swrapper">
<span class="box2" style="background-repeat: no-repeat; background-size:auto; background-position:center  center;  background-image: url(' . $rows[$x][photograph_url] . '); min-height:350px;">

</span>

<span class="box1" style="background-repeat: no-repeat; background-size:auto; background-position:center  center;  background-image: url(' . $rows[$x][photograph_url] . '); min-height:350px;">
	<div class="in-span">
	<div class="in-span-1"> <a href="/mediaone/interiography/view.php?view=' . $rows[$x]['id'] . '" class="button"> Love it!  </a></div>
<div class="in-span-left"><a href="/mediaone/interiography/view.php?view=' . $rows[$x]['id'] . '" class="button"> Share it Facebook!  </a></div>
	<div class="in-span-right"><a href="/mediaone/interiography/view.php?view=' . $rows[$x]['id'] . '" class="button"> Share it Twitter!  </a></div>
<div class="in-span-left"><a href="/mediaone/interiography/view.php?view=' . $rows[$x]['id'] . '" class="button"> Share it Google!  </a></div>
	<div class="in-span-right"><a href="/mediaone/interiography/view.php?view=' . $rows[$x]['id'] . '" class="button"> Share it Twitter!  </a></div>
			<div class="in-span-1"> <a href="/mediaone/interiography/view.php?view=' . $rows[$x]['id'] . '" class="button">See more!  </a></div>
</div>
			</span>

</div><h3>' . $rows[$x][name] . '</h3>' . $rows[$x][date] . ' 
';
   
   

CSS

#main-container{clear:both; background-color:#e0e0e0; padding:1%; margin-bottom:2%; border-radius:4px; border:#f8f8fb solid 1px;}

#main-container-left{background-color:#e0e0e0; padding:1%; margin-bottom:2%; width:45%; float:left; border-radius:4px; border:#f8f8fb solid 1px;}

#main-container-right{background-color:#e0e0e0; padding:1%; margin-bottom:2%; width:45%; float:right; border-radius:4px; border:#f8f8fb solid 1px;}

.swrapper .box1 { display: none; }
.swrapper:hover .box2 { display: inline;  }
.swrapper:hover .box1 { display: inline;  }

.box1{}


.in-span{ padding-top:5%; margin-left:auto; margin-right:auto; width:70%;}


.in-span-1{clear:both;  width:98%; padding:1%;  }

.in-span-left{ padding:1%; margin-bottom:2%; width:44%;  float:left;  }

.in-span-right{ padding:1%; margin-bottom:2%; width:44%; float:right;}

.button{background-color:#e0e0e0; width:100%; display:block; border:#f8f8fb solid 1px; padding:10px 10px 10px 10px; text-align:center; }

Starting to feel im going in circles now looking at all this.. I am most likely missing something simple..

 

 

 

The latest attempt (I saw a JS Fiddle and cant get that to work either

  $template = '
  <div class="procduct">

	<div class="image">
 <span style="background-repeat: no-repeat; background-size:auto; background-position:center  center;  background-image: url(' . $rows[$x][photograph_url] . '); min-height:350px;">
            <div class="overlay">
                <div class="btn1">
                    <a href="/mediaone/interiography/view.php?view=' . $rows[$x]['id'] . '"> Share it Facebook!  </a>
                </div>
                <div class="btn2">
                    <a href="/mediaone/interiography/view.php?view=' . $rows[$x]['id'] . '"> Share it Twitter!  </a>
                </div>
            </div>
        </div>  
</div> 
<h3>' . $rows[$x][name] . '</h3>' . $rows[$x][date] . ' 
';

css:

.procduct {
	margin: 5px;
	padding: 5px;
	max-height: 350px;
	border: 1px solid #F00;
	float: left;
}
.image {
	z-index: -1;
	position: absolute;
}
.overlay {
	padding-top: 65px;
	padding-left: 17px;
    display:none;
}
.procduct:hover .overlay {
    display:block;     
}
.procduct:hover .image {
    opacity:0.50;    
}
.bt1 {
	float: left;
	margin: 5px;
	padding: 5px;
	background-color: #0F0;
}
.bt2 {
	margin: 5px;
	padding: 5px;
	float: left;
	background-color: #F69;
}
.clear {
	clear: both;
}

Any ideas?

 

Thanks

For an overlay to work you need the following:

 

  • A parent wrapper, with position relative.
  • The content that lives inside the wrapper
  • The overlay.

Your markup could look like this.

<div class="has-overlay">
  <img src=".." alt="">
  <div class="has-overlay__overlay>
    Overlay content
  </div>
</div>
.has-overlay {
  position: relative;
}

.has-overlay__overlay {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0,0,0,0.6);
}

This is just an example of a very basic image overlay, but it could overlay anything that is contained within the .has-overlay div. The overlay of course contain any kind of content.

 

There's no need for anything more complex than this really.

 

If you want it to appear on hover the css is simply:

.has-overlay {
  position: relative;
}
.has-overlay:hover .has-overlay__overlay {
  opacity: 1;
}

.has-overlay__overlay {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0,0,0,0.6);
  opacity: 0;
}

I can see a lot of what is known as 'code smells' in your css, things like magic numbers etc. I suggest you take a read of the following post, it will help you to gradually eliminate these from your css http://csswizardry.com/2012/11/code-smells-in-css/

Edited by rbrtsmith

  • Author

For an overlay to work you need the following:

 

  • A parent wrapper, with position relative.
  • The content that lives inside the wrapper
  • The overlay.

Your markup could look like this.

<div class="has-overlay">
  <img src=".." alt="">
  <div class="has-overlay__overlay>
    Overlay content
  </div>
</div>
.has-overlay {
  position: relative;
}

.has-overlay__overlay {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0,0,0,0.6);
}

This is just an example of a very basic image overlay, but it could overlay anything that is contained within the .has-overlay div. The overlay of course contain any kind of content.

 

There's no need for anything more complex than this really.

 

I cant believe that Just happened. I read this and could just face plant my desk.

 

Thank you.

 

Another famous coders block on my part.

Thanks for that link to code smells rbrtsmith. Seriously a great article.

 

I agree, I read it around a year ago, completely changed the way I wrote CSS.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.