CSS-Regeln werden ohne Klassen angewendet, warum?
P粉795311321
P粉795311321 2023-09-04 13:26:05
0
1
451
<p>Ich definiere CSS-Regeln: </p> <pre class="brush:php;toolbar:false;">.info-specs h2, h3, h4, h5 { Schriftgröße: 1,5em; Texttransformation: keine; }</pre> <p>Dies sollte nur für h5 in Elementen mit der Klasse „info-specs“ funktionieren. Bei der Untersuchung stellte ich jedoch fest, dass auch andere h5-Elemente diese Regel verwenden. Warum? Hier ist ein Beispiel: </p> <p> <pre class="snippet-code-css lang-css Prettyprint-override"><code>.info-specs h2, h3, h4, h5 { Schriftgröße:5em; Texttransformation: keine; }</code></pre> <pre class="snippet-code-html lang-html Prettyprint-override"><code><h5>mytest </h5></code></pre> </p>
P粉795311321
P粉795311321

Antworte allen(1)
P粉308089080

浏览器的 CSS 解释器将查找任何 h3h4h5 元素,并且仅查找 h2 它将查看它是否在 .info-specs 内。逗号或分组选择器将逗号分隔的所有内容视为单独的选择。

您的问题的可能解决方案是:

/* These select for any h2, h3, h4 and h5 within .info-specs */

.info-specs h2,
.info-specs h3,
.info-specs h4,
.info-specs h5
{
  text-decoration: underline;
}

/* These select for ant h2, h3, h4 and h5 that are direct chldren of .info-specs */
.info-specs > h2,
.info-specs > h3,
.info-specs > h4,
.info-specs > h5
{
  color: red;
}
<div class="info-specs">
  <p>In this example the headings within inf-specs will all be underlined but only the headings that are direct children of info-specs will be coloured red.</p>
  <h2>Heading 2</h2>
  <h3>Heading 3</h3>
  <div>
    <h3>Heading 3 in another div</h3>
  </div>
  <h4>Heading 4</h4>
  <h5>Heading 5</h5>
</div>
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!