编写注册表单 HTML

1、 视口高度(vh`单位)

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

2、 去掉水平滚动条

设置 body 的默认 margin 为 0 来重置一些浏览器的默认设置,从而去掉水平滚动条

3、 method 属性

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

4、 rem 单位

rem 单位代表根 em,与 html 元素的字体大小有关
由于 label 元素默认是行内元素,它们出现在其标签文本的同一行,使得文本难以阅读。 给 label 元素添加 display: block,并设置其 margin0.5rem 0,来使其显示在不同的行,并且行之间有一定的距离

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

在这里插入图片描述

5、 type 属性

指定表单元素的 type 属性对浏览器预期表单的数据类型至关重要。 如果未指定 type,浏览器会使用默认值 text
给前两个 input 元素设置其 type 属性值为 text,第三个 type 属性值为 email,第四个 type 属性值为 password

<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>
type=“email”

email 类型只允许包含 @ 以及域名中包含 . 的电子邮件
一些 type 属性值含有内置表单验证。 例如, type="email" 要求该值是一个有效的电子邮件地址

type=" password "

password 类型会屏蔽输入,如果网站没有启用 HTTPS 会警告
给密码 input 元素添加自定义验证,新增 minlength 属性设置其值为 8。 这样密码少于 8 个字符的时候会阻止提交

<label for="new-password">Create a New Password: <input id="new-password" type="password" required minlength='8'/></label>

type="password" 内可以使用 pattern 属性来使用正则表达式来校验密码,给密码 input 元素添加 pattern 属性,要求输入匹配 [a-z0-5]{8,},这是一个正则表达式,匹配 8 个以上小写字母或数字 0 到 5

<label for="new-password">Create a New Password: <input id="new-password" type="password" pattern="[a-z0-5]{8,}" required /></label>
type=“file”

类型为 fileinput 可以实现允许用户上传个人资料图片

<label><input type="file"/>Upload a profile picture:</label>
type=“number”

由于不希望 13 岁以下的用户注册,给 input 添加一个 min 属性并设置其值为 13。 此外,我们假设年龄超过 120 岁的用户不会来注册,添加一个 max 属性,并设置其值为 120
现在,如果提交的表单里面的年龄超过了这个范围,会出现一个警告,并阻止提交

<label>Input your age (years):<input type="number" min="13" max="120"/></label>

6、提交按钮

距离父级 form 元素最近的第一个 typesubmitinput 元素会被当作提交按钮

<input value="Submit" type="submit" />

7、 交互性

为了使表单更具交互性,在第一个 fieldset 字段中的 input 元素添加 required 属性,这样如果没有填写必需字段就提交表单,会展示一个错误信息

8、 下拉列表(select 元素)

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

<select>
     <option>(select one)</option>
     <option>freeCodeCamp News</option>
     <option>freeCodeCamp YouTube Channel</option>
     <option>freeCodeCamp Forum</option>
     <option>Other</option>
</select>

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

9、 textarea 元素

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

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

在这里插入图片描述
可以添加 rowscols 属性,来指定它的初始大小

10、 placeholder 属性

placeholder 接受一个文本值,在用户开始键入之前显示以提示用户

<textarea id="bio" rows="3" cols="30" placeholder="I like coding on the beach..."></textarea>

在这里插入图片描述

11、 last-of-type CSS 伪类

可以使用 last-of-type CSS 伪类选择特定类型的最后一个元素,如下所示:

p:last-of-type { }

12、 属性选择器

根据给定的属性值选择元素:下面选择了 name 属性值为 passwordinput 元素

input[name="password"]

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

input[type="submit"] {
  display: block;
  width: 60%;
}

完整代码

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Registration Form</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <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>
        <label for="first-name">Enter Your First Name: <input id="first-name" name="first-name" type="text" required /></label>
        <label for="last-name">Enter Your Last Name: <input id="last-name" name="last-name" type="text" required /></label>
        <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">
          <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>
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>

styles.css

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

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

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

fieldset {
  border: none;
  padding: 2rem 0;
  border-bottom: 3px solid #3b3b4f;
}

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

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

input,
textarea,
select {
  margin: 10px 0 0 0;
  width: 100%;
  min-height: 2em;
}

input, textarea {
  background-color: #0a0a23;
  border: 1px solid #0a0a23;
  color: #ffffff;
}

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

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;
}

input[type="file"] {
  padding: 1px 2px;
}
a{
  color:#dfdfe2;
}

效果图

在这里插入图片描述

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个简HTML注册页面的示例: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册</title> </head> <body> <h1>注册</h1> <form action="#" method="post"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> <br> <label for="password">密码:</label> <input type="password" id="password" name="password" required> <br> <label for="email">邮箱:</label> <input type="email" id="email" name="email" required> <br> <label for="gender">性别:</label> <input type="radio" id="male" name="gender" value="male"> <label for="male">男</label> <input type="radio" id="female" name="gender" value="female"> <label for="female">女</label> <br> <label for="birthdate">生日:</label> <input type="date" id="birthdate" name="birthdate"> <br> <input type="submit" value="注册"> <input type="reset" value="重置"> </form> </body> </html> ``` 这个页面包含一个表单,用于收集用户注册信息,包括用户名、密码、邮箱、性别和生日。每个输入字段都有一个相关联的标签,以便于用户明确知道应该在哪里输入相应的信息。此外,`required`属性确保用户必须填写这些字段才能提交表单。 ### 回答2: HTML(HyperText Markup Language)是一种用于创建网页结构和内容的标记语言。下面是一个简的300字回答,解释如何使用HTML编写一个注册页面。 在编写HTML注册页面之前,需要考虑页面的布局和所需要的输入字段。一个典型的注册页面通常包含输入框、选按钮、复选框、下拉列表和提交按钮等元素。 下面是一个使用HTML编写的示例注册页面: ```html <!DOCTYPE html> <html> <head> <title>注册页</title> </head> <body> <h2>用户注册</h2> <form> <label for="username">用户名:</label><br> <input type="text" id="username" name="username"><br><br> <label for="password">密码:</label><br> <input type="password" id="password" name="password"><br><br> <label for="email">邮箱:</label><br> <input type="email" id="email" name="email"><br><br> <label for="gender">性别:</label><br> <input type="radio" id="gender" name="gender" value="male">男 <input type="radio" id="gender" name="gender" value="female">女<br><br> <label for="newsletter">订阅新闻:</label><br> <input type="checkbox" id="newsletter" name="newsletter" value="yes"><br><br> <label for="country">国家:</label><br> <select id="country" name="country"> <option value="China">中国</option> <option value="USA">美国</option> <option value="UK">英国</option> </select><br><br> <input type="submit" value="注册"> </form> </body> </html> ``` 在上面的示例中,我们定义了一个简注册表。该表单包括用户名、密码、邮箱、性别、订阅新闻、国家以及提交按钮。每个输入字段都有相应的`id`和`name`属性,以及标签描述。表单使用`input`元素定义文本输入框、密码框、邮箱输入框和选按钮。使用`checkbox`元素定义复选框。使用`select`元素定义下拉列表。提交按钮使用`submit`类型的`input`元素。 通过这段代码,用户可以在注册页填写用户名、密码、邮箱等信息,并选择性别、订阅新闻和选择国家。最后点击"注册"按钮提交表单数据。 使用HTML编写注册页面只是一个简示例,实际开发中可能会涉及更多的表单校验、样式美化和后端数据处理。但这段代码可以帮助你了解如何开始编写一个简的注册页面。 ### 回答3: 使用HTML编写注册页面的基本步骤如下: 1. 创建一个HTML文件,使用<!DOCTYPE html>声明文档类型。 2. 在<html>标签中,添加<head>和<body>标签。 3. 在<head>标签中,添加<meta>标签设置字符集为UTF-8,并设置页面标题。 4. 在<body>标签中,添加<form>标签来创建表单。 5. 在<form>标签中,添加<input>标签用于输入用户的用户名、密码和邮箱等信息。 6. 在<input>标签中,使用type属性来指定输入框的类型,如text、password和email等。 7. 使用label标签来为每个输入框添加相应的文本描述。 8. 使用<button>标签来创建提交按钮。 9. 使用CSS样式来美化页面,如设置背景颜色、字体样式和输入框边框等。 10. 添加必要的验证,如设置输入框的最小长度和必填字段等。 11. 添加事件处理程序,如使用JavaScript来验证输入的数据是否符合要求。 12. 最后,保存HTML文件并在浏览器中打开,即可看到注册页面。 总之,使用HTML编写注册页面需要了解HTML语法和标签的使用,以及基本的CSS样式和JavaScript知识。通过添加合适的标签和属性,结合CSS样式和JavaScript验证,就能创建一个简且美观的注册页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值