gpt4 book ai didi

html - CSS 选择要打印的内容(媒体查询)

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:35 25 4
gpt4 key购买 nike

我正在尝试构建打印媒体查询以仅打印屏幕的特定内容:

<div>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div class='printed-content'>
<p>
PRINTED CONTENT
</p>
</div>

CSS:

body {
background-color: white;
color: red;
}

@media print {
body {
display: none;
}
.printed-content {
display: block;
}
}

当我打印时,打印的内容现在正在打印(全部为空)。我确定我遗漏了一些简单的内容,但我无法找出是什么。

JSFiddle here

最佳答案

要隐藏所有与您的选择器不匹配的元素,您可以使用 :not() 伪类。

正如@Dekel 指出的那样,您需要适本地确定范围,例如:

@media print {
body .wrapper *:not(.printed-content) {
display: none;
}
body .wrapper .printed-content * {
display: block;
}
}

关于html - CSS 选择要打印的内容(媒体查询),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45928816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com