当使用内联& quot;宽度& amp; quot;时,省略像素. [英] Omitting pixel when using inline "width"

查看:68
本文介绍了当使用内联& quot;宽度& amp; quot;时,省略像素.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个愚蠢的问题,但对我来说很重要.据我所知,在HTML中使用内联宽度"属性时,允许省略"px"-除非使用percent("20%"),否则将自动理解为"20px".我的问题是:即使不需要使用"..px",这是否有误?该代码对我来说似乎干净得多,它遵循与CSS相同的规则,但至少没有那么持久-每次查看它都不会使我烦恼.预先感谢.

A bit of a silly question but important for me to understand. As far as I know when using the inline "width" attribute in HTML, it is permitted to omit "px" - - will automatically be understood as "20px" unless percentage("20%") is used. My question is: Is it wrong to use the "..px" even though it's not needed? The code seem so much cleaner to me, it follows the same rule as CSS and least but not last - it doesn't bug me anytime I look at it. Thanks in advance.

推荐答案

在HTML 4或更早的规范中从未对此进行过全面阐述,但是所有支持 width 和相关表示属性的HTML DTD都不会这样做.不会对%Pixels 值施加任何限制-它们只是简单地指出它们应该是整数,但在DTD中定义为CDATA:

This is never stated outright in the HTML 4 or older specs, but all HTML DTDs that support width and related presentational attributes don't impose any restrictions on %Pixels values — they simply state in prose that they should be integers, but are defined in the DTD as CDATA:

<!ENTITY % Length "CDATA" -- nn for pixels or nn% for percentage length -->
<!ENTITY % Pixels "CDATA" -- integer representing length in pixels -->

所以,从技术上讲,这不是错误,实际上您可以放置​​任何想要的东西,并且

So, technically, it's not wrong, in fact you could put anything you want and

  1. 它仍然可以针对HTML 4 DOCTYPE进行验证;和
  2. 浏览器只会将属性值解析为整数或百分比.

以下所有功能在功能上都是等效的,生成的表宽为200 CSS像素(因为没有一个值可以解析为百分比):

All of the following are functionally equivalent, producing tables that are 200 CSS pixels wide (because none of the values can be parsed as a percentage):

<table border="1" width="200"><tr><td><code>width="200"</code></table>
<table border="1" width="200px"><tr><td><code>width="200px"</code></table>
<table border="1" width="200abcd"><tr><td><code>width="200abcd"</code></table>
<table border="1" width="200x10px"><tr><td><code>width="200x10px"</code></table>

这篇关于当使用内联&amp; quot;宽度&amp; amp; quot;时,省略像素.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