如何仅对第一个 `` 应用 CSS

huangapple go评论57阅读模式
英文:

How to apply css only for first span

问题

尝试应用CSS

.content>span:first-child{
  color: red;
  font-size: 12px;
  font-weight: bold;
}
<span class="content">
  <svg xmlns="http://www.w3.org/2000/svg">
    <use xlink:href="assets/icons.svg#back-button"></use>
  </svg>
  <span> Lorem ipsom </span>
  <!-- only this span font color should be red -->
  <span>
    <span> </span>
  </span>
</span>
英文:

Trying to apply CSS

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.content&gt;span:first-child{
  color: red;
  font-size: 12px;
  font-weight: bold;
}

<!-- language: lang-html -->

&lt;span class=&quot;content&quot;&gt;
       &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
       &lt;use xlink:href=&quot;assets/icons.svg#back-button&quot;&gt;&lt;/use&gt;
    &lt;/svg&gt;
     &lt;span&gt; Lorem ipsom &lt;/span&gt;
&lt;!-- only this span font color should be red --&gt;

&lt;span&gt;
       &lt;span&gt; &lt;/span&gt;
&lt;/span&gt;
&lt;/span&gt;

<!-- end snippet -->

for only the first span. I have tried but it is not working. If anyone knows please help to find the solution.

Demo: https://stackblitz.com/edit/angular-svg-use-gvmekn?file=src%2Fapp%2Fapp.component.html

答案1

得分: 3

使用 :first-of-type 伪类

> :first-of-type
>
> :first-of-type CSS 伪类表示一组兄弟元素中其类型的第一个元素。
>
> https://developer.mozilla.org/zh-CN/docs/Web/CSS/:first-of-type

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

span.content &gt; span:first-of-type {
  color: red;
}

<!-- language: lang-html -->

&lt;span class=&quot;content&quot;&gt;
  &lt;svg viewBox=&quot;0 0 10 2&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
  &lt;line x1=&quot;0&quot; y1=&quot;0&quot; x2=&quot;10&quot; y2=&quot;0&quot; stroke=&quot;black&quot; /&gt;
&lt;/svg&gt;

  &lt;span&gt; Lorem ipsom &lt;/span&gt;
  &lt;!-- 只有这个 span 的字体颜色应该是红色 --&gt;

  &lt;span&gt;
    Child
   &lt;span&gt; Inner child &lt;/span&gt;
  &lt;/span&gt;
&lt;/span&gt;

<!-- end snippet -->

使用相邻兄弟组合器

> +
>
> 相邻兄弟组合器 (+) 分隔两个选择器,仅当第二个元素紧随第一个元素之后,并且两者都是同一父元素的子元素时才匹配。
>
> https://developer.mozilla.org/zh-CN/docs/Web/CSS/Adjacent_sibling_combinator

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

svg+span {
  color: red;
}

<!-- language: lang-html -->

&lt;span class=&quot;content&quot;&gt;
  &lt;svg viewBox=&quot;0 0 10 2&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
  &lt;line x1=&quot;0&quot; y1=&quot;0&quot; x2=&quot;10&quot; y2=&quot;0&quot; stroke=&quot;black&quot; /&gt;
&lt;/svg&gt;

  &lt;span&gt; Lorem ipsom &lt;/span&gt;
  &lt;!-- 只有这个 span 的字体颜色应该是红色 --&gt;

  &lt;span&gt;
    Child
   &lt;span&gt; Inner child &lt;/span&gt;
  &lt;/span&gt;
&lt;/span&gt;

<!-- end snippet -->

英文:

use the :first-of-type pseudo-class

> :first-of-type
>
> The :first-of-type CSS pseudo-class represents the first element of its type among a group of sibling elements.
>
> https://developer.mozilla.org/en-US/docs/Web/CSS/:first-of-type

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

span.content &gt; span:first-of-type {
  color: red;
}

<!-- language: lang-html -->

&lt;span class=&quot;content&quot;&gt;
  &lt;svg viewBox=&quot;0 0 10 2&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
  &lt;line x1=&quot;0&quot; y1=&quot;0&quot; x2=&quot;10&quot; y2=&quot;0&quot; stroke=&quot;black&quot; /&gt;
&lt;/svg&gt;

  &lt;span&gt; Lorem ipsom &lt;/span&gt;
  &lt;!-- only this span font color should be red --&gt;

  &lt;span&gt;
    Child
   &lt;span&gt; Inner child &lt;/span&gt;
  &lt;/span&gt;
&lt;/span&gt;

<!-- end snippet -->

use adjacent sibling combinator

> +
>
> The adjacent sibling combinator (+) separates two selectors and matches the second element only if it immediately follows the first element, and both are children of the same parent element.
>
> https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

svg+span {
  color: red;
}

<!-- language: lang-html -->

&lt;span class=&quot;content&quot;&gt;
  &lt;svg viewBox=&quot;0 0 10 2&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
  &lt;line x1=&quot;0&quot; y1=&quot;0&quot; x2=&quot;10&quot; y2=&quot;0&quot; stroke=&quot;black&quot; /&gt;
