Static background image with transparent content - html

This is a question for the CSS gurus. A trend at the moment seems to be to place an image in the background and then have a transparent content scroll over the top.
AIM
What technique is used to produce this result, where the top content is transparent and slides over a background image.
http://jsfiddle.net/spadez/2uUEL/9/embedded/result/
MY ATTEMPT
What I have tried to do is apply a background and then make the top section transparent on top of it.
http://jsfiddle.net/spadez/N9sCD/3/
body {
background-image"http://www.hdwallpapers.in/walls/abstract_color_background_picture_8016-wide.jpg";
}
#top {
height: 160px;
opacity:0.4;
filter:alpha(opacity=40);
}
#section {
height: 600px; background-color: blue;
}
QUESTION
How has this technique of a transparent div moving over a static background image been achieved in my first link and how can I reproduce it. It must be a CSS solution because it still works without javascript enabled.

Here's a FIDDLE
<div id="top">
<span class="mask">
<img src="https://app.h2ometrics.com/build/v0.1.1a/styles/img/chrome_logo.png" class="logo" alt="Chrome">
Link 3
Link 2
Link 1
</span>
</div>
<div class="section l">
</div>
<div class="section d">
</div>
#top {
background:url(http://www.hdwallpapers3d.com/wp-content/uploads/2013/06/6.jpg) fixed;
background-size: cover;
position: relative;
height: 400px;
}
#top a {
background: rgba(200,200,200,0.5);
display: block;
float: right;
margin: 10px 15px;
padding: 2px 5px;
text-decoration: none;
color: #111;
cursor: pointer;
border: 2px solid #ddd;
border-radius: 8px;
transition: color 0.2s ease-in;
}
#top a:hover {
color: #fff;
}
.mask {
background: rgba(0,187,255,0.5); /* or hex combined with opacity */
position: absolute;
display: block;
top: 0;
left: 0;
width: 100%;
height: 100%;
box-shadow: inset 0 -5px 8px -3px #666; /* makes #top little inset */
}
.logo {
position: relative;
width: 60px;
height: 60px;
margin: 10px;
}
.section {
height: 600px;
}
.l {
background: #ddd;
}
.d {
background: #333;
}
Update #top content placed inside .mask which removes need for z-index.

You were essentially correct in building but your CSS has some errors.
body {
background: url('http://www.hdwallpapers.in/walls/abstract_color_background_picture_8016-wide.jpg') fixed; /* fixed stops background from scrolling */
background-size: cover cover; /* expands bg image to cover body */
}
#top {
height: 160px;
color: #fff; /* this just makes the text visible on your dark bg */
}
You don't need to set the opacity of #top because without a background set it will already be transparent.

Try this:
HTML - pushed the menu into its own div
<div id="top">
<div id="menu">
logo
link 1
link 2
</div>
</div>
<div id="section">
</div>
CSS - removed margin from body, set the background to a fixed position and to always cover the whole body, added background color to menu. Note that #top does not need a transparency as it is 100% transparent by default. If you want to get a 'colour washed' looking image it would be better to adjust the image itself rather than trying to re-create a colour overlay.
body {
margin: 0;
background: url("http://www.hdwallpapers.in/walls/abstract_color_background_picture_8016-wide.jpg") fixed;
background-size: cover;
}
#top {
height: 500px;
}
#menu {
padding: 10px;
background-color: #fff;
}
#section {
height: 600px; background-color: blue;
}

Related

How can I make a background continuous?

