Applying css to the remaining of an element

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

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 &lt;p&gt; 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: &#39;:&#39;;
  float: right;
}

p {
  color: black;
}

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

&lt;p&gt;:Welcome&lt;/p&gt;

<!-- end snippet -->

答案1

得分: 2

根据文档:

> 在第一个字母之前或之后紧跟的标点符号包括在匹配中。标点符号包括在 open(Ps)、close(Pe)、initial quote(Pi)、final quote(Pf) 和 other punctuation(Po)类别中定义的任何 Unicode 字符。
>
> &ndash; ::first-letter | MDN Web Docs

这意味着仅选择冒号是不可能的。但是,您可以使用伪元素创建一个在实际内容之前的不可访问的冒号:

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

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

p::before {
  content: &#39;:&#39;;
  color: black;
}

p {
  color: red;
}

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

&lt;p&gt;欢迎&lt;/p&gt;

<!-- end snippet -->

...或者将冒号包装在元素内:

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

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

.colon {
  color: black;
}

p {
  color: red;
}

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

&lt;p&gt;&lt;span class=&quot;colon&quot;&gt;:&lt;/span&gt;欢迎&lt;/p&gt;

<!-- 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.
>
> &ndash; ::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: &#39;:&#39;;
  color: black;
}

p {
  color: red;
}

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

&lt;p&gt;Welcome&lt;/p&gt;

<!-- 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 -->

&lt;p&gt;&lt;span class=&quot;colon&quot;&gt;:&lt;/span&gt;Welcome&lt;/p&gt;

<!-- end snippet -->

答案2

得分: 1

如果你没有其他选择,不幸的是你必须按照我们在开始时采用的方案行事,即 &lt;p&gt;:Welcome&lt;/p&gt;,那么你有几个选项,我想不出其他更一致的选项。

我的概念有许多缺点,即它会造成:

  • 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 -->

&lt;p&gt;:Welcome&lt;/p&gt;

<!-- end snippet -->

如果你有更好的消除不需要的文本以及使文本选择恢复正常的想法,请写一条评论。

英文:

If you have no other choice, and unfortunately you have to act on the scheme we adopted at the beginning, i.e. &lt;p&gt;:Welcome&lt;/p&gt; 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 &quot;Welcome&quot; after the content of the paragraph element */
  content: &quot;Welcome&quot;;
  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 &quot;:&quot; before the content of the paragraph element */
  content: &quot;:&quot;;
  letter-spacing: 0px; /* sets the letter-spacing to 0px, removing the overlap of letters*/
}

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

&lt;p&gt;:Welcome&lt;/p&gt;

<!-- end snippet -->

If you have a better idea for eliminating unwanted text, as well as getting text selection back to work - write a comment.

huangapple
  • 本文由 发表于 2023年6月9日 03:08:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435001.html
匿名

发表评论

匿名网友

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

确定