通过创建注册表单学习HTML表单

步骤 1
欢迎来到注册表单项目! 首先,在文档的顶部添加 !DOCTYPE html 声明,这样浏览器就知道它在解析什么类型的文档。
步骤 4
给 head 添加一个 title 和一个 meta 元素。 设置项目的标题为 Registration Form,添加一个 meta 元素设置其charset 属性的值为 UTF-8。

 <head>
<meta charset ="utf-8">
<title>Registration Form</title>
  </head>

步骤 5
在 head 元素中嵌套一个自闭合 link 元素。 设置其 rel 属性值为 stylesheet, href 属性值为 styles.css。

 <head>
    <meta charset="UTF-8">
    <title>Registration Form</title>
    <link rel="stylesheet" href="styles.css" />
  </head>

步骤 7
在标题下,添加文字如下的段落元素来鼓励用户注册:

Please fill out this form with the required information

<body>
    <h1>Registration Form</h1>
<p>Please fill out this form with the required information</p>
  </body>

步骤 8
vh 单位代表视口高度(viewport height),相当于视口 height 高度的 1%。

现在是时候用 CSS 来装饰项目了。 首先设置 body 的 width 为 100%,height 为 100vh。

body{
  width: 100%;
  height: 100vh;
}

步骤 9
现在,通过设置 body 的默认 margin 为 0 来重置一些浏览器的默认设置,从而去掉水平滚动条。

body {
  width: 100%;
  height: 100vh;
  margin: 0;
}

步骤 10
看起来好多了。 现在,把 body 的 background-color 设置为 #1b1b32,使背景更护眼。 然后相应的把 color 设置为 #f5f6f7,让文字显示出来。

body {
  width: 100%;
  height: 100vh;
  margin: 0;
  background-color: #1b1b32;
  color: #f5f6f7;
}

步骤 11
正如标题所述的那样,你正在创建表单。 因此,在 p 元素之后,插入一个 form 元素,其 action 属性值为 https://register-demo.freecodecamp.org。

<body>
    <h1>Registration Form</h1>
    <p>Please fill out this form with the required information</p>
<form action="https://register-demo.freecodecamp.org"></form>
  </body>

步骤 12
method 属性指定了如何将表单数据发送到 action 属性中指定的 URL。 表单数据可以通过 GET 请求作为 URL 参数发送(method="get")或通过 POST 请求作为请求正文中的数据发送(method="post")。
设置 method 属性通过 POST 请求发送你的表单数据。

<form action='https://register-demo.freecodecamp.org' method="post"></form>

步骤 13
表单包含三个不同的部分,在 form 内部添加三个 fieldset 元素。

<body>
    <h1>Registration Form</h1>
    <p>Please fill out this form with the required information</p>
    <form method="post" action='https://register-demo.freecodecamp.org' >
      <fieldset>
        </fieldset>  
          <fieldset>
            </fieldset>
            <fieldset>
        </fieldset>
    </form>
  </body>

步骤 14
第一个 fieldset 将包含名字(name)、邮箱(email)和密码(password)字段。 首先,在第一个 fieldset 内部添加四个 label 元素。

 <form method="post" action='https://register-demo.freecodecamp.org'>
      <fieldset>
<label></label>
<label></label>
<label></label>
<label></label>
      </fieldset>
      <fieldset></fieldset>
      <fieldset></fieldset>
    </form>

步骤 15
在 label 元素中添加以下内容:

Enter Your First Name:
Enter Your Last Name:
Enter Your Email:
Create a New Password:

<fieldset>
       <label>Enter Your First Name:</label>
       <label>Enter Your Last Name:</label>
       <label>Enter Your Email:</label>
       <label>Create a New Password:</label>
     </fieldset>

步骤 16
rem 单位代表根 em,与 html 元素的字体大小有关。

由于 label 元素默认是行内元素,它们出现在其标签文本的同一行,使得文本难以阅读。 给 label 元素添加 display: block,并设置其 margin 为 0.5rem 0,来使其显示在不同的行,并且行之间有一定的距离。

label{
  display: block;
  margin:  0.5rem 0;
}

步骤 17
在每个 label 中嵌套一个 input 元素。 使 input 在 label 文字后面,冒号后添加一个空格。

