不要濫用div,保持代碼的整潔—–Coding Clean and Semantic Templates

來源:互聯網
上載者:User
文章目錄
  • Conclusion

 

本文旨在介紹如何保證頁面代碼的整潔、以維護性。使用有語義的頁面標籤,減少<div>標籤的濫用。

1. 移除不必要的<div>標籤

    嵌套在<form><ul>外面的標籤沒有必要

2. 使用有語義的標記

    <h1><ul><p>等標籤,替代<div>,即便樣式表丟失,仍然保證頁面的可讀性。

3. 盡量少的使用<div>標籤

4. 代碼縮排格式

5. 在</div>結尾處加上這個<div>塊的注釋

 

 

 

 

原文地址:http://www.webdesignerwall.com/tutorials/coding-clean-and-semantic-templates/

 

If you are the guy who uses <div>
tag for
everything, this post is for you. It focuses on how you can write clean
HTML code by using semantic markups and minimize the use of <div>
tag. Have you ever edited someone’s templates, don’t those messy tags
drive you crazy? Not only writing clean templates can benefit yourself,
but your team as well. It will save you time when you have to debug and
edit (particularly the large projects).

 

1.
Remove The Unnecessary <div> Tags

I’ve seen a lot of people wrap a <div>
tag around the <form>
or <ul>
menu list. Why create an extra <div>
tag that you don’t need? You can achieve the same result by applying the CSS rules to the selector.

Example 1:

The example below shows how you can drop the <div>
tag and declare the styling to the form
selector.

Example 2:

Sometimes we wrap the content with an extra <div>
tag strictly for spacing purposes. The example on the left uses a <div class="sidebox">
to create margin space in between the boxes. But if each box has a heading (ie. <h4>
), we can simply apply the margin space to the h4
selector and drop the extra <div class="sidebox">
tag.

2.
Use Semantic Markups

You should always use semantic markups to code HTML documents (ie. <h1>
for headings, <p>
for paragraph text, and <ul>
for list items). So, even when the CSS is not presented nor supported, your document still makes sense.

Example:

The image below compares the rendering differences between <div> markups and semantic markups with no css supported.

3.
Minimize The Use of <div> Tags

Have you seen the messy templates where <div>
tags are everywhere and they drive you crazy? Have you ever missed a closing </div>
tag or have an extra opening <div>
tag messing up the entire layout? I’m sure most developers have
experienced it before. So, you should always minimize the use of <div>
tag if possible. It will make debugging and editing easier.

Example 1:

Instead of using <div>
tag for breadcrumb navigation, it makes more sense to use <p>
tag.

Example 2:

The following example shows how I can use CSS to cut down two <div>
tags by replacing with one <span>
tag. They both produce the same layout.

4.
Format (Indent) Your Code

You should always format your source code (ie. indent your nested
elements) so it is easier to read and debug. If you have Adobe
Dreamweaver, you can easily format the code by using the Commands > Apply Source Formatting
(from the application menu).

5.
Comment The Closing </div> Tags

When coding for platform templates (ie. WordPress themes), the
template is most likely splitted into several files: index.php,
header.php, sidebar.php, and footer.php. Hence, you should always make
a comment for the closing </div>
tags so you won’t get lost. For example, when I see </div><!-- /wrapper -->
, I know it is the closing tag for <div id="wrapper">
.

Example:

I usually insert a HTML comment tag right after the closing </div>
tag. I use the forward slash to indicate it is the closing tag.

Conclusion
  • Minimize the use of <div>
    tags.
  • You should only use the <div>
    tag for the main layout sections such as: header, content, sidebar, and footer.
  • The content should be in semantic HTML tags, not <div>
    tags.
  • Format the source code and label the closing </div>
    tags.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.