admin管理员组文章数量:1421245
I am trying to control the size/orientation when printing a ponent using styled-ponents. My question is: how can I use the @page CSS at-rule, or another method to style the printing ponent, with styled ponents?
CSS @page documentation:
/@page
I have tried:
const PrintSchedulesContainer = styled.div`
display: none;
@media print and (min-width: 480px) {
padding: none;
margin: none;
}
`;
And:
const PrintSchedulesContainer = styled.div`
display: none;
@page {
size: landscape;
}
`;
I am trying to control the size/orientation when printing a ponent using styled-ponents. My question is: how can I use the @page CSS at-rule, or another method to style the printing ponent, with styled ponents?
CSS @page documentation:
https://developer.mozilla/en-US/docs/Web/CSS/@page
I have tried:
const PrintSchedulesContainer = styled.div`
display: none;
@media print and (min-width: 480px) {
padding: none;
margin: none;
}
`;
And:
const PrintSchedulesContainer = styled.div`
display: none;
@page {
size: landscape;
}
`;
Share
Improve this question
edited Aug 14, 2019 at 20:22
Emile Bergeron
17.4k5 gold badges85 silver badges132 bronze badges
asked Aug 14, 2019 at 20:11
jonniejonnie
7651 gold badge13 silver badges23 bronze badges
1
- 1 change the title to a property one that describes your question – Lucas Vazquez Commented Aug 14, 2019 at 20:17
1 Answer
Reset to default 4You can't target a single ponent to print.
You need to hide the other elements in order your ponent to be the only one printed.
@page works only for changing printing rules.
@media print let you define other class styles as well just like @media screen.
You can use @media print inside a wrapper styled ponent, making it fullscreen, fixed with a white background.
Example:
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>
);
}
To change print rules you need to add the @page into a global style and render the global style ponent.
import styled, { createGlobalStyle } from "styled-ponents";
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>
);
}
本文标签: javascripthow to use styled components to style the printing of a componentStack Overflow
版权声明:本文标题:javascript - how to use styled components to style the printing of a component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745343843a2654388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论