[英]Change css properties, like color and background, for lines starting with “#” within <pre>
I am trying to add a minimal code syntax highlighting rule to a web page by only changing the color of comments - lines starting with "#" symbol within the <pre><code>
tags.我试图通过仅更改注释的颜色来向 web 页面添加最小代码语法突出显示规则 - <pre><code>
标记中以“#”符号开头的行。
To my surprise I could not find information about this by searching on StackOverflow.令我惊讶的是,我无法通过在 StackOverflow 上搜索找到有关此的信息。 However projects such as ft-syntax-highlight make me assume this to be possible.然而,诸如ft-syntax-highlight 之类的项目让我认为这是可能的。
Is it possible to adjust the color of lines starting with "#" in the following code block using only css?:是否可以仅使用 css 调整以下代码块中以“#”开头的行的颜色?
<pre><code>
# function to do something
do_something_here(x1, x2);
</code></pre>
And if so - how?如果是这样 - 如何?
Probably not what you're looking for but a minimal workaround would be using CSS's content
and :before
可能不是您要找的东西,但最小的解决方法是使用 CSS 的content
和:before
View in jsfiddle在 jsfiddle 中查看
[comment]:before {
content: "# " attr(comment);
color: red;
white-space: pre-wrap;
width: 45vw;
display: inline-block;
}
<pre>
<code comment="function to do something here line function to do something here line function to do something here line">
do_something_here(x1, x2);
</code>
<code comment="you can use `missing()` to test whether or not the argument y was supplied">
fooBar <- function(x,y){
if(missing(y)) {
x
} else {
x + y
}
}
</code>
</pre>
Note: use data-comment
instead of comment
for better html semantics注意:使用data-comment
而不是comment
以获得更好的 html 语义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.