As you can see, in the title block, only the upper half has background, I want the whole title block to have the same background. Of course, I can set background for the title block itself, but this way the background won't look continuous, as you can see in the fiddle.
Is there a way to achieve this with pure css?
.header {
width: 100%;
padding-top: 30%;
background: url('https://cchc-herald.org/images/discuss_cavatar/titleSampleBG.jpg') no-repeat 50% 50%;
background-size: cover;
position: relative;
}
.title {
position: absolute;
transform: translateY(-50%);
padding: 8px 24px;
font-size: 24px;
background: none;
border-radius: 50px;
border: 4px solid white;
left: 10%
}
body {
background-color: #eee
}
.title.b {
background: url('https://cchc-herald.org/images/discuss_cavatar/titleSampleBG.jpg') no-repeat 50% 50%;
background-size: contain
}
<div class="header">
<div class="title"> Title Title </div>
</div>
<div class="header" style="margin-top:60px">
<div class="title b">
Title Title
</div>
</div>
Fiddle: https://jsfiddle.net/s7pkr2w8/1/
Here is an idea using clipping and masking
.header {
padding-top: 30%;
position: relative; /* relative here !! **/
display:flex;
z-index:0;
}
.title {
font-size: 24px;
color:#fff;
border-radius: 50px;
margin:auto auto 0 10%; /* position the element using flexbox instead of absolute */
-webkit-mask:linear-gradient(#fff 0 0); /* clip the pseudo element to only the title shape*/
}
/* extra div needed for the white border*/
.title > div {
padding: 8px 24px;
border:4px solid #fff;
position:relative;
border-radius: inherit;
}
/**/
/* two pseudo element relative to the container having the same background
to have the continuous effect
*/
.title::before,
.header::before{
content:"";
position:absolute;
z-index:-1;
top:0;
bottom:0;
left:0;
right:0;
background: url('https://cchc-herald.org/images/discuss_cavatar/titleSampleBG.jpg') no-repeat 50% 50%/cover;
}
.header::before {
clip-path:inset(0 0 20px 0); /* cut 20px from the bottom to be around the middle of the title */
}
body{
background-color:#eee
}
<div class="header">
<div class="title">
<div>Title Title</div>
</div>
</div>
you can try to set the background on a parent element or just event to the whole body:
body{
background:url('https://cchc-herald.org/images/discuss_cavatar/titleSampleBG.jpg') no-repeat 50% 50%;
background-size:cover;
}

How to Overlay Image on Background in Twitter Bootstrap Section

I have an image that I would like to overlay on top of a background gradient that I have set on a section element. Both the background gradient and image I am setting in CSS and calling via a class in HTML. Originally when just using the background gradient it worked fine, but after adding the image to place over the background gradient the background gradient disappeared?
.banner-gradient {
background: radial-gradient(circle, #ba000b, #9e0008);
color: white;
z-index: 0;
}
.banner-overlay {
background: url("../imagery/image.png");
max-width: 100%;
height: auto;
background-position: bottom;
background-repeat: repeat-x;
z-index: 1;
}
.section-align-center {
text-align: center;
}
<section class="banner-gradient banner-overlay section-align-center">
<div class="container">
<p>image over background gradient</p>
</div>
</section>
Try using background-image instead of background for image.
.banner-gradient:before {
content: " ";
width: 100%;
height: 100%;
position: absolute;
z-index: 1;
top: 0;
left: 0;
background: -webkit-radial-gradient(top center, ellipse cover, rgba(255,255,255,0.2) 0%,rgba(0,0,0,0.5) 100%);
}
.banner-overlay {
background: url('https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a') repeat;
}
.section-align-center {
height: 400px;
position: relative;
}
<section class="banner-gradient banner-overlay section-align-center">
<div class="container">
<p>image over background gradient</p>
</div>
</section>
I solved this with the help of this post. You must first place the banner-gradient in your outer div then in your inner div use banner-image.
HTML
<section class="banner-gradient section-align-center">
<div class="container banner-overlay">
<p>image over background gradient</p>
</div>
I would rather edit the class of the element you want the transparency in
<div class="background">
<div class="transbox">
<p>This is some text that is placed in the transparent box.</p>
</div>
</div>
div.background {
background: url('https://www.w3schools.com/css/klematis.jpg') repeat;
border: 2px solid black;
}
div.transbox {
margin: 30px;
background-color: #ffffff;
border: 1px solid black;
opacity: 0.6;
filter: alpha(opacity=60); /* For IE8 and earlier */
}
div.transbox p {
margin: 5%;
font-weight: bold;
color: #000000;
}
https://jsfiddle.net/mergatroid/xkmyqjec/1/

Creating a option-choice landing page

I want to create a landing page like a game. The visitor gets the option either to chose "Professioneel" or "Speels".
Telling it is easy but programming it is hard for me, so this is what I want:
2 div's with 2 different background-image when someone hover over one of the divs I want the background-image to scale (ONLY THE IMAGE) and the opacity placed on the div to change from 50% to 80%.
And a really nice future would be to display a snow falling gif over the image.
This is what I want to create:
Before
After:
What I have achieved till now is making the 2 divs with a background-image and I'm not even sure if that is the right way.
Can someone please help me out?
This is what happens when I hover with my current code: (the whole div scales, not only the image)
As an user asked, here some code:
#containerEntree {
height: 100vh;
width: 1920px;
padding-left: 0;
padding-right: 0;
}
#professioneelContainer {
background-color: red;
text-align: center;
width: 1920px;
height: 475px;
}
#speelsContainer {
background: red;
width: 100%;
height: 475px;
text-align: center;
}
.entreeTekst:hover {
transform: scale(1.2);
}
.entreeTekst {
width: 100%;
height: 100%;
position: relative;
transition: all .5s;
margin: auto;
}
.entreeTekst > span {
color: white;
/* Good thing we set a fallback color! */
font-size: 70px;
position: absolute;
}
<div class="container" id="containerEntree">
<div id="professioneelContainer">
<div class="entreeTekst">
<span>professioneel</span>
<img src="img/professioneel.jpg" />
</div>
</div>
<div id="speelsContainer">
<div class="entreeTekst">
<span>Speels</span>
<img src="img/speels.jpg" />
</div>
</div>
</div>
Please note that I'm still working on it so don't say that this (of course) won't work.
You can do this by using 2 divs with background images and use padding on the div to replicate the aspect ratio of the background image. Scale the image using background-size on :hover. Then use a pseudo element to create the color overlay and transition the opacity on :hover, then use the other pseudo element on top of that with the text and the "snow" gif as a background.
body {
width: 600px;
max-width: 80%;
margin: auto;
}
div {
background: url('https://static.tripping.com/uploads/image/0/5240/towns-funny-names-us_hero.jpg') center center no-repeat / 100%;
height: 0;
padding-bottom: 33.33333%;
position: relative;
transition: background-size .25s;
}
.speel {
background-image: url('http://www.luketingley.com/images/large/The-Punchbowl-Web-Pano.jpg');
}
div::after, div::before {
content: '';
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
}
div::before {
opacity: .5;
transition: opacity .25s;
}
.pro::before {
background: blue;
}
.speel::before {
background: red;
}
div::after {
display: flex;
align-items: center;
justify-content: center;
color: #fff;
font-family: sans-serif;
font-size: 1.5em;
font-weight: bold;
}
.pro::after {
content: 'PROFESSIONEEL';
}
.speel::after {
content: "SPEELS";
}
div:hover::after {
background: url('https://media.giphy.com/media/26BRyql7J3iOx875u/giphy.gif') center center no-repeat / cover;
}
div:hover::before {
opacity: 0.8;
}
div:hover {
background-size: 150%;
}
<div class="pro">
</div>
<div class="speel">
</div>
You can simply increase the background-size: height width; and opacity: value; property when you hover over an element. You can, if you want to, add some transition to make it smooth. This only scales the background image, not the div itself.
#d {
background-image: url(https://cdn.pixabay.com/photo/2016/10/29/20/52/cincinnati-1781540_960_720.png);
height: 200px;
width: 200px;
background-size: 100px 100px;
background-repeat: no-repeat;
background-position: center;
/*To make the transistion smooth*/
-o-transition:.5s;
-ms-transition:.5s;
-moz-transition:.5s;
-webkit-transition:.5s;
transition:.5s;
opacity: 0.5;
}
#d:hover {
background-size: 110px 110px;
opacity: 0.8;
}
<div id='d'>
</div>