<fieldset>
       <label>Enter Your First Name: <input></label>
       <label>Enter Your Last Name: <input></label>
       <label>Enter Your Email: <input></label>
       <label>Create a New Password: <input></label>
     </fieldset>

步骤 18
按照无障碍最佳实践,将 input 元素和 label 元素使用 for 属性联系起来。

使用 first-name、last-name、email 和 new-password作为相应的 id 属性的值。

<fieldset>
  <label for="first-name">Enter Your First Name: <input id="first-name"/></label>
  <label for="last-name">Enter Your Last Name: <input id="last-name"/></label>
  <label for="email">Enter Your Email: <input id="email" /></label>
  <label for="new-password">Create a New Password: <input id="new-password"/></label>
</fieldset>
<fieldset></fieldset>

步骤 19
指定表单元素的 type 属性对浏览器预期表单的数据类型至关重要。 如果未指定 type,浏览器会使用默认值 text
给前两个 input 元素设置其 type 属性值为 text,第三个 type 属性值为 email,第四个 type 属性值为 password。
email 类型只允许包含 @ 以及域名中包含 . 的电子邮件。 password 类型会屏蔽输入,如果网站没有启用 HTTPS 会警告。

 <fieldset>
        <label for="first-name">Enter Your First Name: <input id="first-name" type="text" /></label>
        <label for="last-name">Enter Your Last Name: <input id="last-name" type="text"/></label>
        <label for="email">Enter Your Email: <input id="email" type="email"/></label>
        <label for="new-password">Create a New Password: <input id="new-password" type="password"/></label>
      </fieldset>

步骤 20
距离父级 form 元素最近的第一个 type 为 submit 的 input 元素会被当作提交按钮。

为了处理表单提交,在最后 fieldset 元素后添加一个 input 元素,其 type 属性设置为 submit,value 属性设置为 Submit。

 <fieldset>
        <label for="first-name">Enter Your First Name: <input id="first-name" type="text" /></label>
        <label for="last-name">Enter Your Last Name: <input id="last-name" type="text" /></label>
        <label for="email">Enter Your Email: <input id="email" type="email" /></label>
        <label for="new-password">Create a New Password: </label>
        
      </fieldset>
      <fieldset></fieldset>
      <fieldset></fieldset>
      <input type="submit" value="Submit" />

步骤 21
到这里,应该可以提交表单了。 实际上,会发现提交之后没有任何效果。

为了使表单更具交互性,在第一个 fieldset 字段中的 input 元素添加 required 属性。

这样,如果没有填写必需字段就提交表单,会展示一个错误信息。

<fieldset>
       <label for="first-name">Enter Your First Name: <input id="first-name" type="text" required/></label>
       <label for="last-name">Enter Your Last Name: <input id="last-name" type="text" required/></label>
       <label for="email">Enter Your Email: <input id="email" type="email" required/></label>
       <label for="new-password">Create a New Password: <input id="new-password" type="password" required/></label>
     </fieldset>
     <fieldset></fieldset>
     <fieldset></fieldset>
     <input type="submit" value="Submit" />

步骤 22
一些 type 属性值含有内置表单验证。 例如, type="email" 要求该值是一个有效的电子邮件地址。

给密码 input 元素添加自定义验证,新增 minlength 属性设置其值为 8。 这样密码少于 8 个字符的时候会阻止提交。

<fieldset>
       <label for="first-name">Enter Your First Name: <input id="first-name" type="text" required /></label>
       <label for="last-name">Enter Your Last Name: <input id="last-name" type="text" required /></label>
       <label for="email">Enter Your Email: <input id="email" type="email" required /></label>
       <label for="new-password">Create a New Password: <input minlength="8" id="new-password" type="password" required /></label>
     </fieldset>

步骤 23
在 type="password" 内可以使用 pattern 属性来使用正则表达式来校验密码。

给密码 input 元素添加 pattern 属性,要求输入匹配 [a-z0-5]{8,}。

上面是一个正则表达式,匹配 8 个以上的小写字母或数字 0 到 5。 可以删掉 minlength 属性,测试一下。

