简体   繁体   English

::selection 伪选择器忽略 ::before 伪元素

[英]::selection pseudo selector ignoring ::before pseudo element

I am using Clipboard.js to copy a .container 's contents to the user's clipboard.我正在使用 Clipboard.js 将.container的内容复制到用户的剪贴板。 I have styled the ::selection / ::-moz-selection of this .container and it's child elements as all child elements are .select() ed during the copy to clipboard process.我已经设计了这个.container::selection / ::-moz-selection和它的子元素,因为在复制到剪贴板的过程中所有子元素都是.select() ed。

It works great except for on ::before (and presumably ::after ) pseudo elements.除了在::before (大概是::after )伪元素之外,它工作得很好。 ::before more or less ignores my css declarations. ::before或多或少忽略了我的 css 声明。 I am using the counter css property in ::before as content .我在::before使用counter css 属性作为content

Here is a snippet exposing the issue.这是一个暴露问题的片段。 I have not included any JS as it is not necessary to expose the issue.我没有包含任何 JS,因为没有必要公开这个问题。 Click and drag on the code block and you will see that no content is highlighted on ::selection except for the ::before pseudo element.单击并拖动代码块,您将看到::selection除了::before伪元素之外没有突出显示任何内容。

Can anyone show me how to override ::selection of ::before pseudo element so that it is not visible?谁能告诉我如何覆盖::selection of ::before伪元素使其不可见?

Edit: This seems to be a Safari/Chrome (presumably -webkit- issue).编辑:这似乎是 Safari/Chrome(大概是-webkit-问题)。 Not happening in Firefox after doing a bit of isolation testing.在进行了一些隔离测试后,Firefox 中没有发生。

::在被选中之前

 .html.container { position: relative; display: block; padding: .9375rem .9375rem .9375rem 2.5rem; margin-bottom: 20px; background: rgba(38, 38, 38, .08); counter-reset: lines; } .html.container::before { content: ''; position: absolute; top: 0; left: 0; width: 5px; height: 100%; background: grey; } .html.syntax { display: block; border-left: .0625rem solid black; } .html.syntax *::selection { background: transparent; color: inherit; } .html.syntax *::-moz-selection { background: transparent; color: inherit; } .html.line::before { content: counter(lines); position: absolute; left: 5px; width: 25px; color: grey; text-align: right; transition: all .25s ease; } .html.line { display: block; padding-left: 15; counter-increment: lines; } .html.line::before::selection { background: transparent; color: inherit; } .html.syntax::before::-moz-selection { background: transparent; color: inherit; }
 <div class="js html container" data-clipboard-target="#\\<h1\\>"> <code class="html syntax" id="<h1>"> <span class="html line"> <span class="html comment">&lt;!-- Heading level 1 --&gt;</span> </span> <span class="html line"> &lt;<span class="html tag">h1</span>&gt;Heading level 1&lt;<span class="html tag">/h1</span>&gt; </span> </code> </div>

You can use a data attribute on the "html line" classed span, this prevents the number from appearing in the selection in Chrome.您可以在“html 行”分类跨度上使用数据属性,这可以防止数字出现在 Chrome 的选择中。 The downside to this is you'll lose the CSS counter to auto-increment the line numbers:这样做的缺点是您将丢失 CSS 计数器来自动增加行号:

<div class="js html container" data-clipboard-target="#\<h1\>">
    <code class="html syntax" id="<h1>">
        <span class="html line" data-pseudo-content="1">
            <span class="html comment">&lt;!-- Heading level 1 --&gt;</span>
        </span>
        <span class="html line" data-pseudo-content="2">
            &lt;<span class="html tag">h1</span>&gt;Heading level 1&lt;<span class="html tag">/h1</span>&gt;
        </span>
    </code>
</div>

https://jsfiddle.net/ohyj81c4/ https://jsfiddle.net/ohyj81c4/

在 Chrome 上选择了数据属性的行

ref https://danoc.me/blog/css-prevent-copy/参考https://danoc.me/blog/css-prevent-copy/

The reason you can't change the pseudo element selection colour is because you can only use 1 pseudo-element in a selector.您不能更改伪元素选择颜色的原因是因为您只能在选择器中使用 1 个伪元素。 Both ::selection and ::before come under this definition, rather than ::selection being apseudo-class like :active, :visited etc. ::selection 和 ::before 都属于这个定义,而不是 ::selection 是像 :active, :visited 等的伪类

ref: https://developer.mozilla.org/en/docs/Web/CSS/Pseudo-elements参考: https : //developer.mozilla.org/en/docs/Web/CSS/Pseudo-elements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM