Background color in input and text fields
Asked Answered
L

4

107

I would like to change the color background in the text and input fields of a form, but when I do this it also affects the submit button! Could it be done in some other way that does not affect the button?

I have used this code:

input, textarea {
  background-color: #d1d1d1;
}
Lamontlamontagne answered 11/4, 2011 at 6:47 Comment(1)
As @Damien-at-SF said, use input[type="text"] instead of input. But also use border: none; rule to avoid default text input borders.Unintelligible
V
185
input[type="text"], textarea {

  background-color : #d1d1d1; 

}

Edit: working example, http://jsfiddle.net/C5WxK/

Veliavelick answered 11/4, 2011 at 6:50 Comment(6)
Nice! There is a border effect in the input field, but not the textarea?Lamontlamontagne
Why did you use both input[type="text"] and textarea ?Alainaalaine
They are 2 separate tags and need to be addressed separately... input[type="text"] doesn't address textarea fields.Veliavelick
Chrome autofill broke it for me. This answer can fix that: #2782049Complected
For some reason, my input fields are only being stylized the moment the page renders. Then, it fades.Aga
Why does this change the size of my text field?Volumed
C
10

The best solution is the attribute selector in CSS (input[type="text"]) as the others suggested.

But if you have to support Internet Explorer 6, you cannot use it (QuirksMode). Well, only if you have to and also are willing to support it.

In this case your only option seems to be to define classes on input elements.

<input type="text" class="input-box" ... />
<input type="submit" class="button" ... />
...

and target them with a class selector:

input.input-box, textarea { background: cyan; }
Clerical answered 11/4, 2011 at 6:54 Comment(0)
K
3

You want to restrict to input fields that are of type text so use the selector input[type=text] rather than input (which will apply to all input fields (e.g. those of type submit as well)).

Koch answered 11/4, 2011 at 6:50 Comment(0)
M
0

you can simply use the button tag with type="submit"

<button type="submit">Submit</button>

besides solving your problem, now you can put HTML inside it(icons for example), rather than using the value attribute to set the text.

If you need to insert a normal button inside a form, use type="button" to prevent it submitiing the form

Microscopic answered 27/1, 2023 at 20:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.