<fieldset>
       <label for="first-name">Enter Your First Name: <input id="first-name" type="text" required /></label>
       <label for="last-name">Enter Your Last Name: <input id="last-name" type="text" required /></label>
       <label for="email">Enter Your Email: <input id="email" type="email" required /></label>
       <label for="new-password">Create a New Password: <input id="new-password" type="password" pattern="[a-z0-5]{8,}" required /></label>
     </fieldset>

步骤 24
让我们进入注册表单的下一部分。 This section will ask for the type of account the user is opening.

Start by adding two label elements to the second fieldset.

<fieldset>
<label></label>
<label></label>
   </fieldset>

步骤 25
Users will be allowed to choose either a Personal or Business.

在前两个 label 元素里,添加一个 type="radio" 的 input 元素。

<fieldset>
        <label>
          <input type="radio"></input>
        </label>
        <label> <input type="radio"></input></label>
      </fieldset>

步骤 26
Within each corresponding label element, and immediately after the input element, add a space and add the following text:

Personal
Business

<label for="personal-account"><input id="personal-account"  /> Personal Account</label>
        <label for="business-account"><input id="business-account" /> Business Account</label>

步骤 28
Currently when someone submit the form, they can submit it without checking the radio inputs. Although you had used required attribute to indicate the the input is required previously, this can't work in this case, because adding required to both inputs, will convey the wrong information to the form users.

To solve this, you can provide context of what is needed by adding legend element below the second fieldset with text Account type (required), then add checked attribute to the Personal input to make sure that the form is submitted with the required data in it.

<fieldset>
       <label><input type="radio" name="account-type" required checked/> Personal</label>
      
       <label><input type="radio" name="account-type"   required/> Business</label>
      <legend>Account type (required)</legend>
     </fieldset>

Step 29
Follow accessibility best practices by linking the input elements and the label elements in the second fieldset.

Use personal-account, and business-account as values for the respective id attributes.

 <fieldset>
        <legend>Account type (required)</legend>
        <label for="personal-account"><input id="personal-account" type="radio" name="account-type" checked /> Personal</label>
        <label for="business-account"><input id="business-account" type="radio" name="account-type" /> Business</label>
      </fieldset>

Step 30
You need to confirm that the user has read the terms and conditions.

Add a label element. Inside the newly created label element add an input element and set the type attribute to checkbox. Make this input element required so users can not sign up without agreeing to the terms and conditions.

Add an id and for attribute with the value terms-and-conditions to the elements for accessibility.

<label for="terms-and-conditions" name="terms-and-conditions">
          <input id="terms-and-conditions" type="checkbox" required name="terms-and-conditions" class="inline" /> I accept the <a href="https://www.freecodecamp.org/news/terms-of-service/">terms and conditions</a>
        </label>

Step 31
Add I accept the terms and conditions text to the newly added label, then link the text terms and conditions to the following location:
https://www.freecodecamp.org/news/terms-of-service/

<fieldset></fieldset>

      <label for="terms-and-conditions"> <input id="terms-and-conditions" type="checkbox" required name="terms-and-conditions" class="inline" /> I accept the <a href="https://www.freecodecamp.org/news/terms-of-service/">terms and conditions</a>

Step 32
现在到了最后一个 fieldset。 如果你想允许用户上传个人资料图片,应该怎么做呢?

类型为 file 的 input 可以实现。 添加一个 label,其文本为 Upload a profile picture:,嵌套一个 input 以接收上传的文件。

      <fieldset>
 <label for="profile-picture">Upload a profile picture: <input id="profile-picture" type="file" name="file" /></label>
      </fieldset>

Step 33
在第一个 label 在添加另一个,其文本为 Input your age (years):。 在它后面添加一个 input 并设置其 type 为 number。

由于我们不希望 13 岁以下的用户注册,给 input 添加一个 min 属性并设置其值为 13。 此外,我们假设年龄超过 120 岁的用户不会来注册,添加一个 max 属性,并设置其值为 120。

现在,如果提交的表单里面的年龄超过了这个范围,会出现一个警告,并阻止提交。 试一下吧。

  <fieldset>
        <label>Upload a profile picture: <input type="file" /></label>
<label for="age">Input your age (years): <input id="age" type="number" name="age" min="13" max="120" /></label>
      </fieldset>

