Home > Article > Web Front-end > Refactoring HTML to improve web application design_html/css_WEB-ITnose
This article summarizes several rules and practices for reconstructing HTML to improve Web application design from three perspectives: good structure, effectiveness, and layout, combined with past project development experience. Part of the reference is from "Reconstructing HTML to Improve Web Application Design".
Refactoring. What is refactoring? Why refactor.
Refactoring is a process of making small changes without changing the behavior of the program. It is a process of gradually improving the code. Removing bad code that has accumulated over time to create code that is clearer and easier to maintain, debug, and add new features cannot just happen late in the coding process, or even when you realize your code is no longer viable. When it is necessary to rewrite, it should be gradually accumulated and modified from the beginning of development. In the past, due to the randomness of daily coding, problems accumulated and gradually spread, and in the end we had to start over. If time cannot withstand the need to reinvent the wheel, you have no choice. The only option is to refactor.
No matter what you do, don’t neglect improvement because of the pursuit of perfection. If you have enough time to do a little refactoring, then do a little. You can do more in the future when you have time. Although the overall reconstruction design is eye-catching and unforgettable, without daily accumulation, how can we achieve huge achievements? Your goal should be to have new changes to your code every day. After a few months of persistence, I believe we can all have clear code with pride.
Well-formed
The first task in converting markup to conform to modern standards is to achieve well-formation. Well-structured ensures the uniqueness of the DOM's operable document tree structure, thus becoming the basis for reliable cross-browser JavaScript code. For a chaotic page, any reliable automated processing or testing is very difficult to guarantee. Secondly, the display effect of the browser page is even more unpredictable. For scrambled pages, different browsers use different methods to supplement the correct fragments and correct errors. Therefore, when it comes to reconstructing HTML, the most important thing is undoubtedly to make the page well-structured.
To be well-formed, most websites must do at least or all of the following:
Validity
Validity is slightly stricter than well-formedness, that is, not only the syntax of the document must be correct, but also the semantics must be correct . Ensure that elements and attributes can only appear in the appropriate places according to their own semantics.
Effectiveness is the cornerstone of future-oriented development. Effective websites are device-independent, and effective pages convey the same information to different readers, even if they are using browsers with different interfaces.
Well-formedness and validity checks are the basic grammatical constraint guarantees. The next step is to ensure that the semantics are appropriate.
Specific implementation methods:
1. Add transitional DOCTYPE statement
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
过渡式DTD让你不必要完全语义标记就能让文档通过验证,并且他允许包含i,b和center等这些不推荐使用的表现性元书,因此,在进一步改善文档的语义之前,你可以找出比修复更严重的结构问题。
2,删除所有不存在的标签
3,用CSS替代center,i,font等不被推荐或弃用的标签。
4,把行内(inline)元素,放在块(block)元素中
要做到有效,文档中所有元素是有效的严格性XHTML元素还不够,还必须保证它们之间的正确关系,浏览器和其他程序处理XHTML需要依赖元素间的正确位置。
Do you like this pictrue?<br /><img src = "file.gif" />I think it's really <em>neat</em><!--改写为--><p>Do you like this pictrue?<br /></p><div><img src = "file.gif" /></div><p>I think it's really <em>neat</em></p>
布局
1,熟悉元素语义
每一个元素都应各司其职:ul是无序列表,ol是有序列表,table是表格式数据,blockquote是应用,h1~h6是标题等。恰当的语义元素有助于屏幕阅读器呈现更容易理解的结构,也能保证不同平台间正确显示。对于初学者,很多本来语义良好的元书,如ul,blockquote,table等,被滥用与实现某种特定布局效果。这些滥用的目的是给网页生成特定的外观,然而这些外观难以跨浏览器,几乎除了设计者自己的电脑,通常很多地方都不通用的。
失败经历: 为了实现导航效果,一开始没有考虑内容语义,无意中将导航主项和相关联的菜单分开,在利用css中的相对定位,设定top和left属性,将相关菜单移动到相关的主项下。这样会导致一个严重的问题。一旦将不同页面放置在不同的分辨率的屏幕上,菜单就会错位。就是说,针对不同分辨率的屏幕,还要设计不同的top和left属性。
<div class = "nav"> <div class="nav01">菜单一</div><div class="nav02">菜单二</div><div class="nav03">菜单三</div></div><div class = "menu"> <div class="menu01">子菜单一</div><div class="menu02">子菜单二</div><div class="menu03">子菜单三</div></div> 优化设计过的导航HTML结构,组合主菜单和子菜单:
<nav> <ul class="clear"> <li class="first"><a href="#">菜单一</a></li> <li> <span class="Darrow"></span> <a href="#">菜单二</a> <dl> <dt><span class="arrow"></span></dt> <dd><a href="#">子菜单一</a></dd><dd><a href="#">子菜单二</a></dd><dd><a href="#">子菜单三子菜单三</a></dd> </dl> </li> <li> <span class="Darrow"></span> <a href="#">菜单三</a> <dl> <dt><span class="arrow"></span></dt> <dd><a href="#">子菜单一</a></dd><dd><a href="#">子菜单二</a></dd><dd><a href="#">子菜单三子菜单三</a></dd> </dl> </li> </ul></nav>
|
The purpose of writing HTML is not for a certain structure or page appearance, but for how to better present the content. Therefore, before writing HTML, be sure to think about which semantic elements should be used for this content. .
Proper HTML is great for handling cross-browser issues. After you get the web design, before you start building the web application, you must stop thinking about how the page looks and start thinking about what the page means.
2. Replace table layout
CSS-based pages are smaller and simpler than table-based pages.
a) Easier to write and edit, faster to download,
b) Save bandwidth by switching to CSS. At the same time, the external css files can be cached and reused, without having to download them again every time the page is downloaded.
Instead of abusing the table element used to present tabular data and using table layout, you can consider the frequently used column layout:
1) Two columns, left There is a fixed-width sidebar on the side, and a flexible-width content column on the right
2) Three columns, a fixed-width sidebar on the left and right, and content in the middle.
|
3,内容与样式分离
当然我们的页面也需要漂亮的外观,以帮助我们在竞争中脱颖而出。这可以通过在独立的CSS样式中放置有关表现的信息来实现。CSS用来描述网页的外观,而浏览器可以自由选择不同的样式表或是修改过的样式表。实际上,你可以为不同的浏览器随意发送不同的样式表,也可以为它们独特的能力量身定制。这是响应式设计的基本实现方法。 “响应式网页不仅仅是响应不同类型的设备,而且需要响应不同的用户需求。响应式的初衷是为了让信息更好的传递交流,让所有人无障碍的获取信息,同时这也是 Web 的初衷。” 出于方便或者是自身的编码习惯,在修改某种被更改的需求的样式代码时候,我们很容易在html代码中直接镶嵌样式代码。这样的做法除了更快的完成你暂时的任务之外没有任何好处。
4,使用CSS定位替代框架
网站使用框架的理由实际上就两种: (1)为所有页面引入相同的静态内容,而不用单独编辑每个页面。例如导航,网页头部尾部。也就是说,单独的非框架页面可能比相应的框架页面更耗带宽,因为框架内容每次都要给客户端重新发送内容。 (2)显示多栏外观。例如Java API,包含包和类的列表,主体内容 然而,过多使用框架将降低可用性: (1)难以标记数千或返回指定页面 (2)难以保存和打印页面 (3)过多的滚动条占据屏幕的宝贵空间 在每一个页面上都有导航和其他相同或几乎相同的内容,对网站来说是非常普遍的,使用CSS取代框架,关键在于设立对应每个框架的div,每个div内容是对应框架里的文档内容。然而,这样做的问题在于,它违反了DRY原则(Don't Repeat Youself,别重复你自己),对于相同的内同,虽然有时候只是很小的变化,但还是需要不断在这一页那一页重复出现。重复内容通常也是代码的坏味道。框架在静态页面上避免恶劣不必要的重复,我提倡清晰,可维护的代码,在不损害用户界面的前提下,我更远一选择难看的代码而不是难看的用户界面。日常中原始的HTML是重复的,但不是我们必须编辑的,很多时候我们可以通过后台自动生成重复内容。
5,正确标记列表 正确标记列表能够提升可访问性,通常我们子啊列表中实现跳转和导航。 大部分浏览器给列表以及列表中的项目都制定特定的外观,通常表现为缩进和项目符号,可能不是你所需要的外观,因此很容易在搭建html的时候忽略它们的存在,放弃使用正确列表标记,而使用语义较差的标签,实现同样的效果。根据需求,你能够通过CSS可以容易地修复这些特定的外观。下面整理了开发中常用的CSS样式修改规则:
/*删除项目符号*/ ul{ list-style-type:none; }/*载入外部图片自定义项目符号*/ ul li { list-style-image : url(images/str.gif);}/*去掉缩进的规则*/ ul{ margin-left : 0px; padding-left :0px;}/*把项目排成一行*/ ul,li{ display: inline; margin:0px; padding: 0px;}/*在列表项之间插入逗号*/ ul li:after{ content : ",";}/*制定项目的宽度,超出时显示省略号*/ div.titleholder { font-size: 8pt; width: 100px; text-overflow: ellipsis; overflow: hidden; white-space: nowrap;} 然而,lu应用与列表上,blockquote应用在应用上。blockquote与ul相比,整理文本缩进更强大,更准确。
6,为图片添加width和height属性 width和height属性能让浏览器更快地样式化页面并展现给用户。但注意,这样做,对页面的显示速度有提升,但对下载速度并没有帮助。 出于一般项目开发触觉,改变图片的尺寸意味着要修改HTML,否则图片会奇怪地变大变小。如果需要经常改变图片,比如设计页面是,最好是在最后的阶段插入确定的宽度和高度。
|