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

时间:2017-08-28 23:54:40

标签: html css

我正在尝试构建一个打印媒体查询,只打印屏幕的特定内容:

<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

1 个答案:

答案 0 :(得分:-1)

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

正如@Dekel所说,你想要适当地确定这个范围,例如:

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