Step 34
使用 select 元素给表单添加一个下拉列表很方便。 select 元素是包含一组 option 元素的容器,option 元素就是下拉列表的内容。 每一个元素都需要一个结束标签。

首先,在两个 label 元素下面添加一个 select 元素。 然后,在 select 元素内添加 5 个 option 元素。

 <fieldset>
        <label>Upload a profile picture: <input type="file" /></label>
        <label>Input your age (years): <input type="number" min="13" max="120" /></label>
<select id="referrer" name="referrer">
            <option></option>
            <option></option>
            <option></option>
            <option></option>
            <option></option>
          </select>
      </fieldset>
      

Step 35
把 select 元素(以及它的 option 元素)放到 label 元素内, 后者具有文本 How did you hear about us?。 文本应该在 select 元素前面。

 <fieldset>
        <label>Upload a profile picture: <input type="file" /></label>
        <label>Input your age (years): <input type="number" min="13" max="120" /></label>
       <label for="referrer">How did you hear about us?
        <select>

Step 36
下拉菜单目前是空的。 给每一个 option 元素依次添加对应下面的文本。

(select one)
freeCodeCamp News
freeCodeCamp YouTube Channel
freeCodeCamp Forum
Other

 <fieldset>
        <label>Upload a profile picture: <input type="file" /></label>
        <label>Input your age (years): <input type="number" min="13" max="120" /></label>
        <label>How did you hear about us?
          <select>
             <option>(select one)</option>
            <option>freeCodeCamp News</option>
            <option>freeCodeCamp YouTube Channel</option>
            <option>freeCodeCamp Forum</option>
            <option>Other</option>
          </select>
        </label>
      </fieldset>

Step 37
提交带有选择菜单的表单向服务端发送的信息 value 并不是预期的。 因此,每一个 option 需要指定一个 value 属性。 如果没有指定,向服务器默认提交的是 option 内的文本。

给第一个 option 指定 value "",同级的 option 元素的 value 属性依次指定 1 到 4。

<select>
            <option value="">(select one)</option>
           <option value="1">freeCodeCamp News</option>
           <option value="2">freeCodeCamp YouTube Channel</option>
           <option value="3">freeCodeCamp Forum</option>
           <option value="4">Other</option>
         </select>

Step 38
textarea 元素和 text 类型的 input 类似,区别是 textarea 有一些额外的好处,比如可以方便地添加更多的文本以及设置默认显示的行数和列宽。

用户将能够注册个人简历。 在 fieldset 的末尾添加带有 Provide a bio: 文本的 label。 在 label 元素内添加一个 textarea 元素。 请注意,textarea 需要一个结束标签。

  </select>
        </label>
<label for="bio">Provide a bio:
          <textarea ></textarea>
        </label>
      </fieldset>

Step 39
将表单元素和它们的 label 元素关联起来。

使用 profile-picture、age、referrer 和 bio 作为相应的 id 属性的值。

<fieldset>
       <label for="profile-picture">Upload a profile picture: <input id="profile-picture" type="file" name="file" /></label>
       <label for="age">Input your age (years): <input id="age" type="number"  /></label>
       <label for="referrer">How did you hear about us?
         <select id="referrer">
           <option value="">(select one)</option>
           <option value="1">freeCodeCamp News</option>
           <option value="2">freeCodeCamp YouTube Channel</option>
           <option value="3">freeCodeCamp Forum</option>
           <option value="4">Other</option>
         </select>
       </label>
       <label for="bio">Provide a bio:
         <textarea id="bio"></textarea>
       </label>
     </fieldset>

Step 42
在提交表单时,向每个可提交的元素提供 name 属性很有用,并且是最佳实践。 此属性用于表单提交时识别不同的元素。

