【问题标题】:::selection pseudo selector ignoring ::before pseudo element::selection 伪选择器忽略 ::before 伪元素
【发布时间】:2017-06-07 23:05:11
【问题描述】:

我正在使用 Clipboard.js 将 .container 的内容复制到用户的剪贴板。我已经为这个.container 设置了::selection / ::-moz-selection 的样式,它是子元素,因为在复制到剪贴板的过程中,所有子元素都是.select()ed。

除了::before(可能是::after)伪元素外,它的效果很好。 ::before 或多或少忽略了我的 css 声明。我将::before 中的counter css 属性用作content

这是一个暴露问题的 sn-p。我没有包含任何 JS,因为没有必要公开这个问题。单击并拖动代码块,您将看到::selection 上除了::before 伪元素外没有突出显示任何内容。

谁能告诉我如何覆盖::before 伪元素的::selection 使其不可见?

编辑:这似乎是 Safari/Chrome(大概是 -webkit- 问题)。经过一些隔离测试后,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>

【问题讨论】:

    标签: html css css-selectors cross-browser pseudo-element


    【解决方案1】:

    您可以在“html line”分类跨度上使用数据属性,这可以防止数字出现在 Chrome 的选择中。这样做的缺点是你会失去 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://danoc.me/blog/css-prevent-copy/

    您不能更改伪元素选择颜色的原因是您只能在选择器中使用 1 个伪元素。 ::selection 和 ::before 都属于此定义,而不是 ::selection 是 pseudo-class,如 :active、:visited 等。

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

    【讨论】:

    • 感谢@jonnow。从功能上讲,这完全符合我的要求,::before 内容不会复制到剪贴板。这是伟大的。但出于 UX/设计/美学的目的,我想删除 ::before 伪元素的突出显示。感谢您提醒我,您只能在选择器中使用一个伪元素。我本来以为他们不会互相忽视。
    • 谢谢。这看起来确实是一个可行的解决方案。在您的示例中将data-pseudo-content="4" 应用于span.html.tag 的原因是什么?我不遵循解决方案的那一部分。
    • 我打错了 :D
    • 这显然会使事情变得更加手动。但它解决了我的问题。谢谢。现在阅读自定义数据属性...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2012-05-31
    • 2015-02-08
    • 1970-01-01
    • 2015-06-27
    • 2014-05-03
    • 2017-10-06
    相关资源
    最近更新 更多