2012-08-12 82 views

回答

0

其實辛西婭,如果你打算做這樣的事情,我會說什麼會最有意義的將使用print-header.gif圖像作爲兩個圖像的來源。

這種方法被稱爲CSS 精靈。您可以使用CSS定位來只能說明你在.header-left.header-right爲屏幕所需的圖像的部分。要進行打印,您可以展開.header-left以顯示整個圖像並隱藏標頭右側。這意味着你已經在頁面上少了一個<img>並削減HTTP請求(一個用於圖像對三)。

通常情況下,這種方法只能用於背景圖像(這會使您回到步驟1!),但可以使用clip屬性在前景圖像中使用此方法。你的CSS將是沿着線:

screen.css:

div#header { 
    position: relative; 
} 

img.header-left { 
    position: absolute; 
    clip:rect(top, right, bottom, left); 
} 

img.header-right { 
    position: absolute; 
    clip:rect(top, right, bottom, left); 
    top: offset; 
    left: offset; 
} 

print.css:

img.header-left { 
    position: static; 
    clip: auto; 
} 

img.header-right { 
    display: none; 
} 

你的HTML便可設爲

<div id="header"> 
    <img src="/images/print-header.gif" class="header-left" /> 
    <img src="/images/print-header.gif" class="header-right" /> 
</div> 
+0

我同意,這將很好地工作,如果頭(從瀏覽器端)的兩個部分只是圖像。在這種情況下,標題左圖像只是圖像,而標題右圖像是標題內容塊(日期選擇器)的背景圖像。由於打印時不需要日期選擇器,所以單一替代標題圖像就符合該法案。 – Cynthia 2012-08-12 16:27:17

+0

@Cynthia,我只是用你下面提供的HTML,其中兩個頭項目是圖像去......如果不是的話,你真的需要在你的問題給予更多的細節;) – anotherdave 2012-08-12 16:37:51

0

您可以使用media-query這樣的:

#header { width: 100%; height: 100px; } 

@media screen 
{ 
    #header > #left { background: url("display1.png"); } 
    #header > #right { background: url("display2.png"); } 
} 

@media print 
{ 
    #header > #left { background: url("print.png"); } 
    #header > #right { display: none; } 
} 

,然後插入到HTML:

<div id="header"> 
    <div id="left"></div> 
    <div id="right"></div> 
</div> 
+0

除瀏覽器端*上的標題*有*爲2個獨立的圖像,而不是一個。所以爲div分配背景圖片並不會削減它。 – Cynthia 2012-08-12 15:30:59

+0

@Cynthia我更新了答案。它看起來不好嗎? :) – 2012-08-12 15:39:20

0

瀏覽器現在已經是經濟,默認情況下,他們沒有打印任何背景色了。

如Firefox瀏覽器的一些讓您管理,但每個用戶設置那是一個。

0

你可以使用media="print"

<style type="text/css"> 
#img{background-image:url('img1.jpg');} 
</style> 
<style type="text/css" media="print"> 
#img{background-image:url('img2.jpg');} 
</style> 

但是,如果問題是,瀏覽器不打印背景顏色和你使用Firefox,你可以這樣做:

文件>頁面設置... >格式&選項>選項>打印背景(顏色&圖像)

網絡連接refox按鈕>打印...>頁面設置...>格式&選項>選項>打印背景(顏色&圖像)

0

我找到了解決方案!

我創建了第二個標題圖像,它將兩個原始圖像(加上一點填充到 使它看起來不錯)僅用於打印。

這裏是我得到的圖像,以示對打印,但在其他任何時候是不可見的:

我創建了一個類調用.print-header,在我的主要的style.css我把它就像這樣:

.print-header { display:none } 

在我的print.css我把它就像這樣:

.print-header { display:block !important } 
.header-left { display:none } /* this hides the original left header image when printing */ 
.header-right { display:none } /* this hides the original right header image when printing */ 

然後改變了我的HTML:

<div id="header"> 
    <img src="/images/logo.gif" class="header-left" /> 
    <img src="/images/cta.gif" class="header-right" /> 
    <img src="/images/print-header.gif" class="print-header" /> 
</div> 

,瞧!在瀏覽器中,顯示原始2圖像標題,並在打印時顯示單個圖像標題。

+0

對不起,但這很醜。 :) – 2012-08-12 15:28:01

+0

它可能不漂亮,但它是有效的標記,它可以在所有的板子上工作。出於內容原因,瀏覽器端的標題必須是兩個單獨的部分(標題左和標題右)。它不需要用於打印視圖。因此,備用打印頭是最有意義的。 – Cynthia 2012-08-12 15:33:31