如何缩进HTML代码--以及为什么它很重要

1,099 阅读3分钟

当你在建立HTML文件时,缩进你的代码是非常重要的。但你如何在HTML中做到这一点,为什么它很重要?

在这篇文章中,我将告诉你如何正确缩进你的HTML文件,并解释为什么正确格式化你的代码很重要。

如何在HTML中缩进你的代码

每当你有HTML元素嵌套在其他HTML元素中时,最好是使用缩进。嵌套元素被称为其父元素的子元素。

在这个例子中,我有一个p 元素嵌套在一个div 元素里面。 为了缩进p 元素,我将它向右移动两个空格:

<div>
  <p>This is what indentation looks like for HTML</p>
</div>

这被认为是最佳做法,将使你的代码更容易被其他开发者阅读。 现在我们可以看到,p 元素被嵌套在它的父元素--div

在下一个例子中,我有一个h2p 元素嵌套在一个main 元素里面,没有缩进:

<main>
<h2>Let's learn about indentation</h2>
<p>There is no indentation here</p>
</main>

但如果我编辑代码,将h2p 元素向右移动两个空格,现在我们就有适当的缩进:

<main>
  <h2>Let's learn about indentation</h2>
  <p>This is indentation</p>
</main>

h2p 元素是main 元素的子女。

HTML中常用的缩进例子

无序列表

li 元素向右缩进了两个空格,并嵌套在ul 元素内。ul 元素是li 元素的父元素:

<ul>
  <li>Cake</li>
  <li>Pizza</li>
  <li>Salad</li>
  <li>Apple</li>
</ul>

有序列表

li 元素向右缩进两个空格,并嵌套在ol 元素内。ol 元素是li 元素的父元素:

<ol>
  <li>Drive 1.2 miles and turn left on Cherry lane</li>
  <li>Drive 4.5 miles and turn right on Sycamore Rd.</li>
  <li>Drive 400 feet and stop at the light</li>
  <li>Turn left at the light</li>
  <li>Arrive at the destination on your right</li>
</ol>

为什么缩进很重要?

当你写代码时,重要的是写出能让其他开发者阅读的代码。可读性的一个重要部分是正确缩进你的HTML。

在这个例子中,我复制了 "通过制作猫咪照片应用程序学习HTML"项目中的所有代码,并删除了所有缩进部分,以向你展示糟糕的代码格式是什么样子:

<html lang="en">
<head>
<title>CatPhotoApp</title>
</head>
<body>
<h1>CatPhotoApp</h1>
<main>
<section>
<h2>Cat Photos</h2>
<!-- TODO: Add link to cat photos -->
<p>
Click here to view more
<a target="_blank" href="https://freecatphotoapp.com">cat photos</a>.
</p>
<a href="https://freecatphotoapp.com"
><img
src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg"
alt="A cute orange cat lying on its back."
/></a>
</section>
<section>
<h2>Cat Lists</h2>
<h3>Things cats love:</h3>
<ul>
<li>cat nip</li>
<li>laser pointers</li>
<li>lasagna</li>
</ul>
<figure>
<img
src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/lasagna.jpg"
alt="A slice of lasagna on a plate."
/>
<figcaption>Cats <em>love</em> lasagna.</figcaption>
</figure>
<h3>Top 3 things cats hate:</h3>
<ol>
<li>flea treatment</li>
<li>thunder</li>
<li>other cats</li>
</ol>
<figure>
<img
src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/cats.jpg"
alt="Five cats looking around a field."
/>
<figcaption>Cats <strong>hate</strong> other cats.</figcaption>
</figure>
</section>
<section>
<h2>Cat Form</h2>
<form action="https://freecatphotoapp.com/submit-cat-photo">
<fieldset>
<legend>Is your cat an indoor or outdoor cat?</legend>
<label
><input
id="indoor"
type="radio"
name="indoor-outdoor"
value="indoor"
checked
/>
Indoor</label
>
<label
><input
id="outdoor"
type="radio"
name="indoor-outdoor"
value="outdoor"
/>
Outdoor</label
>
</fieldset>
<fieldset>
<legend>What's your cat's personality?</legend>
<input
id="loving"
type="checkbox"
name="personality"
value="loving"
checked
/>
<label for="loving">Loving</label>
<input id="lazy" type="checkbox" name="personality" value="lazy" />
<label for="lazy">Lazy</label>
<input
id="energetic"
type="checkbox"
name="personality"
value="energetic"
/>
<label for="energetic">Energetic</label>
</fieldset>
<input
type="text"
name="catphotourl"
placeholder="cat photo URL"
required
/>
<button type="submit">Submit</button>
</form>
</section>
</main>
<footer>
<p>
No Copyright -
<a href="https://www.freecodecamp.org">freeCodeCamp.org</a>
</p>
</footer>
</body>
</html>

