Pero B
Pero B

Reputation: 53

CSS background cut

this is my code, and as you can see body has some background image, and #cont should be white. But I want my div elements to be transparent in a way that I can see body background image. Any CSS tricks that can do that?

body {
  margin: 0;
  border: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  background-image: url(image.png);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
#cont {
  margin: 0;
  border: 0;
  padding: 0;
  background-color: white;
}
#cont div {
  margin: 20px;
  border: 1px solid gray;
  padding: 0;
  width: 50px;
  height: 50px;
  float: left;
}
<div id="cont">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>

Upvotes: 2

Views: 7528

Answers (3)

Razia sultana
Razia sultana

Reputation: 2221

Check it:-

body {
  margin: 0;
  border: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
#cont {
  margin: 0;
  border: 0;
  padding: 0;
  opacity: 0.3;
}
#cont div {
  margin: 20px;
  border: 1px solid gray;
  padding: 0;
  background-color: white;
  width: 50px;
  height: 50px;
  float: left;
  background-image: url("http://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg");
  background-attachment: fixed;
  background-position: top center;
}
<div id="cont">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>

Upvotes: 0

Poney
Poney

Reputation: 511

You can add a background-image to your div elements and play with background-position to give the illusion of being transparent.

If you're using JavaScript you can get the x/y position of the element and dynamically adjust the css.

Upvotes: 0

jotavejv
jotavejv

Reputation: 2086

here is the snippet updated:

body {
  margin: 0;
  border: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  background-image: url(https://placekitten.com/1000/1000);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover; /*include this*/
  background-attachment: fixed; /*include this*/
}
#cont {
  margin: 0;
  border: 0;
  padding: 0;
  background-color: white;
}
#cont div {
  margin: 20px;
  border: 1px solid gray;
  padding: 0;
  width: 50px;
  height: 50px;
  /*float: left; replaced by display inline-block*/
  display: inline-block; /*include this*/
  background: url(https://placekitten.com/1000/1000) center center no-repeat; /* <<< same body image*/
  background-size: cover; /*include this*/
  background-attachment: fixed; /*include this*/
}

Here is the full example: jsbin

Upvotes: 3

Related Questions