Home >Web Front-end >CSS Tutorial >How to select text input field using CSS selector?
Selecting text input fields using CSS selectors is a powerful and crucial tool for styling and targeting the specific elements on the webpage. Text input fields are an essential part of any web form that requires users to provide input. As a web developer or designer, we may need to select text input fields using CSS selectors to apply styling to them. If we want to change the font color, background color, or add custom styles to the input fields, it is important to understand how to select them using CSS selectors.
Before using CSS selectors to select a text input field, it is important to understand its structure. A text input field is usually represented by an HTML element with its type attribute set to "text". For example, the following HTML code creates a text input field.
<input type="text" name="user-name" id="user-id" value="initial-value" placeholder="Enter username >
In the above code -
type="text" Specifies that this is a text input field.
name="user-name"Set the attributes of the input field to identify the input when the form is submitted.
id="user-id" Sets the ID attribute of the input field, which is used to identify the input field for styling and script processing.
value="initial-value"Set the initial value of the input field. This is optional and can be omitted.
placeholder="Enter user name" Set a placeholder text to be displayed in the input box to give the user an input prompt.
An easy way to select all text input fields on a web page is to use an element selector. Text input fields are HTML elements that allow users to enter text, such as their name, email address, or password. These elements are typically created using the "input" tag with the "type" attribute set to "text", "email", "password", or "search". To select all text input fields we can use the following CSS selector −
input[type="text"], input[type="email"], input[type="password"] { /* write your CSS Code */ }
This selector targets all input fields that have the "type" attribute set to "text", "email", or "password". A comma between selectors means that all selectors will receive the same style.
In this example, we use the type attribute selector to select all text input fields in the form. CSS styles are applied to all text input fields with type="text", type="email" and type="password" attributes. These input fields have border, padding, font size, and bottom margin styles applied.
<!DOCTYPE html> <html> <head> <style> body { text-align: center; } input[type="text"], input[type="email"], input[type="password"] { border: 2px solid lightgray; padding: 12px; font-size: 18px; margin-bottom: 15px; } </style> </head> <body> <h3>Selecting text input fields using type attribute</h3> <form> <label for="name">Name:</label> <input type="text" id="name" name="name" placeholder="Enter username"><br> <label for="email">Email:</label> <input type="email" id="email" name="email" placeholder="Enter Email ID"><br> <label for="password">Password:</label> <input type="password" id="password" name="password" placeholder="Enter your Password"><br> </form> </body> </html>
We can use ID selectors to target specific text input fields. The ID selector is represented by the "#" character, followed by the value of the ID attribute of the HTML element. For example, if we have an HTML element with the ID attribute set to "username", we can select it using the following CSS selector -
#username { /* Write CSS rules here */ }
In this example, we use the ID attribute selector to select three text input boxes with IDs name, email and password. These CSS styles are applied to the three input boxes. The border-radius and background-color styles are applied to these input boxes.
<!DOCTYPE html> <html> <head> <style> body { text-align: center; } input[type="text"], input[type="email"],input[type="password"] { border: 2px solid lightgray; padding: 12px; font-size: 18px; margin-bottom: 15px; } #name, #email{ background-color: lightgreen; border-radius:10px; } </style> </head> <body> <h3>Selecting text input fields using ID attribute</h3> <form> <label for="name">Name:</label> <input type="text" id="name" name="name" placeholder="Enter username"><br> <label for="email">Email:</label> <input type="email" id="email" name="email" placeholder="Enter Email ID"><br> <label for="password">Password:</label> <input type="password" id="password" name="password" placeholder="Enter your Password"><br> </form> </body> </html>
If we have multiple text input fields with similar style or functionality, we can use class selectors to locate them. The class selector is represented by the "." character, followed by the value of the HTML element's class attribute. For example, if we have multiple HTML elements with the class attribute set to "input-field", we can use the following CSS selector to select them -
.input-field { /* write CSS rules here */ }
This selector targets all HTML elements with the class attribute set to "input-field".
In this example, we use the Class attribute selector to select three text input boxes with class names name, email, and password. These CSS styles are applied to the three input boxes. The border-radius and background-color styles are applied to these input boxes.
<!DOCTYPE html> <html> <head> <style> body { text-align: center; } input[type="text"], input[type="email"], input[type="password"]{ border: 2px solid lightgray; padding: 12px; font-size: 18px; margin-bottom: 15px; } .password, .search{ background-color: lightgreen; border-radius:10px; } </style> </head> <body> <h3>Selecting text input fields using ID attribute</h3> <form> <label for="name">Name:</label> <input type="text" id="name" name="name" placeholder="Enter username"><br> <label for="email">Email:</label> <input type="email" id="email" name="email" placeholder="Enter Email ID"><br> <label for="password">Password:</label> <input type="password" class="password" id="password" name="password" placeholder="Enter your Password"><br> </form> </body> </html>
Selecting a text input field using CSS selectors is a simple process once we understand the structure of a text input field and the different CSS selectors available. By using appropriate CSS selectors, we can easily position and style text input fields to enhance the user experience of web forms.
The above is the detailed content of How to select text input field using CSS selector?. For more information, please follow other related articles on the PHP Chinese website!