How can I target input fields of type 'text' using CSS selectors?
8 Answers
input[type=text]
or, to restrict to text inputs inside forms
form input[type=text]
or, to restrict further to a certain form, assuming it has id myForm
#myForm input[type=text]
Notice: This is not supported by IE6, so if you want to develop for IE6 either use IE7.js (as Yi Jiang suggested) or start adding classes to all your text inputs.
Reference: http://www.w3.org/TR/CSS2/selector.html#attribute-selectors
Because it is specified that default attribute values may not always be selectable with attribute selectors, one could try to cover other cases of markup for which text inputs are rendered:
input:not([type]), /* type attribute not present in markup */
input[type=""], /* type attribute present, but empty */
input[type=text] /* type is explicitly defined as 'text' */
Still this leaves the case when the type is defined, but has an invalid value and that still falls back to type="text". To cover that we could use select all inputs that are not one of the other known types
input:not([type=button]):not([type=password]):not([type=submit])...
But this selector would be quite ridiculous and also the list of possible types is growing with new features being added to HTML.
Notice: the :not
pseudo-class is only supported starting with IE9.

- 23,478
- 6
- 59
- 81

- 43,655
- 12
- 77
- 90
-
50+1 for citing the actual standard rather then some tutorial web-site – Šime Vidas Nov 06 '10 at 16:35
-
Does it work in IE6? For a cross-browser solution i tend to prefer to add classes (.input-text, .input-submit, etc) it sucks for html development, but it makes the css and javascript a little nicer. – zzzzBov Nov 06 '10 at 17:18
-
It doesn't in IE6. But I suggest going with IE7.js as Yi Jiang said in his answer. Of course this may not always be possible so in some limit cases adding classes is the only solution. – Alin Purcaru Nov 06 '10 at 17:22
-
My 2 cents: The question is about forms and w3.org doesn't have form selector examples. Granted, the same rules apply but real-world examples would help: form classes, input.someclass, etc. I ended up just using Firebug to find the selector I wanted, owell. – PJ Brunet Dec 24 '12 at 00:09
-
Apparently, this doesn't work for IE10, Chrome (...), see: http://stackoverflow.com/questions/17699492/how-can-i-target-text-input-fields-with-css/17699597?noredirect=1#17699597 for a way to solve this. – Peter Jul 17 '13 at 12:28
-
It works, but officially you need the quotation marks around the actual type: input[type="text"]. See also the answer below. – Wilt Aug 30 '13 at 07:45
-
@Wilt Not according to this documentation: http://www.w3.org/TR/CSS2/selector.html#attribute-selectors ...as far as I know that is the _official_ one. – Alin Purcaru Aug 30 '13 at 10:27
-
doesn't work if type="text" is not specified within input as the text is default some developer like myself may not have used type="text" attribute. What is the workaround other than adding type attribute in all the text inputs? – Mubashar Sep 04 '13 at 07:20
-
1@MubasharAhmad I've updated my answer and as you can see there is a workaround only if you're targeting browsers above IE9. – Alin Purcaru Sep 04 '13 at 08:09
-
If you use lint as style guide, `input[type="text"]` raises a warning. See this: https://github.com/brigade/scss-lint/blob/master/lib/scss_lint/linter/README.md Is there any other way? – yiwen Oct 11 '15 at 18:03
You can use the attribute selector here:
input[type="text"] {
font-family: Arial, sans-serif;
}
This is supported in IE7 and above. You can use IE7.js to add support for this if you need to support IE6.
See: http://reference.sitepoint.com/css/attributeselector for more information
I usually use selectors in my main stylesheet, then make an ie6 specific .js (jquery) file that adds a class to all of the input types. Example:
$(document).ready(function(){
$("input[type='text']").addClass('text');
)};
And then just duplicate my styles in the ie6 specific stylesheet using the classes. That way the actual markup is a little bit cleaner.

- 165
- 3
In jQuery you can use the :text
selector to select all inputs with type text. See the working Fiddle here.
$(document).ready(function () {
$(":text").css({ // or $("input:text")
'background': 'green',
'color':'#fff'
});
});
:text
is a jQuery extension and not part of the CSS specification. Queries using :text
cannot take advantage of the performance boost provided by the native DOM querySelectorAll()
method. For better performance in modern browsers, use [type="text"]
instead. This will work for IE6+
.
$("[type=text]").css({ // or $("input[type=text]")
'background': 'green',
'color':'#fff'
});
For regular CSS, use the following:
[type=text] { /* or input[type=text] */
background: green;
}

- 2,832
- 2
- 23
- 31

- 6,683
- 8
- 47
- 70
As @Aamir mentions, the best way nowadays – cross-browser and leaving behind IE6 – is to use this:
[type=text] {}
Nobody mentioned lower CSS specificity (why is that important?) yet. [type=text]
features 0,0,1,0 instead of 0,0,1,1 with input[type=text]
.
Performance-wise there's no negative impact at all any more.
normalize v4.0.0 just released with lowered selector specificity.
I wanted to style a text input field inside a table row. I did it with this code:
.admin_table input[type=text]:focus {
background-color: #FEE5AC;
}

- 2,832
- 2
- 23
- 31

- 471
- 5
- 10
input[type="text"]
This will select all the input type text in a web-page.

- 623
- 1
- 6
- 16
With attribute selector we target input type text in CSS
input[type=text] {
background:gold;
font-size:15px;
}

- 12,002
- 3
- 36
- 36