2017-11-15 6 views
-1

バックグラウンドイメージでスワップするサブクラスを指定する方法を教えてください。私はどちらかのサブクラスPOST1などでポストに依存して画像を変更したり、理想的にHTMLでそれを上書きしたいが、それは働いていないサブクラスを使用して背景イメージを変更できますか?

.background-image-post{ 
    background-image:url('../images/posts/background-post.jpg'); 
    background-repeat: no-repeat; 
    background-position: center center; 
    background-size: cover; 

} 

は、ここに私のCSSです。

どのような考えですか?

+1

いない '選択図】図-画像post.post1 {背景画像:のURL(...); } '仕事? – jhpratt

+1

"サブクラス"を定義する –

答えて

1

ここではサブクラスが間違った言葉です。あなたが必要とするのは、より高いレベルの特異性を作り出す「追加の」クラスだけです。

例:クラスbackground-image-postのすべてのdivは、プレースホルダーの背景を取得します。ただし、とphilクラスのdivは、より具体的にはなので、fillmurrayの背景を取得します。

.background-image-post{ 
 
    display: inline-block; 
 
    margin: 3px; 
 
    background-image:url('//placehold.it/200x200'); 
 
    width: 200px; 
 
    height: 200px; 
 
    background-repeat: no-repeat; 
 
    background-position: center center; 
 
    background-size: cover; 
 
} 
 

 
.background-image-post.phil { 
 
    background-image:url('//fillmurray.com/200/200'); 
 
}
<div class="background-image-post"></div> 
 
<div class="background-image-post phil"></div>

+0

シンプルな_order_(HTMLクラス属性ではなくスタイルシート内)だけで2番目のセレクタを作るので、2番目のセレクタに '.background-image-post'を追加する必要はありません優先されます。 – CBroe

+0

絶対に。そのクラス名がOPにその背景が適用されることを望まない他の意味がある状況でない限り。 –

+0

ありがとうございました。それは私が必要としたものです。 – YR22

関連する問題