Make input field readonly without changing background colour in CSS or JavaScript
Asked Answered
J

5

17

I would like to make a HTML input field readonly but without getting the grey background that appears when I just add the attribute readonly (it should look the same as a normal field just not allow editing).

Is there a way I can realise this in CSS and/or JS, ideally with a class ?

Example field:

<input type="text" style="width:96%" class="myClass" />
Joesphjoete answered 4/2, 2014 at 10:47 Comment(10)
possible duplicate of how to disable keyboard events on an input text box in javascriptSleep
readonly="readonly", does not make the input grayed.Gerontocracy
@ParthoGanguly I think his means disabled true..?Raoul
@Sleep Wrong duplicate vote :)Debenture
@Mr.Alien: Apparently I was over-thinking it xDSleep
@GrijeshChauhan- No, there is difference between readonly, and disabled.Gerontocracy
@ParthoGanguly readonly is graying out in chrome. see: jsfiddle.net/Valtos/w8tgCAvril
@ManuelRicharz - I don't find any difference, and I am using Chrome only.Gerontocracy
readonly doesn't graying out in chrome... and even if it does... you can use background: white; to override it in readonly or disabled inputsPainty
@ParthoGanguly see my screen: directupload.net/file/d/3523/8wdt76zg_png.htmAvril
D
10

Yes, you can use the :disabled pseudo class, e.g.

input.myClass:disabled {
    /* style declaration here */
}
Dalhousie answered 4/2, 2014 at 10:54 Comment(0)
M
25

I think you mistook disabled and readonly

<input type="text" value="readonly" readonly>
<input type="text" value="disabled" disabled>

have a look to this fiddle : http://jsfiddle.net/36UwE/

As you can see, only the disabled input is grey (tested on Windows with latest Chrome & IE)

However, this may differ, according the browser, operating system and so on. You can use custom the display with css:

input[readonly] {
  background-color: white;
}
Mousseline answered 4/2, 2014 at 10:56 Comment(3)
define a background: white; to set them both the same stylePainty
The main difference between readonly and disabled is that readonly field are sent when the form is submitted, not the disabled ones.Mousseline
Bootstrap 4 (at least) sets the background colour for read only inputs to a grey colour. Solution (as per @Painty above) is to use <input readonly class="bg-white form-control" value="..." />Clothier
D
10

Yes, you can use the :disabled pseudo class, e.g.

input.myClass:disabled {
    /* style declaration here */
}
Dalhousie answered 4/2, 2014 at 10:54 Comment(0)
D
5
<input type="text" class="form-control" value="MyText" readonly />

<style>
    input.form-control:read-only {
        background-color: #fff;
    }
</style>

or

<input type="text" class="form-control" value="MyText" style="background-color: #fff;" readonly />

More info here.

Dela answered 29/12, 2019 at 14:7 Comment(0)
N
3

While you can apply any styling using :disabled, :readonly or .myClass CSS selectors, be aware that different browsers/platforms will render the standard, readonly & disabled input fields differently, so trying to override color, borders and background based on the defaults for your platform (e.g. Windows) might break the styling on other platforms (e.g. Mac).

It's usually not a good idea to override the default styling cues that users expect, but if you must do this then you should ensure that readonly/disabled input fields are visually discernible from standard ones, and use custom styling that is not platform-specific.

Neuritis answered 4/2, 2014 at 11:1 Comment(0)
M
0

This works for all input fields:

input:read-only {
    background: whitesmoke;
}
Marjorie answered 27/7, 2023 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.