search
HomeWeb Front-endHTML Tutorial《web前端最佳实践》-HTML篇_html/css_WEB-ITnose

一、web标准

很多人把web标准挂在嘴边,却没有引起足够重视,只关注页面外在的表现而忽略页面本身代码的质量。

标准的页面具有哪些优点?

  • 保证浏览器正确渲染 最大限度的保证页面在不同浏览器正常解析,同时还能最大限度保证在未来的各种客户端中正常解析。

  • 更容易被搜索引擎搜寻,提高网站搜索排名。标准的页面基本完成了一半的搜索引擎优化工作,搜索引擎的网络“爬虫”目的是读懂网站的内容,并找出网站关键字。合理使用标签和属性,可以帮助搜索引擎准确理解网站内容。

  • 提高网站易用性。让网站能被更多用户访问,比如视力或肢体障碍用户。

  • 更好的维护和扩展。 多个开发者如果遵循统一的标准,则会更好的理解和维护已有的页面。标签、样式、以及行为分离的标准页面显然具有更好的扩展性。

  • 如何做到标准?

  • 标准的HTML页面结构,比如文档类型声明、head(title和引入的文件)、body等。

  • 正确闭合标签。

  • 停止使用不标准的标签和属性,简化代码。

  • 样式和结构分离。

  • 添加javascript禁用的提示信息。

  • 添加必要的标签,比如常用的keywords、description,设置IE浏览器的兼容模式,设置页面在移动设备中的显示等。

二、高可读性HTML

一些小原则

1、注意标签语义化,网页的外观看起来怎样跟浏览器的解析以及搜索引擎的抓取毫无关系,高可读性的html代码除了使开发者自身更易看懂和维护之外,最重要还是给机器去读的。

2、没必要使用的标签就不要用,可以利用其它的方法去做到同样的效果,比如伪元素。 比较常见的使用不当之处有“分隔符”和“空标签清除浮动”等。

3、使用一些非文字内容的时候,虽然看似使用空标签配合css就可以完成,但搜索引擎却不能得知它的作用,可以添加一些文字说明,然后再用css将其隐藏。这样做也便于第三方阅读工具的识别。

4、熟悉各标签上规范的属性,给html标签添加必要的属性 比如常见的:alt、title、for等。

5、
应仅用于文本内容的换行,不应用于行级元素的换行。

6、网页中的图片是使用《web前端最佳实践》-HTML篇_html/css_WEB-ITnose还是background? 若作为页面内容的一部分,则使用img,若仅用于装饰,使用background。

常用几类标签的正确使用方式

网页标题层级

标题标签指的是h1~h6这6个标签,统称hx标签,相比其他的语义标签,标签会受到搜索引擎的“特殊照顾。

(1)在标题处应使用。

(2)页面中只使用一个h1标签,搜索引擎会给予它最高权重。其他模块标题标签从h2开始。

(3)标签使用过程中不要跳级,保持层级连贯性。

(4)不要单纯使用标签给内容设置样式。

正确设计表单

(1)不要使用

(2)不要用展示表单的文本,应使用

给表单控件分组,用来定义控件组的标题,另外还可以通过设置
的disable属性禁用包含的表单控件。

(4)给输入控件设置合适的水印提示,所谓水印提示,即在未填写之前里面所显示的内容,当控件获取焦点,或者用户开始输入时消失。可使用placeholder属性,IE9以下可使用jquery-placeholder插件模拟。

(5)如有必要,设置tab顺序。很多用户会有使用键盘tab键来切换到下一个输入框的习惯,默认情况下,会安装界面上的分布顺序来处理,如需要特殊设定,可设置tabindex属性值。

(6)使用html5中新引入的表单控件类型,如url、email、search等。高级浏览器有很好的支持,不支持它们的浏览器会以text类型控件展现。

精简html代码

1、删除多余容器,比较典型的是

,这是两个没有任何语义,常被滥用的标签。

2、装饰性元素使用css实现。如背景、伪元素等。

3、避免使用table布局,是代码更少,更清晰,避免因重绘或重排而降低性能。

过时的块级和行内元素

每个使用css的人都了解过这两个概念,但是,却都是从样式的角度去理解原生的元素,有点不妥,而且它本身跟“结构与表现分离”的思路有违背,html5中淡化了二者的定义,更具体的分成了7类。 它们是:元数据式内容、流式内容、章节式内容、标题式内容、段落式内容、嵌入式内容和交互式内容。

从内容模型来说,以前对于块级和行内元素中内容有一些限制,html5结束了这种复杂的局面,从实际需求出发重新定义了内容模型。比如元素可以包含块状元素了。还有其他的很多差异,需要大家有重新的认识。

三、小叙Html5

先来看几个时间点。

2012年12月17日,HTML5定义完成。

2014年6月17日,发布标准草案的最终征求意见稿。

2014年10月28日,W3C发布HTML5正式推荐标准。

新特性的使用

近几年,HTML5的概念被炒的过头了,给人感觉是酷炫,甚至“无所不能”,然而它只是一个新的web标准。是对几个应用比较广泛的特性的讨论。

1、简化定义方式。

避免不必要的复杂性,比如文档类型声明、定义页面编码、样式和脚步的引用方式等。

2、使用新标签和新属性。

新标签:

语义化标签:

功能性标签:

新的输入控件类型:tel、search、email等

新属性:

<script>的async属性</script>

的target属性

标签自定义属性data-*,用于保存自定义数据,同时添加对应API:dataset

小tips

不要使用HTML5中已经弃用的标签和属性

1、没有任何语义,仅为了设置外观

等 2、破坏了可用性的标签 、、

3、移除的标签属性 align、valign、bgcolor等

如何处理兼容性

(1)通过document.creatElement产生标签

(2)使用成熟的框架,如:html5shim

注:此处仅为让浏览器识别新标签的兼容方法,其他还有很多兼容问题,此处不赘述。

此文为读了党建前辈所著《web前端开发最佳实践》HTML部分之后所记录,内容仅仅是冰山一角。阅读过程中意识到了自身的很多不足,受益匪浅。希望大家都能读一下。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor