Để đảm bảo độ tin cậy, tôi khuyên bạn nên đặt tên lớp hoặc ids cho các thành phần để tạo kiểu (lý tưởng là classcho đầu vào văn bản, vì có lẽ sẽ có một số) và idnút gửi (mặc dù classcũng sẽ hoạt động):
<form action="#" method="post">
<label for="text1">Text 1</label>
<input type="text" class="textInput" id="text1" />
<label for="text2">Text 2</label>
<input type="text" class="textInput" id="text2" />
<input id="submitBtn" type="submit" />
</form>
Với CSS:
.textInput {
/* styles the text input elements with this class */
}
#submitBtn {
/* styles the submit button */
}
Đối với các trình duyệt cập nhật hơn, bạn có thể chọn theo các thuộc tính (sử dụng cùng một HTML):
.input {
/* styles all input elements */
}
.input[type="text"] {
/* styles all inputs with type 'text' */
}
.input[type="submit"] {
/* styles all inputs with type 'submit' */
}
Bạn cũng có thể chỉ sử dụng các tổ hợp anh chị em (vì các kiểu nhập văn bản để tạo kiểu dường như luôn tuân theo một labelphần tử và trình gửi đi theo một textarea (nhưng điều này khá dễ vỡ)):
label + input,
label + textarea {
/* styles input, and textarea, elements that follow a label */
}
input + input,
textarea + input {
/* would style the submit-button in the above HTML */
}