How can I use counter-increment to show page numbers on a printed web page?

穿精又带淫゛_ 提交于 2019-12-23 07:57:46

问题


I have the following CSS:

@media print {
    div.question-list-footer
    {
        position: fixed;
        bottom: 0;
        padding-left: 180px;
    }
    div.question-list-footer-center 
    {
        text-align: center;
    }
    @page { counter-reset: page 1}
}

#pageNumber:after { content: counter(page); }
#pageNumber { counter-increment: page; }

and the following html on my page:

<div class="question-list-footer">
    <div class="question-list-footer-center">
        <span>Page Number: <span id="pageNumber"></span></span><br/>
        Date: @firstItem.Date.Value.ToShortDateString()
        Id: @firstItem.Id
    </div>
</div>

and this works when printing except that all pages have "Page Number 1". (IE9, Chrome & FF) I have been staring at this and playing with it for ages and still can't see why. Does anyone have a fix? - Please tell me it's not obvious. (FWIW - Chrome doesn't like my bottom).


回答1:


I think this line:

@page { counter-reset: page 1}

Means:

“On each printed page, reset the page counter to 1.”

See http://www.w3.org/TR/CSS21/generate.html#propdef-counter-reset

Hence every page would have page number 1, as you’re resetting it to 1 on each page.

Does it work if you do body { counter-reset: page 1} instead? (Like in the example from the spec.)




回答2:


EDIT

I think if you just remove this, the counter won't be reset so it will just increment like you want

@page { counter-reset: page 1}


来源:https://stackoverflow.com/questions/8985453/how-can-i-use-counter-increment-to-show-page-numbers-on-a-printed-web-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!