Changing image on hover without using an extra div

I'm trying to make my footer img change to a different img on hover, I've accomplished this by creating each img as a div, however, I'm trying to avoid this, is there any other possible way to do this?
Current CSS:
.footerLeft {
float:left;
width: 50%;
height: 100px;
color: #fff;
background-color: #33383b;
}
.footerLeft p {
margin-left: 25px;
}
.footerLeft img {
width: 175px;
height: 50px;
padding-left: 25px;
margin-top: 10px;
}
.footerRight {
float:right;
width: 50%;
height: 100px;
color: #fff;
background-color: #33383b;
}
.footerRight img {
width: 35px;
height: 35px;
}
.footerRight p {
text-align: right;
position:relative;
margin-top: -20px;
margin-right: 20px;
}
Current HTML:
<div class="footerLeft">
<img src="img/logo.png">
<p>Sharpturn Network© 2016</p>
</div>
<div class="footerRight">
<ul id="menu">
<li><img src="img/footer/facebook.png"></li>
<li><img src="img/footer/twitter.png"></li>
<li><img src="img/footer/youtube.png"></li>
</ul>
<p>Designed by Ryan Williams</p>
</div>
Set one img as the background img, and the other as the background img on hover.
footer {
background-image: url(...);
}
footer:hover {
background-image: url(...);
}
The only alternative I can think of to using a div (or some other element with background image) is to use JavaScript. This will allow you to change the 'src' of the image on hover.
element.setAttribute('src', 'http://www.example.com/image.jpg');
As #partians said, just added "background-size" as cover, I assumed that you need that.
footer {
width: 900px;
height: 600px;
background-image: url("http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg");
background-size: cover
}
footer:hover {
background-image: url("http://image2.redbull.com/rbcom/010/2013-07-25/1331603705670_2/0010/1/900/600/2/red-bull-illume.jpg");
}
<footer> </footer>
try this.just copy and pastea and try it.only have to replace your two images.
<!DOCTYPE html>
<html>
<head>
<title>hover</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<style type="text/css">
body{
margin:0;
padding:0;
}
.imageOne{
width: 500px;
height: 500px;
box-shadow: 1px 2px 1px black;
background-image: url(http://www.ron-gatepain.com/images/Golden_Gate_Bridge.JPG?306);
background-repeat: no-repeat;
}
.imageTwo{
width: 500px;
height: 500px;
box-shadow: 1px 2px 1px black;
background-image: url(http://gym1526-english.narod.ru/images/Statue.jpg);
background-repeat: no-repeat;
}
</style>
</head>
<body>
<div id="myimage" class="imageOne">
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#myimage").mouseenter(function(){
$(this).addClass("imageTwo").removeClass("imageOne");
});
$("#myimage").mouseleave(function(){
$(this).addClass("imageOne").removeClass("imageTwo");
});
});
</script>
</body>
</html>
EDIT: Note that some of the other answers will change the image when hovering any part of the whole footer container. This method changes the image only when hovering the image's region.
You could declare a pseudo-element on the footer container in the same position as the image to replace it on hover. Basically, using a combination of opacity and z-index you can hide the original image and show the pseudo element in the same place.
Here is an example of how to accomplish that:
.footerLeft:before {
/* this creates the pseudo-element */
width: 175px; /* Same as img */
height: 50px; /* Same as img */
margin-left: 25px; /* Same as img */
margin-top: 10px; /* Same as img */
background: url(http://lorempixel.com/175/50/cats); /* your replacement img */
position: absolute;
z-index: 0;
content: '';
}
.footerLeft img {
width: 175px;
height: 50px;
margin-left: 25px; /* I used margin-left instead of padding-left */
margin-top: 10px;
position: relative; /* necessary for the z-index to work */
z-index: 1; /* puts the img above the pseudo-element by default */
}
.footerLeft img:hover {
opacity: 0; /* hides the original image on hover */
}
EDIT 2: If you want the social media icons to change images on hover, the process is similar. Create pseudo elements on each a tag with CSS, with the same dimensions as the original img. Configure a different replacement image for each icon:
.footerRight img {
width: 35px;
height: 35px;
position: relative; /* enables the use of z-index */
z-index: 1; /* puts the image on top by default */
transition: opacity .2s; /* remove this for no smooth transtion */
}
.footerRight a:before {
width: 35px; /* same a img */
height: 35px; /* same a img */
z-index: 0;
content: '';
position: absolute;
}
.footerRight li:nth-child(1) a:before {
/* facebook icon */
background: url(http://lorempixel.com/35/35/cats);
}
.footerRight li:nth-child(2) a:before {
/* twitter icon */
background: url(http://lorempixel.com/35/36/cats);
}
.footerRight li:nth-child(3) a:before {
/* youtube icon */
background: url(http://lorempixel.com/36/35/cats);
}
.footerRight img:hover {
opacity: 0; /* hides the original image */
}
Note: This method has the advantage of loading the replacement images immediately. The same might not be true for other methods.
You can see the working example here: http://codepen.io/wilman/pen/mVZVzg

'background-position' on 'background-color' with CSS

I'm trying to use 'bakground-position' in the background of my div, but not working.
When background an image, the 'background-position' works, but with 'background-color' is not working.
What can I do?
This is my CSS:
#defaultContent {
width: 983px;
min-height: 382px;
margin: 0 auto;
background-color: #000000;
background-position: right 50px;
}
You can provide an background-image as a solid color, creating a monochrome gradient:
#defaultContent {
width: 983px;
min-height: 382px;
margin: 0 auto;
background-image: linear-gradient(#000, #000);
background-position: right 50px;
background-repeat: no-repeat;
}
The gradient is fully compatible with an image, and if you set both colors to the same, it is fully equivalent to a solid color
demo
You can workaround with a div only for background, simulating it by mixing position: absolute offsets and negative z-index.. (Though I've tested only in chrome)
See fiddle
HTML
<div id="defaultContentParent">
<div id="defaultContent"></div>
<div id="defaultContentContent"><div>
</div>
CSS
#defaultContentParent {
border: 1px solid #00f;
background-color: #aaa;
color: #fff;
position: relative;
width: 200px;
min-height: 140px;
margin: 0 auto;
z-index: 0;
}
#defaultContent {
background-color: #000000;
height: 50px;
position: absolute;
top: 50px;
right: 0;
width: 80px;
z-index: -1;
}
#defaultContentContent {
z-index: 9999;
}
You can't position a background-color property, since that property fills the entire space. Likewise, you can't use background-color with a background image, because that would, basically, replace your background image with the filled background-color, at which point there is no reason to use a background image at all!
Are you perhaps thinking of using a gradient, or giving a background image a filter of a certain filter? That would be a different question.