2017-01-25 6 views
0

Ich glaube, ich könnte auf einen General Sibling Selector ~ Bug stoßen, aber ich bin mir nicht ganz sicher. Mein Code p ~ div löst den Selektor nicht aus, sondern tauscht mit dem spezifischen Klassennamen text_paragraph aus. Meine Geige und Code sind unten.CSS General Sibling Selector möglicher Fehler

Fiddle https://jsfiddle.net/t2Ljmgar/

.outer_container { 
 
    margin: 20px; 
 
    padding-bottom: 10px; 
 
    background-color: rgba(216, 23, 27, 1); 
 
    border: 1px solid rgba(0, 0, 0, 1); 
 
} 
 
.text_paragraph { 
 
    margin: 10px 10px 0px 10px; 
 
    padding-left: 10px; 
 
    font: 2em/4em Arial, Helvetica, "sans-serif"; 
 
    color: rgba(64, 64, 64, 1.00); 
 
    border: 1px solid rgba(0, 0, 0, 1); 
 
    background-color: rgba(180, 180, 120, 1); 
 
} 
 
.text_container { 
 
    margin: 10px 10px 0px 10px; 
 
    padding-left: 10px; 
 
    font: 1.5em/3em Arial, Helvetica, "sans-serif"; 
 
    color: rgba(64, 64, 64, 1.00); 
 
    background-color: rgba(215, 215, 251, 1); 
 
} 
 
p ~ div { 
 
    background-color: rgba(0, 255, 255, 1); 
 
}
<div class="outer_container"> 
 
    <p class="text_paragraph">Important header up top.</p> 
 
    <div class="text_container">This is the first text on the page.</div> 
 
    <div class="text_container">This is the second text on the page.</div> 
 
    <div class="text_container">This is the third text on the page.</div> 
 
</div>

der spezifischen Klassennamen löst den Wähler ändern P.

.outer_container { 
 
    margin: 20px; 
 
    padding-bottom: 10px; 
 
    background-color: rgba(216, 23, 27, 1); 
 
    border: 1px solid rgba(0, 0, 0, 1); 
 
} 
 
.text_paragraph { 
 
    margin: 10px 10px 0px 10px; 
 
    padding-left: 10px; 
 
    font: 2em/4em Arial, Helvetica, "sans-serif"; 
 
    color: rgba(64, 64, 64, 1.00); 
 
    border: 1px solid rgba(0, 0, 0, 1); 
 
    background-color: rgba(180, 180, 120, 1); 
 
} 
 
.text_container { 
 
    margin: 10px 10px 0px 10px; 
 
    padding-left: 10px; 
 
    font: 1.5em/3em Arial, Helvetica, "sans-serif"; 
 
    color: rgba(64, 64, 64, 1.00); 
 
    background-color: rgba(215, 215, 251, 1); 
 
} 
 
.text_paragraph ~ div { 
 
    background-color: rgba(0, 255, 255, 1); 
 
}
<div class="outer_container"> 
 
    <p class="text_paragraph">Important header up top.</p> 
 
    <div class="text_container">This is the first text on the page.</div> 
 
    <div class="text_container">This is the second text on the page.</div> 
 
    <div class="text_container">This is the third text on the page.</div> 
 
</div>

+2

Das ist nur CSS Spezifität ist. Eine Klasse hat eine höhere Spezifität als ein Tag/Elementname. https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity –

+0

Alle Tutorials, die ich online gesehen habe, verwenden Tag-Namen wie P und DIV und sie funktionieren gut. Ich kann nicht herausfinden, warum ein Tag-Name in dieser spezifischen Instanz nicht funktioniert. – DR01D

+0

Michael hat dir schon gesagt warum. Lesen Sie über CSS-Spezifität unter https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity – j08691

Antwort

1

Ihre Klasse Ihr allgemeines Element Selektor überschreibt, da es eine höhere Spezifität aufweist.

Kommentar Ihre .text_containerbackground-color Eigenschaft und sehen, wie die generische übernimmt:

.outer_container { 
 
    margin: 20px; 
 
    padding-bottom: 10px; 
 
    background-color: rgba(216, 23, 27, 1); 
 
    border: 1px solid rgba(0, 0, 0, 1); 
 
} 
 
.text_paragraph { 
 
    margin: 10px 10px 0px 10px; 
 
    padding-left: 10px; 
 
    font: 2em/4em Arial, Helvetica, "sans-serif"; 
 
    color: rgba(64, 64, 64, 1.00); 
 
    border: 1px solid rgba(0, 0, 0, 1); 
 
    background-color: rgba(180, 180, 120, 1); 
 
} 
 
.text_container { 
 
    margin: 10px 10px 0px 10px; 
 
    padding-left: 10px; 
 
    font: 1.5em/3em Arial, Helvetica, "sans-serif"; 
 
    color: rgba(64, 64, 64, 1.00); 
 
    /* background-color: rgba(215, 215, 251, 1); */ 
 
} 
 
p ~ div { 
 
    background-color: rgba(0, 255, 255, 1); 
 
}
<div class="outer_container"> 
 
    <p class="text_paragraph">Important header up top.</p> 
 
    <div class="text_container">This is the first text on the page.</div> 
 
    <div class="text_container">This is the second text on the page.</div> 
 
    <div class="text_container">This is the third text on the page.</div> 
 
</div>

+0

Ich habe getestet und Sie haben Recht. So kann 'P ~ DIV' eine Hintergrundfarbe zuweisen, aber nicht eine vorhandene Hintergrundfarbe ÜBERNEHMEN. Aber 'text_paragraph ~ div' kann die vorhandene Hintergrundfarbe ÜBERNEHMEN. Mein Kopf dreht sich. 8-) – DR01D