这根本不是好的HTML实践,因为它真的很难阅读和理解代码在做什么。如果你试图在专业的开发者环境中提交这样的东西,你的团队会对你一点也不满意。

现在,我将采用完全相同的代码,并适当缩进,向你展示其不同之处:

<html lang="en">
  <head>
    <title>CatPhotoApp</title>
  </head>
  <body>
    <h1>CatPhotoApp</h1>
    <main>
      <section>
        <h2>Cat Photos</h2>
        <!-- TODO: Add link to cat photos -->
        <p>
          Click here to view more
          <a target="_blank" href="https://freecatphotoapp.com">cat photos</a>.
        </p>
        <a href="https://freecatphotoapp.com"
          ><img
            src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg"
            alt="A cute orange cat lying on its back."
        /></a>
      </section>
      <section>
        <h2>Cat Lists</h2>
        <h3>Things cats love:</h3>
        <ul>
          <li>cat nip</li>
          <li>laser pointers</li>
          <li>lasagna</li>
        </ul>
        <figure>
          <img
            src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/lasagna.jpg"
            alt="A slice of lasagna on a plate."
          />
          <figcaption>Cats <em>love</em> lasagna.</figcaption>
        </figure>
        <h3>Top 3 things cats hate:</h3>
        <ol>
          <li>flea treatment</li>
          <li>thunder</li>
          <li>other cats</li>
        </ol>
        <figure>
          <img
            src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/cats.jpg"
            alt="Five cats looking around a field."
          />
          <figcaption>Cats <strong>hate</strong> other cats.</figcaption>
        </figure>
      </section>
      <section>
        <h2>Cat Form</h2>
        <form action="https://freecatphotoapp.com/submit-cat-photo">
          <fieldset>
            <legend>Is your cat an indoor or outdoor cat?</legend>
            <label
              ><input
                id="indoor"
                type="radio"
                name="indoor-outdoor"
                value="indoor"
                checked
              />
              Indoor</label
            >
            <label
              ><input
                id="outdoor"
                type="radio"
                name="indoor-outdoor"
                value="outdoor"
              />
              Outdoor</label
            >
          </fieldset>
          <fieldset>
            <legend>What's your cat's personality?</legend>
            <input
              id="loving"
              type="checkbox"
              name="personality"
              value="loving"
              checked
            />
            <label for="loving">Loving</label>
            <input id="lazy" type="checkbox" name="personality" value="lazy" />
            <label for="lazy">Lazy</label>
            <input
              id="energetic"
              type="checkbox"
              name="personality"
              value="energetic"
            />
            <label for="energetic">Energetic</label>
          </fieldset>
          <input
            type="text"
            name="catphotourl"
            placeholder="cat photo URL"
            required
          />
          <button type="submit">Submit</button>
        </form>
      </section>
    </main>
    <footer>
      <p>
        No Copyright -
        <a href="https://www.freecodecamp.org">freeCodeCamp.org</a>
      </p>
    </footer>
  </body>
</html>

这就更容易阅读了,现在我们可以看到所有嵌套在父元素中的子元素,并理解代码在做什么。

结论

在编写HTML时,使用良好的缩进方式来正确格式化你的代码是很重要的。你可以通过将元素向右移动两个空格来缩进它们:

<main>
  <h2>Let's learn about indentation</h2>
  <p>This is indentation</p>
</main>

这将使你的代码更容易被其他开发者阅读,并显示子元素和父元素之间的关系。

我希望你喜欢这篇文章,祝你在开发者的道路上好运。