2016-11-14 10 views
1

これは私のコードです。の本文には背景イメージがあり、#contは白である必要があります。しかし私はdiv要素を、私がボディの背景画像を見ることができるように透明にしたい。それを行うことができるCSSトリックですか?CSSの背景カット

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>

+2

あなたのフィドルは表示されません。問題。 –

+0

image.pngをリンクとして追加します。 –

+0

私は知っていますが、私はdiv要素が#cont背景のように白であることは望ましくありません。私はそれらに白い背景をカットして体の背景画像を表示したいので –

答えて

3

は、スニペットが更新されます。ここでは

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*/ 
} 

は完全な例である:jsbin

+1

偉大な考えが...何か新しいことを学んだ:) – kukkuz

0

あなたのdiv要素に背景画像を追加し、透明であることの錯覚を与えるために、バックグラウンド位置で遊ぶことができます。

JavaScriptを使用している場合、要素のx/y位置を取得してCSSを動的に調整できます。

+0

ええ、それは私の心を渡りましたが、あまりにも調整するように思えます。私はCSSでそれを解決できると期待していた –

+0

私はあなたがこの振る舞いに達することができる唯一の方法だと思う。 – GiuServ

+0

またはボーダーで遊ぶことができます。 .contは透明にしますが、上端と下端に白い枠線を0のパディングで追加します。次に、divに左右の罫線を追加します。 – Poney

0

それを確認します - ここ

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>

+1

まあ、私はあなたが間違っていると思う - その逆の周り – kukkuz

+0

は、不透明度の増減を使用して透明を変更することができます。 –

+0

あなたは私にjsfiddleを見せてもらえますか? –