A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/css/how-to-select-text-input-fields-using-css-selector/ below:

How to Select Text Input Fields using CSS Selector ?

How to Select Text Input Fields using CSS Selector ?

Last Updated : 11 Jul, 2025

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 selector to select text input fields

Selecting Text Input Fields using type Selector

The type 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: Here The type selector in CSS targets specific input types, 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>

Output:

Selecting Text Input Fields using type Selector 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>

Output:

Selecting Text Input Fields using id Selector 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: Here 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>

Output:

Selecting Text Input Fields using class Selector Selecting Text Input Fields using attribute Selector

The attribute selector targets text input fields based on their attributes, such as type="text". Using input[type="text"], you can apply specific styles to all input fields that match this attribute.

Example: The attribute selector input[type="text"] is used to specifically target and style all text input fields, applying consistent margins, font size, padding, 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 Attribute Selector</title>
    <style>
        /* Target and style input fields using attribute selector */
        input[type="text"] {
            margin: 2px;
            font-size: 16px;
            padding: 10px;
            border-radius: 5px;
            width: 50%;
        }
    </style>
</head>

<body>

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

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

</body>

</html>

Output:

Selecting Text Input Fields using attribute Selector

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4