&lt;/svg&gt;

  &lt;span&gt; Lorem ipsom &lt;/span&gt;
  &lt;!-- only this span font color should be red --&gt;

  &lt;span&gt;
    Child
   &lt;span&gt; Inner child &lt;/span&gt;
  &lt;/span&gt;
&lt;/span&gt;

<!-- end snippet -->

答案2

得分: 1

你可以使用 first-of-type,像这样:

.content > span:first-of-type {
    color: red;
}
英文:

You can use first-of-type so something like:

.content &gt; span:first-of-type {
    color:red;
}

答案3

得分: 1

问题是 :first-child 选择器仅在你寻找的 span.content 的第一个子元素时才适用。

在你的情况下,第一个子元素是一个 svg 标签,这就是为什么它不起作用的原因。

有三种方法可以解决这个问题:

  1. 将选择器改为 .content > span:nth-child(2)。这在上面的例子中可以工作,但如果你将其他元素放在问题的 span 和你的 svg 之间,它将再次中断。

  2. .content > span:first-of-type。这似乎是你想要的,即 .content 的第一个子元素是第一个 span。

  3. span.some-meaningful-classname - 这使你有选择地为 span 添加类的灵活性,通过在你的 span 中添加 classname="some-meaningful-classname"

根据你的需求,你可以选择其中一种方法。

另外,这里有一个关于 CSS 选择器的好参考:https://www.w3schools.com/cssref/css_selectors.php

英文:

The problem is that the :first-child selector only applies if the span you're after is the first child of .content.

In your case the first child is an svg tag, hence why it's not working.

There are 3 ways of getting around this:

  1. Make the selector .content &gt; span:nth-child(2). This'll work in the above but will again break if you ever put additional elements between the span in question and your svg.

  2. .content &gt; span:first-of-type. This seems to be what you're after, i.e. the first span that's the child of .content.

  3. span.some-meaningful-classname - This gives you the flexibility to choose exactly which spans to apply the class to by adding classname=&quot;some-meaningful-classname&quot; to your span.

Up to you which to go with, depending on your requirements.

Also, there's a good reference to CSS selectors here: https://www.w3schools.com/cssref/css_selectors.php

答案4

得分: 0

你可以通过给span添加类来实现,如下所示:

原始:

&lt;span&gt; Lorem ipsom &lt;/span&gt;

新的:

&lt;span class=&quot;firstspan&quot;&gt; Lorem ipsom &lt;/span&gt;

(编辑:抱歉,忘记将&lt;span&gt;标记为代码,所以它没有正确显示)

接下来,你可以在CSS文件中添加类名,像这样:

.firstspan {
  /* 这是将文本变为红色的示例代码 */
  color: red;
  font-size: 12px;
  font-weight: bold;
}

希望对你有帮助。

英文:

You can do it by just giving the span a class like the following

Old:

&lt;span&gt; Lorem ipsom &lt;/span&gt;

New:

&lt;span class=&quot;firstspan&quot;&gt; Lorem ipsom &lt;/span&gt;

(Edit: Sorry forgot to mark the <span> as code so it did not display correctly)

next you can add the class name to the css file like this:

.firstspan {
  /* this is a example of code that will make the text red*/
  color: red;
  font-size: 12px;
  font-weight: bold;
}

I hope this helps.

答案5

得分: 0

因为.container也是一个span,所以声明会重叠在一起。

要做到这一点,你只需要定位第N个子元素,比如span:nth-child(2),或者使用nth-child('child No.')来选择你想要的span。以下是解决方案:

HTML:

<span class="content">
  <svg xmlns="http://www.w3.org/2000/svg">
    <use xlink:href="assets/icons.svg#back-button"></use>
  </svg>
  <span> Lorem ipsom </span>
  <!-- 只有这个 span 的字体颜色应该是红色 -->
  
  <span>
    <span> </span>
  </span>
</span>

CSS:

span:nth-child(2) {
  color: red;
  font-size: 12px;
  font-weight: bold;
}
英文:

Because the .container is also a span, the declarations overlap each other.


To do this, you just need to target the nth child, like span:nth-child(2) or whichever span you want by nth-child('child No.'). Here is the solution:


HTML:

&lt;span class=&quot;content&quot;&gt;
       &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
       &lt;use xlink:href=&quot;assets/icons.svg#back-button&quot;&gt;&lt;/use&gt;
    &lt;/svg&gt;
     &lt;span&gt; Lorem ipsom &lt;/span&gt;
&lt;!-- only this span font color should be red --&gt;

&lt;span&gt;
       &lt;span&gt; &lt;/span&gt;
&lt;/span&gt;
&lt;/span&gt;

CSS:

span:nth-child(2) {
        color: red;
        font-size: 12px;
        font-weight: bold;
      }

huangapple
  • 本文由 发表于 2023年2月8日 22:01:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定