制作一个单行文本框
创作时间:
2025-01-22 07:08:27
作者:
@小白创作中心
制作一个单行文本框
表单控件是网页开发中非常基础和重要的内容,掌握它们的使用方法对于前端开发来说至关重要。本文将通过11个详细的案例,带你全面了解HTML+CSS表单控件的使用方法,包括单行文本框、密码输入框、单选按钮、复选框、普通按钮、提交按钮、重置按钮、文件上传框、Textarea控件和select控件等。每个案例都包含了详细的代码和效果展示,非常适合初学者学习和参考。
单行文本框 (text)
- 用途:允许用户输入单行文本。
- HTML 示例:
<input type="text" name="username"> - 特点:可以设置最大长度、只读等属性。
案例:制作一个单行文本框
1. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>制作一个单行文本框</title>
<style>
input[type="text"] {
width: 300px;
height: 50px;
border: 1px solid #000;
border-radius: 5px;
padding: 10px;
font-size: 16px;
transition: all 0.3s ease;
background-color: #f5f5f5;
color: #333;
}
input[type="text"]:focus {
border-color: #007bff;
outline: none;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
input[type="text"]:hover {
border-color: #007bff;
}
</style>
</head>
<body>
<input type="text" placeholder="请输入内容">
</body>
</html>
2. 效果
未选中效果
选中效果
密码输入框 (password)
- 用途:用于安全地输入密码,输入的内容会以圆点或星号显示。
- HTML 示例:
<input type="password" name="userpass"> - 特点:与文本框类似,但隐藏了实际输入字符。
案例:制作密码输入框
1. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>制作一个密码输入框</title>
<style>
/* 密码输入框的基本样式 */
input[type="password"] {
width: 300px;
height: 50px;
border: 1px solid #ccc;
border-radius: 10px;
padding: 10px;
font-size: 16px;
transition: all 0.3s ease;
background-color: #f9f9f9;
color: #333;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
/* 悬停效果 */
input[type="password"]:hover {
border-color: #007bff;
box-shadow: 0 4px 10px rgba(0, 123, 255, 0.3);
}
/* 焦点状态样式 */
input[type="password"]:focus {
border-color: #007bff;
outline: none;
box-shadow: 0 4px 10px rgba(200, 243, 9, 0.842);
}
/* 密码输入框的占位符文本颜色 */
::-webkit-input-placeholder {
color: #da2323;
}
</style>
</head>
<body>
<input type="password" placeholder="请输入密码" maxlength="6">
</body>
</html>
2. 效果
非选中状态
选中状态
状态
单选按钮 (radio)
- 用途:当一组选项中只能选择一个时使用。
- HTML 示例:
<input type="radio" name="gender" value="male"> Male - 特点:同名的一组单选按钮里只能选一个。
案例:制作单选按钮
1. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>制作单选按钮</title>
<style>
.myRadioStyle{
width: 30px;
height: 30px;
border-radius: 50%;
border: solid 2px #000;
background-color: #00ccff;
cursor: pointer;
}
.myRadioStyle:checked{
background-color: #ff0000;
}
</style>
</head>
<body>
<input class="myRadioStyle" type="radio" name="sex" id="man">男<br>
<br/>
<input class="myRadioStyle" type="radio" name="sex" id="woman">女<br>
</body>
</html>
2. 效果
复选框 (checkbox)
- 用途:当多个选项都可以被同时选择时使用。
- HTML 示例:
<input type="checkbox" name="interests" value="coding"> Coding - 特点:可以独立勾选,允许多个选择。
案例:制作一个复选框
1. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>制作复选框</title>
<style>
/* 复选框的基本样式 */
.myInputByCheckBoxStyle {
width: 20px;
height: 20px;
appearance: none;
border: 1px solid #ccc;
border-radius: 3px;
background-color: #fff;
cursor: pointer;
position: relative;
transition: all 0.3s ease;
}
/* 选中状态的样式 */
.myInputByCheckBoxStyle:checked {
background-color: #007bff;
border-color: #007bff;
}
/* 选中状态的勾选标记 */
.myInputByCheckBoxStyle:checked::before {
content: "";
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 10px;
height: 10px;
background-color: #fff;
}
/* 悬停效果 */
.myInputByCheckBoxStyle:hover {
border-color: #007bff;
}
/* 标签文本样式 */
label[for="checkbox"] {
margin-left: 5px;
font-size: 16px;
color: #333;
}
</style>
</head>
<body>
<label for="checkbox">
<input class="myInputByCheckBoxStyle" type="checkbox" name="checkbox" id="checkbox" value="是否已婚">
是否已婚
</label>
</body>
</html>
2. 效果
普通按钮 (button)
- 用途:执行JavaScript代码或其他客户端脚本。
- HTML 示例:
<button type="button" onclick="alert('Button clicked!')">Click me</button> - 特点:不提交表单,通常用来触发脚本事件。
案例:制作一个普通按钮
1. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>制作一个普通按钮</title>
<style>
.myButton{
width: 100px;
height: 50px;
background-color: #f35005;
border: none;
border-radius: 10px;
color: #fff;
font-size: 24px;
cursor: pointer;
}
.myButton:hover{
background-color: #15e215;
color: #00ccff;
}
</style>
</head>
<body>
<button class="myButton">点我</button>
</body>
</html>
2. 效果
鼠标悬浮
原始状态
提交按钮 (submit)
- 用途:将表单数据发送到服务器。
- HTML 示例:
<input type="submit" value="Submit form"> - 特点:点击后会触发表单的
onsubmit事件,并尝试提交表单数据。
案例:制作一个提交按钮
1. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>制作一个提交按钮</title>
<style>
.myButton{
width: 100px;
height: 50px;
background-color: #2ccdf5;
border: solid 5px #000;
border-radius: 10px;
color: #f70a0a;
font-size: 24px;
cursor: pointer;
}
.myButton:hover{
background-color: #0cfc04;
color: #000;
}
</style>
</head>
<body>
<button class="myButton" type="submit">提交</button>
</body>
</html>
2. 效果
鼠标悬浮
原始状态
重置按钮 (reset)
- 用途:将表单中的所有元素恢复到初始值。
- HTML 示例:
<input type="reset" value="Reset form"> - 特点:清除表单内填写的所有内容。
案例:制作一个重置按钮
1. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>制作一个重置按钮</title>
<style>
.myResetButton{
width: 100px;
height: 50px;
background-color: #ff1e00;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
.myResetButton:hover{
background-color: #4f5557;
color: #00ccff;
}
</style>
</head>
<body>
<button class="myResetButton" type="reset">重置</button>
</body>
</html>
2. 效果
原始状态
鼠标悬浮状态
文件上传框 (file)
- 用途:允许用户从本地计算机选择一个或多个文件进行上传。
- HTML 示例:
<input type="file" name="attachment"> - 特点:可以选择文件,并且可以通过表单提交到服务器。
案例:制作一个文件上传框
1. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>制作一个文件上传框</title>
<style>
.myFileStyle{
width: 200px;
height: 50px;
background-color: #a4dbe9;
border: none;
border-radius: 5px;
color: #fff;
cursor: pointer;
}
.myFileStyle:hover{
background-color: #09e002;
}
</style>
</head>
<body>
<input type="file" class="myFileStyle"></input>
</body>
</html>
2. 效果
原始状态
鼠标悬浮状态
文件上传状态
Textarea 控件
- 用途:提供多行文本输入区域。
- HTML 示例:
<textarea name="message" rows="10" cols="30"></textarea> - 特点:可以设置行数和列数来控制大小,支持大量文本输入。
案例:制作一个Textarea控件
1. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>制作一个Textarea框</title>
<style>
textarea{
width: 300px;
height: 300px;
background-color: #00ccff;
border: solid 5px #000;
border-radius: 10px;
padding: 10px;
}
</style>
</head>
<body>
<textarea name="" id="" cols="30" rows="10"></textarea>
</body>
</html>
2. 效果图
select控件
- 用途:定义一个下拉列表。
- 属性:
name:指定 select 元素的名称,用于表单提交时识别。id:为 select 元素提供一个唯一的标识符。multiple:如果设置该属性,则允许用户选择多个选项(默认是单选)。size:当使用multiple属性时,可以设置可见选项的数量。required:标记这个字段为必填项。disabled:禁用下拉列表,使其不可选择。
案例:select的简单使用
1. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>select的简单使用</title>
</head>
<body>
<select name="car" id="carSelect">
<option value="volvo">Volvo牌</option>
<option value="saab">Saab牌</option>
<option value="mercedes">Mercedes牌</option>
<option value="audi">Audi牌</option>
</select>
<hr size="5" color="red"/>
<select name="cars" id="carSelect">
<optgroup label="Swedish Cars">
<option value="volvo">Volvo牌</option>
<option value="saab">Saab牌</option>
</optgroup>
<!-- 分组 -->
<optgroup label="German Cars">
<option value="mercedes">Mercedes牌</option>
<option value="audi">Audi牌</option>
</optgroup>
</select>
</body>
</html>
2. 效果
热门推荐
精神科医生常用的10种安眠药,一文总结
江永女书:千年密语中的女性史诗
鲍鱼的四种烹饪方法,总有一种适合你
斯利安叶酸片男性是否能吃?医生专业解答
游离三碘甲状腺原氨酸偏高是什么意思
国家体育总局发布“儿童青少年科学健身20条”
南明为何会迅速走向灭亡?
兼职人员费用是入“工资”还是“劳务费”?
新手为什么养不活金鱼
盘点:冠军球队中 10 位最佳角色球员排名
从鲍文到波普:3D球员如何成为NBA争冠必备?
红葡萄酒和白葡萄酒酿造工艺的区别
儿童频繁嗳气要引起警惕
水钠潴留缓解指南:自然疗法与日常管理
常见的胃部检查方式有哪些
厂房装修之耐磨地坪VS环氧树脂应用对比
龙门翠绿茶之三前摘翠
魔兽世界:深度解析交易系统与经济生态
淀粉、纤维素和糖原:三种多糖的结构差异与功能解析
Mac/Windows/iPad/iPhone屏幕镜像一次说个明白
固态硬盘、机械硬盘工作原理和区别(内附接口知识)
步社民:重识幼儿兴趣与幼儿园课程——基于《评估指南》
“用最烂的东西做最贵的粮”,劣质猫狗粮“仅净含量是真的”
丁真爆红背后不为人知的故事:7亿中国人命运因此而改变
如何在研发团队中应对创新挑战
健康中国 | 不同人群如何制定减重目标?国家卫健委发文
复旦肿瘤医院研究突破:新方案让超半数局部晚期直肠癌患者实现肿瘤完全缓解
星露谷物语矿洞钓鱼攻略:稀有鱼类、工具与收益全指南
老公申请陪产假需要哪些证明
瘦子增肌只需要这5步,要猛就这样练!