2011-08-31 16 views
10

これまで私はCSSについて多くのことを学んでいましたが、今はスタイルを再利用する方法を覚えていません。スタイルを再利用するには?

例:

私はクラスtabといくつかのタブがあると私はJavaScriptを使用して、それらを切り替えることができます。現在選択されているタブには別のクラス、activeがあります。

彼らのCSSスタイル:

.tab { 
    position: relative; 
    top: 0; 
    left: 0; 
    width: 100%; 
    padding: 15px 0 15px 0; 
    border: solid thin #CCC; 
    text-align: center; 
    font-weight: bold; 
    margin-bottom: 10px; 
    color: #272F42; 
    cursor: pointer; 
    background-color: white; 
} 

.active { 
    position: relative; 
    top: 0; 
    left: 0; 
    padding: 15px 0 15px 0; 
    border: solid thin #CCC; 
    text-align: center; 
    font-weight: bold; 
    margin-bottom: 10px; 
    color: #272F42; 
    cursor: default; 
    background-color: #FFCF75; 
} 

両方のスタイルは2、cursorbackground-color除いて同じスタイルをたくさん持っています。

私の質問は、どうやって.tabスタイルを無効にして、それを.activeで使用できるのですか?

.active { //extends .tab 
    cursor: default; 
    background-color: #FFCF75; 
} 

ありがとう:

私はこのような何かを達成したいです。

答えて

5

あなたは可能性があり、おそらく、そのような要素に両方のクラスを適用する必要があります:あなたはこの2つのクラスの特定の組み合わせのためのCSSルールをしたい場合、あなたはそのようにそれを行うだろう

<a class="tab active"></a> 

.tab { 
    position: relative; 
    top: 0; 
    left: 0; 
    width: 100%; 
    padding: 15px 0 15px 0; 
    border: solid thin #CCC; 
    text-align: center; 
    font-weight: bold; 
    margin-bottom: 10px; 
    color: #272F42; 
    cursor: pointer; 
    background-color: white; 
} 

.active 
{ 
    cursor: default; 
    background-color: #FFCF75; 
} 

.tab.active /* no space */ 
{ 
    /* styles for elements that are both .tab and .active */ 
    /* leaving .active reusable for things other than tabs */ 
    /* and allowing override of both .tab and .active */ 
} 

これは、あなたのスタイル宣言の不要なコピーを作成することを避けることができます...と要素の両方を持っている場合、個々のクラスのいずれかをオーバーライドするためにあなたの特異性を提供します。

+0

いいえ、それから、一度に両方のクラスが必要で、それはすでにあなたのために "継承"をしています。 – Ryan

+0

正確ですが、他の2つのクラスのどちらかをオーバーライドするための特殊性を得ることができます。 – canon

5

.tabのルールを.tab, .activeに変更してください。

9

class属性の両方のクラス名を使用してください。 .activeルールでは、2番目の例のように異なるスタイルのみを定義します。

<div class="tab active"></div> 
+1

上記のように、クラスをスワップするのではなく、javascriptに '.active'クラスを追加してください。 – steveax

+1

私はこれがOPが望んでいるとは思わない。スタイルシートにスタイルを組み込む方法を知りたい。この例を参照してください。 –

+0

私は説明されたシナリオで使用されるベストプラクティスを説明しました – Samich

2

このセレクタの継承は、SASSという素晴らしい機能です。

普通のCSSを使用する場合は、セレクタの継承に関するセクションを参照してください。@extendを使用してSASSコードを通常のCSSに変換する方法を確認できます。

+0

もちろんそれは価値があります。とても興味深い。私は彼らがスタイルを拡張する方法が好きだった。 :D –

0

要素に複数のクラスを適用できます。したがって、class = "tab active"の要素を持つことができ、両方の仕様を含むようにすることができます。

3
.active, .tab { 
    ... full style of both here 
} 

.active { 
    ... the styles that differ from .tab 
} 
6

これを行います。スタイルを組み合わせ、コンマで区切ります。次に、差異を対象とする他のルールを追加します。あなたのコメント

に基づいて

.tab, .active { 
    position: relative; 
    top: 0; 
    left: 0; 
    width: 100%; 
    padding: 15px 0 15px 0; 
    border: solid thin #CCC; 
    text-align: center; 
    font-weight: bold; 
    margin-bottom: 10px; 
    color: #272F42; 
} 

.tab{ 
    cursor: pointer; 
    background-color: white; 
} 

.active { 
    cursor: default; 
    background-color: #FFCF75; 
} 

EDIT

は、私は現在、クラス 属性に.activeスタイルを追加することで、タブを切り替えています。

これは私がどうなるのかです:

HTML

<div class="tab"></div> 

CSS

.tab { 
    position: relative; 
    top: 0; 
    left: 0; 
    width: 100%; 
    padding: 15px 0 15px 0; 
    border: solid thin #CCC; 
    text-align: center; 
    font-weight: bold; 
    margin-bottom: 10px; 
    color: #272F42; 
    cursor: pointer; 
    background-color: white; 
} 

.active { 
    cursor: default; 
    background-color: #FFCF75; 
} 

それからちょうどあるよう.tabを残して、.activeクラスを追加または削除します。

.activeの場合、スタイルシートでが下になると、必要なビットが上書きされます。

+0

あなたが最後の文章で言うことはおそらく私の誤りの原因でした。ありがとう。 –

0
  1. あなたは同じスタイルで基本クラスを抽出し、二つのクラスを使用して要素をマークすることができます:.tab .base
  2. あなたは組み合わせることができ
0

inhritanceクラスをサポートしていsasslessなどの動的CSSの言語を使用することができます。このように変更する必要のある部分を無効にしてください:

.tab, .active { 
    position: relative; 
    top: 0; 
    left: 0; 
    width: 100%; 
    padding: 15px 0 15px 0; 
    border: solid thin #CCC; 
    text-align: center; 
    font-weight: bold; 
    margin-bottom: 10px; 
    color: #272F42; 
    cursor: pointer; 
    background-color: white; 
} 

.active { 
    cursor: default; 
    background-color: #FFCF75; 
}