2017-10-10 61 views
0

我正在本地工作一個WordPress網站,我正在嘗試動態調用自定義頁眉。我使用下面的代碼:W3C驗證錯誤:百分位高度和寬度屬性

<img src="<?php header_image(); ?>" height="20%<?php echo get_custom_header()->height; ?>" width="20%<?php echo get_custom_header()->width; ?>" alt="header-image" /> 

上面的代碼,將輸出以下行到瀏覽器:

<img src="http://localhost/wordpress-folder/wp-content/uploads/2017/10/image.jpg" height="20%3484" width="20%2439" alt="header-image" /> 

雖然上面的代碼調用成功的自定義頁眉,它並不能W3C驗證。該錯誤消息是如下:

Bad value 20%3484 for attribute height on element img: Expected a digit but saw % instead.

我似乎可以消除這種誤差的唯一方法,是通過去除%(PX也產生錯誤),並且僅在數離開。

有沒有一種方法可以繼續使用像素/百分比,而不是重新組織我的代碼,以便我可以實現一些內聯/外部樣式表?

+1

高度和寬度屬性被假定爲像素。刪除除數字以外的任何內容或使用style屬性。 'style =「width:100px; height100;」' – bassxzero

+0

https://www.w3schools.com/tags/tag_img.asp – bassxzero

+0

@bassxzero謝謝。我最初嘗試了這種方法,並在工作的同時,將圖像放大以覆蓋整個屏幕;即使當px設置爲1.我注意到如果我刪除了'<?php echo get_custom_header() - > height;那麼問題就解決了。謝謝。另外,感謝您分享鏈接。 – Craig

回答

1

您正在使用HTML高度和寬度屬性。當你向他們傳遞值時,你不能將度量(例如:%,px等)傳遞給它。

你將有你的行更改爲:

<img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="header-image" /> 

希望這有助於。 :)

+0

謝謝。我完全忽略了這個事實:'<?php echo get_custom_header() - > height; ?>動態調用相關屬性。因此解釋了錯誤,因爲我手動輸入了屬性值。 – Craig

0

這應該拋出一個錯誤,因爲它的格式不正確。它應該是%格式或px格式。 20%3484是不正確的格式。

如果你想給固定的高度,您可以使用此:

<img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="header-image" /> 

,或者如果你想使用%,然後使用此:

<img src="<?php header_image(); ?>" height="20%" width="20%" alt="header-image" /> 

但是,你只能使用一個他們。

讓我知道它是否有幫助。

+0

由於%,您的第二個建議仍會引發驗證錯誤。你的第一個建議很有效,但我們不需要額外的'px':-)謝謝你的時間。 – Craig

+0

沒問題。我想我們只需要刪除px。 –