:first

:first @page CSS伪类描述打印文档时,第一页的样式。

/* Selects the content on the first page while printing */ @page :first { margin-left: 50%; margin-top: 50%; }

注意:你不能用所有的CSS属性:first。您只能更改文档的边距,orphans, widows 和分页符。所有其他的CSS属性将被忽略。

语法

:first

HTML

<p>First Page.</p> <p>Second Page.</p> <button>Print!</button>

CSS

@page :first { margin-left: 50%; margin-top: 50%; } p { page-break-after: always; }

JavaScript

document.querySelector("button").onclick = function(){ window.print( }

结果

按“ "Print!" 按钮打印示例。第一页上的单词应该位于中间的某个位置,而其他页面的内容则位于默认位置。

注:在定义边距时,只能使用绝对长度单位。请看关于长度想了解更多信息。

规范

SpecificationStatusComment
CSS Paged Media Module Level 3The definition of ':first' in that specification.Working DraftNo change
CSS Level 2 (Revision 1)The definition of ':first' in that specification.RecommendationInitial definition

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support?(Yes)No support8.09.2?

FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support?(Yes)No support???