使无 DOCTYPE 的 HTML 页面符合 W3C 标准
如何获取当前没有文档类型声明的 HTML 网站并使其符合 W3C?
How do i take a HTML site that currently has no doctype declaration and make it W3C compliant?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先:考虑为什么您需要使其符合 W3C。 如果只是为了一个小徽章,那就不值得了。
如果您确实有充分的理由(而且有很多),那么我会这样做:
从选择文档类型开始。 从表面上看,HTML 4.0 可能是正确的选择。 通过验证器运行您的页面,然后查看它为您提供的错误报告。 一项一项地检查,修正每个错误。 通常,错误报告会告诉您有关如何解决每个问题的大量信息,并为您提供规范的相关链接。
First of all: think about why you need to make it W3C compliant. If it's just for the little badge, then it's not worth it.
If you do have a good reason (and there are plenty), then this is what I'd do:
Start by choosing a doctype. From the sound of it, HTML 4.0 might be the way to go. Run your page through a validator and then look at the error report it gives you. Go through it one by one, fixing each error. Usually the error report will tell you a good deal of information about how to solve each problem and give you the relevant links to the specification.
您将需要确定您想要遵守的文档类型,然后开始努力使网站根据该类型进行验证。 我建议首先选择 XHTML Transitional 之类的东西。
这很可能需要付出大量的努力,而且可能没有真正的收获,并且可能涉及重构大量内容或进行软件更改,因此在开始之前请确保您愿意接受这一点。 如果该网站特别糟糕(例如,到处都是字体标签,没有使用 CSS),那么您很可能会发现它带来的麻烦大于其价值。
You're going to need to determine the document type to which you'd like to adhere, and then begin working on making the site validate against it. I'd suggest picking something like XHTML Transitional to start with.
This may very well shape up to be a significant amount of effort, possibly for no real gain, and it may involve refactoring a lot of content, or making software changes, so be sure you're willing to accept that before you start. If the site is particularly egregious (e.g. font tags all over the place, no use of CSS), then you may very well find it's more trouble than it's worth.
注意:如果没有 DOCTYPE,IE 将不会以标准模式呈现您的页面。
Note: Without the DOCTYPE, IE WILL NOT render your page in Standards Mode.
我倾向于保留任何能令人满意地工作的页面。 如果没有损坏,就不要修理它。 然而,任何需要重新设计或麻烦修改的页面我都会使用现代技术重新编写。
主要问题是,当您向页面添加 doctype 时,IE 6 及更高版本开始以显着不同的方式呈现页面: 它使用 Standards 模式而不是 Quirks 模式,并且盒子模型不同。 因此,即使页面编码良好,添加此文档类型也可以完全改变非常流行的浏览器中的呈现。 这意味着至少要花时间修复问题。
至于您应该使用哪种文档类型,我推荐 HTML 4.01 过渡。 XHTML 不值得付出努力; 大多数开发人员并不能很好地理解真正的 XHTML,如果您的代码中有一个错误,整个页面可能无法显示; 如果您将 XHTML 作为 HTML 提供,那么您也可以只提供 HTML。 HTML 不够好并没有太多充分的理由。
选择文档类型后,您可以修复 HTML 和 CSS,以使页面更小、更清晰且更易于维护。 这是一个复杂的问题,无法在快速帖子中完全解释,但归根结底是确保您的布局是使用 DIVS 而不是 TABLES 等完成的。考虑将所有 CSS 放在
确实应该是:
我的建议的主要关键是建设性地花费你的精力; 如果某些东西运行良好,您可能不需要更改它。 这些改变带来好处,但成本也很高,所以不要盲目地尝试实现某些目标,除非你愿意为此付出代价。 简单地说“W3C 验证”并不是一个好的目标。 相反,重点关注易于维护、内容丰富、文件小、屏幕阅读器的可访问性等。
I'd be inclined to leave-alone any pages which work satisfactorily. If it's not broken, don't fix it. However any pages which require redesign or troublesome modifications I would re-write using modern techniques.
The main problem is that when you add a doctype to the page IE 6 and up starts rendering the page significantly differently: it uses Standards mode instead of Quirks mode and the box model is different. So even if the page is well-coded, adding this doctype can totally change the rendering in a very popular browser. This means spending time fixing things, at minimum.
As for which doctype you should use, I recommend HTML 4.01 transitional. XHTML isn't worth the effort; real XHTML isn't well understood by most developers and if your code has a single error in it the whole page may not display; if you're serving XHTML as HTML then you may as well just serve HTML. There's not many good reasons why HTML isn't good enough.
Once you've selected a doctype, you can fix the HTML and CSS so that the page is smaller and cleaner and easier to maintain. This is a complex issue that can't be fully explained in a quick post, but it boils down to making sure your layouts are done using DIVS instead of TABLES, etc. Consider putting all the CSS in a <style> tag or external CSS file; personally I view any HTML attributes like height, width, or the inline style attribute to be bugs. For example:
Should really be:
The main key to my recommendation is to spend your energy constructively; if something works fine you may not need to change it. The changes bring benefits but they also have a high cost, so don't blindly try to achieve something unless you're willing to pay for it. Simply saying "W3C Validation" isn't really a good goal; instead focus on easy maintenance, good content, small file sizes, accessibility for screen readers, etc.
基于对问题的误解,我废弃了我之前的答案...
基本答案是使用 HTML Tidy,它应该对使其兼容有很大帮助。
I trash my previous answer, based on misunderstanding of the question...
The base answer would be to use HTML Tidy, it should help a lot to make it compliant.