Way to Select Text Input Fields using CSS Selectors

Last Updated : 1 Jun, 2026

Selecting text input fields using CSS selectors means targeting <input> elements of type text in a form. This allows you to apply specific styles (e.g., font, color, borders) to those fields. This is typically done using the [type="text"] CSS attribute selector.

Here we have some CSS selectors to select text input fields

Selecting Text Input Fields using Attribute Selector

The Attribute selector approach targets <input> elements with the type="text" attribute, allowing you to apply CSS styles specifically to text input fields. This is done using input[type="text"] to customize properties like borders, fonts, and colors.

Example: The Attribute selector in CSS targets input elements based on their attributes, like input[type="text"], allowing you to style only text input fields without affecting other input types.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Text Input Styling</title>
    <style>
        /* Target and style text input fields using type selector */
        input[type="text"] {
            background-color: lightyellow;
            border: 2px solid blue;
            font-size: 16px;
            padding: 10px;
            border-radius: 5px;
        }
    </style>
</head>

<body>

    <h3>Styled Text Input Field</h3>

    <input type="text" 
           placeholder="Enter your name here">
    <input type="text" 
           placeholder="Enter your email here">

</body>

</html>

Selecting Text Input Fields using ID Selector

The ID selector approach targets specific text input fields by their unique id attribute. Using #elementID, you can apply CSS styles to a particular input element rather than all text fields.

Example: The ID selector targets specific input fields (#nameInput and #emailInput), allowing for unique styling for each field independently and ensuring precise design control.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Text Input Styling with ID Selector</title>
    <style>
        /* Target and style the specific input field using ID selector */
        #nameInput {
            background-color: lightgreen;
            border: 2px solid darkgreen;
            font-size: 16px;
            padding: 10px;
            border-radius: 5px;
        }

        /* Another ID selector for a different input */
        #emailInput {
            background-color: lightblue;
            border: 2px solid navy;
            font-size: 16px;
            padding: 10px;
            border-radius: 5px;
        }
    </style>
</head>

<body>

    <h3>Styled Text Input Fields Using ID Selector</h3>

    <input type="text" 
           id="nameInput" 
           placeholder="Enter your name">
    <input type="text" 
           id="emailInput" 
           placeholder="Enter your email">

</body>

</html>

Selecting Text Input Fields using Class Selector

The Class selector approach targets multiple text input fields that share the same class attribute. This is done using .class-name in CSS, allowing you to apply consistent styles to multiple elements simultaneously.

Example: The Class selector .input-field is used to style multiple input fields consistently, applying the same margin, font size, padding, border-radius, and width.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Text Input Styling with Class Selector</title>
    <style>
        /* Target and style input fields using class selector */
        .input-field {
            margin: 2px;
            font-size: 16px;
            padding: 10px;
            border-radius: 5px;
            width: 50%;
        }
    </style>
</head>

<body>

    <h3>Styled Text Input Fields Using Class Selector</h3>

    <input type="text" 
           class="input-field" 
           placeholder="Enter your name">
    <input type="text" 
           class="input-field" 
           placeholder="Enter your email">
    <input type="text" 
           class="input-field" 
           placeholder="Enter your address">

</body>

</html>
Comment