英文:
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>span:first-child{
color: red;
font-size: 12px;
font-weight: bold;
}
<!-- language: lang-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>
<!-- only this span font color should be red -->
<span>
<span> </span>
</span>
</span>
<!-- 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 > span:first-of-type {
color: red;
}
<!-- language: lang-html -->
<span class="content">
<svg viewBox="0 0 10 2" xmlns="http://www.w3.org/2000/svg">
<line x1="0" y1="0" x2="10" y2="0" stroke="black" />
</svg>
<span> Lorem ipsom </span>
<!-- 只有这个 span 的字体颜色应该是红色 -->
<span>
Child
<span> Inner child </span>
</span>
</span>
<!-- 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 -->
<span class="content">
<svg viewBox="0 0 10 2" xmlns="http://www.w3.org/2000/svg">
<line x1="0" y1="0" x2="10" y2="0" stroke="black" />
</svg>
<span> Lorem ipsom </span>
<!-- 只有这个 span 的字体颜色应该是红色 -->
<span>
Child
<span> Inner child </span>
</span>
</span>
<!-- 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 > span:first-of-type {
color: red;
}
<!-- language: lang-html -->
<span class="content">
<svg viewBox="0 0 10 2" xmlns="http://www.w3.org/2000/svg">
<line x1="0" y1="0" x2="10" y2="0" stroke="black" />
</svg>
<span> Lorem ipsom </span>
<!-- only this span font color should be red -->
<span>
Child
<span> Inner child </span>
</span>
</span>
<!-- 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 -->
<span class="content">
<svg viewBox="0 0 10 2" xmlns="http://www.w3.org/2000/svg">
<line x1="0" y1="0" x2="10" y2="0" stroke="black" />
</svg>
<span> Lorem ipsom </span>
<!-- only this span font color should be red -->
<span>
Child
<span> Inner child </span>
</span>
</span>
<!-- end snippet -->
答案2
得分: 1
你可以使用 first-of-type
,像这样:
.content > span:first-of-type {
color: red;
}
英文:
You can use first-of-type so something like:
.content > span:first-of-type {
color:red;
}
答案3
得分: 1
问题是 :first-child
选择器仅在你寻找的 span
是 .content
的第一个子元素时才适用。
在你的情况下,第一个子元素是一个 svg
标签,这就是为什么它不起作用的原因。
有三种方法可以解决这个问题:
-
将选择器改为
.content > span:nth-child(2)
。这在上面的例子中可以工作,但如果你将其他元素放在问题的 span 和你的 svg 之间,它将再次中断。 -
.content > span:first-of-type
。这似乎是你想要的,即.content
的第一个子元素是第一个 span。 -
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:
-
Make the selector
.content > 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. -
.content > span:first-of-type
. This seems to be what you're after, i.e. the first span that's the child of.content
. -
span.some-meaningful-classname
- This gives you the flexibility to choose exactly which spans to apply the class to by addingclassname="some-meaningful-classname"
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
添加类来实现,如下所示:
原始:
<span> Lorem ipsom </span>
新的:
<span class="firstspan"> Lorem ipsom </span>
(编辑:抱歉,忘记将<span>
标记为代码,所以它没有正确显示)
接下来,你可以在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:
<span> Lorem ipsom </span>
New:
<span class="firstspan"> Lorem ipsom </span>
(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:
<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>
CSS:
span:nth-child(2) {
color: red;
font-size: 12px;
font-weight: bold;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论