英文:
Ignoring first character, applying css to the remaining of an element
问题
Sure, here is the translated content:
有没有办法忽略位于<p>
中的第一个字符,仅对其余部分应用内联样式?
即::Welcome
我想将单词Welcome变为红色,但我想忽略冒号并保持它为黑色。
这可能吗?
我尝试过这个,但它会使冒号(:
)和W都变成红色。
p::first-letter {
color: red;
}
p::after {
content: ':';
float: right;
}
p {
color: black;
}
<p>:Welcome</p>
英文:
Is there a way to ignore the first character that resides in <p>
and only apply inline styling to the remaining?
i.e. : Welcome
I want to make the word Welcome Red but the colon I want to ignore and leave it black
Is this possible?
I tried this but it makes the :
and W
Red.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
p::first-letter {
color: red;
}
p::after {
content: ':';
float: right;
}
p {
color: black;
}
<!-- language: lang-html -->
<p>:Welcome</p>
<!-- end snippet -->
答案1
得分: 2
根据文档:
> 在第一个字母之前或之后紧跟的标点符号包括在匹配中。标点符号包括在 open(Ps)、close(Pe)、initial quote(Pi)、final quote(Pf) 和 other punctuation(Po)类别中定义的任何 Unicode 字符。
>
> – ::first-letter
| MDN Web Docs
这意味着仅选择冒号是不可能的。但是,您可以使用伪元素创建一个在实际内容之前的不可访问的冒号:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
p::before {
content: ':';
color: black;
}
p {
color: red;
}
<!-- language: lang-html -->
<p>欢迎</p>
<!-- end snippet -->
...或者将冒号包装在元素内:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
.colon {
color: black;
}
p {
color: red;
}
<!-- language: lang-html -->
<p><span class="colon">:</span>欢迎</p>
<!-- end snippet -->
英文:
According to the docs:
> Punctuation that precedes or immediately follows the first letter is
> included in the match. Punctuation includes any Unicode character
> defined in the open (Ps), close (Pe), initial quote (Pi), final
> quote (Pf), and other punctuation (Po) classes.
>
> – ::first-letter
| MDN Web Docs
This means selecting only the colon is not possible. You can, however, use a pseudo-element to create a inaccessible colon that precedes your actual content:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
p::before {
content: ':';
color: black;
}
p {
color: red;
}
<!-- language: lang-html -->
<p>Welcome</p>
<!-- end snippet -->
...or wrap the colon inside an element:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
.colon {
color: black;
}
p {
color: red;
}
<!-- language: lang-html -->
<p><span class="colon">:</span>Welcome</p>
<!-- end snippet -->
答案2
得分: 1
如果你没有其他选择,不幸的是你必须按照我们在开始时采用的方案行事,即 <p>:Welcome</p>
,那么你有几个选项,我想不出其他更一致的选项。
我的概念有许多缺点,即它会造成:
- SEO可见性困难,不同的网络爬虫无法处理类似这样的东西。
- 不允许你选择文本,你只是不能选择它。
然而,它能正常显示文本。
这个CSS代码对段落元素应用样式,还添加了伪元素 ::after、::first-letter 和 ::before 并附加额外样式。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
p {
font-weight: bold; /* 设置字体加粗 */
font-size: 2em; /* 增加字体大小至2em */
color: rgba(0, 0, 0, 0.0); /* 使用rgba将颜色设置为透明黑色 */
letter-spacing: -1em; /* 设置字母间距为-1em,有效叠加字母 */
}
p::after { /* 在段落元素内容后添加文本“Welcome” */
content: "Welcome";
color: black; /* 将文本颜色设置为黑色 */
letter-spacing: 0px; /* 将字母间距设置为0px,取消字母叠加效果*/
}
p::first-letter { /* 样式化段落元素内容的第一个字母 */
color: red; /* 将第一个字母的颜色设为红色 */
letter-spacing: 0px; /* 将字母间距设置为0px,取消字母叠加效果*/
}
p::before { /* 在段落元素内容前添加文本“:” */
content: ":";
letter-spacing: 0px; /* 将字母间距设置为0px,取消字母叠加效果*/
}
<!-- language: lang-html -->
<p>:Welcome</p>
<!-- end snippet -->
如果你有更好的消除不需要的文本以及使文本选择恢复正常的想法,请写一条评论。
英文:
If you have no other choice, and unfortunately you have to act on the scheme we adopted at the beginning, i.e. <p>:Welcome</p>
then you have several options for this, I couldn't come up with another that would be more consistent.
There are many disadvantages with my concept, that is, it makes:
- SEO visibility difficult, different crawlers will not be able to cope
with something like this. - Doesn't allow you to select text, you just can't select it.
However, it works and displays the text correctly.
This CSS code applies styling to a paragraph element, as well as adds a pseudo-element ::after, ::first-letter and ::before with additional styling.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
p {
font-weight: bold; /* makes the font bold */
font-size: 2em; /* increases the font size to 2em */
color: rgba(0, 0, 0, 0.0); /* sets the color to a transparent black using rgba */
letter-spacing: -1em; /* sets the spacing between letters to -1em, effectively overlapping the letters */
}
p::after { /* adds text "Welcome" after the content of the paragraph element */
content: "Welcome";
color: black; /* sets the color of the text to blue */
letter-spacing: 0px; /* sets the letter-spacing to 0px, removing the overlap of letters*/
}
p::first-letter { /* styles the first letter of the content of the paragraph element */
color: red; /* sets the color of the first letter to red */
letter-spacing: 0px; /* sets the letter-spacing to 0px, removing the overlap of letters*/
}
p::before { /* adds text ":" before the content of the paragraph element */
content: ":";
letter-spacing: 0px; /* sets the letter-spacing to 0px, removing the overlap of letters*/
}
<!-- language: lang-html -->
<p>:Welcome</p>
<!-- end snippet -->
If you have a better idea for eliminating unwanted text, as well as getting text selection back to work - write a comment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论