响应式表单html源码
时间: 2023-09-19 16:02:12 浏览: 49
响应式表单是一种适应不同屏幕尺寸和设备类型的表单。下面是一个简单的响应式表单的HTML源码:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>响应式表单</title>
<style>
/* 样式部分可以根据实际需要进行自定义调整 */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
}
input[type="text"],
input[type="email"],
select {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
@media screen and (min-width: 600px) {
/* 在大屏幕上进行调整 */
.form-group {
display: grid;
grid-template-columns: 1fr 1fr;
column-gap: 20px;
}
label {
margin-bottom: 0;
text-align: right;
}
input[type="text"],
input[type="email"],
select {
width: 100%;
}
}
</style>
</head>
<body>
<h1>响应式表单</h1>
<form>
<div class="form-group">
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="phone">电话:</label>
<input type="text" id="phone" name="phone" required>
</div>
<div class="form-group">
<label for="message">消息:</label>
<textarea id="message" name="message" rows="5" required></textarea>
</div>
<button type="submit">提交</button>
</form>
</body>
</html>
```
以上的HTML源码展示了一个简单的响应式表单。在小屏幕上,表单元素会保持100%宽度,依次排列。而在大屏幕上,表单元素会根据网格布局进行调整,每行两个表单元素。这样,无论用户使用何种设备访问页面,都能够正常显示和使用表单。