在使用样式组件打印组件时,我试图控制其大小/方向。我的问题是:如何使用@page CSS at-rule,或者使用另一种方法对打印组件进行样式化,使用样式化的组件?
CSS @page文档:
https://developer.mozilla.org/en-US/docs/Web/CSS/@page
我试过:
const PrintSchedulesContainer = styled.div`
display: none;
@media print and (min-width: 480px) {
padding: none;
margin: none;
}
`;和:
const PrintSchedulesContainer = styled.div`
display: none;
@page {
size: landscape;
}
`;发布于 2019-08-14 21:15:45
您不能针对要打印的单个组件。
您需要隐藏其他元素,以便您的组件成为唯一打印的元素。
@page只适用于更改打印规则。
@媒体打印还允许定义其他类样式,就像@media屏幕一样。
您可以在包装器样式的组件中使用@media print,使其成为全屏,并带有白色背景。
示例:
const PrintableBodyWrapper = styled.div`
@media print {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: white;
display: flex;
justify-content: center;
align-items: center;
}
`;
function App() {
return (
<div className="App">
<PrintableBodyWrapper>
<div style={{ width: 100, height: 100, background: "grey" }}>
I will be printed
</div>
</PrintableBodyWrapper>
</div>
);
}要更改打印规则,需要将@页面添加到全球风格中并呈现全局样式组件。
import styled, { createGlobalStyle } from "styled-components";
const GlobalStyle = createGlobalStyle`
@page {
size: landscape;
margin: 5cm;
`;
const PrintableBodyWrapper = styled.div`
@media print {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: black;
display: flex;
justify-content: center;
align-items: center;
}
`;
function App() {
return (
<div className="App">
<GlobalStyle />
<PrintableBodyWrapper>
<div style={{ width: 100, height: 100, background: "grey" }}>
I will be printed
</div>
</PrintableBodyWrapper>
</div>
);
}https://stackoverflow.com/questions/57501406
复制相似问题