CSS 表单选择器用于选择和样式化 HTML 表单元素,如输入框、选择框、按钮等。通过使用不同的选择器,可以精确地控制这些元素的样式。
input
:选择所有输入框。select
:选择所有选择框。button
:选择所有按钮。.classname
:选择具有指定类名的元素。.classname
:选择具有指定类名的元素。.classname
:选择具有指定类名的元素。#idname
:选择具有指定 ID 的元素。#idname
:选择具有指定 ID 的元素。#idname
:选择具有指定 ID 的元素。[attribute]
:选择具有指定属性的元素。[attribute]
:选择具有指定属性的元素。[attribute]
:选择具有指定属性的元素。!important
标记来强制应用样式,但不推荐频繁使用。!important
标记来强制应用样式,但不推荐频繁使用。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Form Selectors</title>
<style>
input, select, button {
padding: 10px;
margin: 5px;
}
.input-field {
border: 1px solid #ccc;
}
#username {
border: 2px solid blue;
}
[type="email"] {
border: 1px solid green;
}
input:invalid {
border-color: red;
}
@media (max-width: 600px) {
input, select, button {
width: 100%;
}
}
input[type="checkbox"]:checked + label::before {
content: "✔";
color: green;
}
</style>
</head>
<body>
<form>
<input type="text" class="input-field" placeholder="Enter text">
<input type="email" id="username" name="user_email" required>
<select>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
</select>
<label><input type="checkbox"> Agree to terms</label>
<button type="submit">Submit</button>
</form>
</body>
</html>
领取专属 10元无门槛券
手把手带您无忧上云