除了两个 radio 输入之外,为每个可提交元素指定一个你选择的唯一 name 属性。
<label for="email">Enter Your Email: <input id="email" name="email" type="email" required /></label>

    <label for="new-password">Create a New Password: <input id="new-password" name="new-password" type="password" pattern="[a-z0-5]{8,}" required /></label>
  </fieldset>
  <fieldset>
    <label for="personal-account"><input id="personal-account" type="radio" name="account-type" class="inline" /> Personal Account</label>
    <label for="business-account"><input id="business-account" type="radio" name="account-type" class="inline" /> Business Account</label>
    <label for="terms-and-conditions" name="terms-and-conditions">
      <input id="terms-and-conditions" type="checkbox" required name="terms-and-conditions" class="inline" /> I accept the <a href="https://www.freecodecamp.org/news/terms-of-service/">terms and conditions</a>
    </label>
  </fieldset>
  <fieldset>
    <label for="profile-picture">Upload a profile picture: <input id="profile-picture" type="file" name="file" /></label>
    <label for="age">Input your age (years): <input id="age" type="number" name="age" min="13" max="120" /></label>
    <label for="referrer">How did you hear about us?
      <select id="referrer" name="referrer">
        <option value="">(select one)</option>
        <option value="1">freeCodeCamp News</option>
        <option value="2">freeCodeCamp YouTube Channel</option>
        <option value="3">freeCodeCamp Forum</option>
        <option value="4">Other</option>
      </select>
    </label>
    <label for="bio">Provide a bio:
      <textarea id="bio" name="bio" rows="3" cols="30" placeholder="I like coding on the beach..."></textarea>
    </label>
  </fieldset>

Step 43
注册表单的 HTML 已完成。 现在来美化一下它。

首先将 body 的字体更改为 Tahoma,字体大小更改为 16px。

body {
  width: 100%;
  height: 100vh;
  margin: 0;
  background-color: #1b1b32;
  color: #f5f6f7;
  font-family: Tahoma;
  font-size: 16px;
}

Step 44
给 h1 和 p 元素添加一个值为 1em auto 的 margin 属性使其居中显示。 同样也设置其文字 center 居中对齐显示。

h1, p {
  margin: 1em auto;
  text-align: center;
}

Step 45
通过将 form 元素的 margin 设置为 0 auto 来使其居中显示。 然后设置其最大宽度为 500px,最小宽度为 300px。 在这个范围内,设置其 width 为 60vw。

form {
  width: 60vw;
  max-width: 500px;
  min-width: 300px;
  margin: 0 auto;
  padding-bottom: 2em;
}

Step 46
在开发过程中,看到 fieldset 默认边框很有帮助。 然而,它们使内容间的距离过远。

删除 border,然后给 fieldset 的顶部和底部添加 2rem 的 padding。 确保移除了左侧和右侧的 padding。

fieldset {
  border: none;
  padding: 2rem 0;
  
}

Step 48
最后一个 fieldset 元素的边框看起来有点不合适。 你可以使用 last-of-type CSS 伪类选择特定类型的最后一个元素,如下所示:

p:last-of-type { }
这将选择最后一个 p 元素。 创建一个定位最后一个 fieldset 元素的新选择器,并将其 border-bottom 设置为 none。

fieldset:last-of-type {
  border-bottom: none;
}

Step 51
只选择 .inline 元素,并设置其 width 为unset。 这会清除之前设置的 input 元素的 width: 100% 规则。

.inline {
  width: unset;
  margin: 0 0.5em 0 0;
  vertical-align: middle;
}

Step 57
要设置提交按钮的样式,你可以使用_属性_选择器,它根据给定的属性值选择元素。 下面是一个例子:

input[name="password"]
上面选择了 name 属性值为 password 的 input 元素。

现在,使用属性选择器设置提交按钮的样式,其中 display 为 block,width 为 60%。

input[type="submit"] {
  display: block;
  width: 60%;
  margin: 1em auto;
  height: 2em;
  font-size: 1.1rem;
  background-color: #3b3b4f;
  border-color: white;
  min-width: 300px;
}

Step 64
Last, but not least, make the input for the terms and condition inline, then change the text color of the terms and conditions link element to #dfdfe2.

很棒! 你已经完成了_注册表_练习项目的最后一部分。

<label for="terms-and-conditions">
       <input id="terms-and-conditions" type="checkbox" required name="terms-and-conditions" /> I accept the <a href="https://www.freecodecamp.org/news/terms-of-service/">terms and conditions</a>
     </label>
a{font-color:#dfdfe2;}

评论

暂无评论