161

I'm currently using readonly="readonly" to disable fields. I'm now trying to style the attribute using CSS. I've tried using

input[readonly] {
  /* styling info here */
}

but it is not working for some reason. I've also tried

input[readonly='readonly'] {
  /* styling info here */
}

that doesn't work either.

How can I style the readonly attribute with CSS?

tanguy_k
  • 11,307
  • 6
  • 54
  • 58
Daphne
  • 1,727
  • 3
  • 11
  • 7
  • 14
    `input[readonly]` should work. Make sure it's not being overridden by other, more specific selectors elsewhere in your stylesheet. – BoltClock Mar 09 '12 at 14:04
  • 1
    if you want to disable, then use the disabled attribute and not readonly – Sven Bieder Mar 09 '12 at 14:16
  • I noticed you miss an apostrophe in second selector. Should be input[readonly='readonly'] and it will work as well as input[readonly]. – vlad saling Mar 09 '12 at 14:18
  • 5
    @SvenBieder Readonly and disabled have different behavior altogether. Readonly fields sent to the server on form submit while disabled fields are not. – Naren Mar 09 '12 at 14:21
  • 5
    `input[readonly='readonly']` will only work if you use HTML like ``, not for e.g. ``. `input[readonly]` should match both. – Mathias Bynens Mar 09 '12 at 14:22
  • just an FYI, IE6/7 won't support this attribute selector- only IE8+ – Matt K Mar 09 '12 at 14:24
  • @Matt K: IE7 is fine with attribute selectors for stuff like `readonly`. It only messes up in some corner cases (e.g. getting DOM properties and HTML attributes mixed up). – BoltClock Mar 09 '12 at 14:25
  • @BoltClock: sorry, attached this comment to the wrong part. Curt's answer below won't work in IE6/7. it doesn't recognize `input[readonly="readonly"]`, it must be only `input[readonly]`. – Matt K Mar 09 '12 at 14:35
  • @SvenBieder Yeah, I know it doesn't work the same as disabled. I have to use readonly because I want the form to submit the same values but prevent users from editing the field. I've used jQuery to prevent the readonly value from focusing so it's like disabled but you can submit values. – Daphne Mar 09 '12 at 14:40
  • @vladsaling Sorry, that was a typo. I do have the apostrophe in the second selector. Have amended the question description. – Daphne Mar 09 '12 at 14:41
  • @BoltClock I think this might be the reason. I use `class="setting"` to modify the settings fields in the form to make it different from the normal input value. I just tried using `!important` next to the readonly class in attempt prioritize the readonly class but it doesn't work. How else can I have both the settings class and readonly class present because only some fields are readonly and can't use readonly for all the fields. – Daphne Mar 09 '12 at 14:45
  • 1
    `!important` should work to override, unless you also have `!important` on the setting class? – Matt K Mar 09 '12 at 14:45
  • @Daphne oh, ok then :) what styles exactly your are trying to apply? not every input element support every css attribute... – vlad saling Mar 09 '12 at 14:46
  • @MattK Hmm, no I don't have `!important` on the settings class. My code is as follows: `input[readonly] !important { color: red; } input.setting { font-family: Verdana, Helvetica, sans-serif; font-size: 11px; color: #676563; background: #d5cec5; border: 0px; margin: 3px; padding: 8px; height: 18px; width: 200px; -moz-border-radius: 5px; -webkit-border-radius: 5px; } input:focus.setting { color: #555; background-color: #f5f1eb; }` – Daphne Mar 09 '12 at 14:50
  • Could you edit that into your question, please? I also notice a stray `!important` after your selector - was that a mistake in your comment? – BoltClock Mar 09 '12 at 15:13
  • You can target attribute selectors in ie8 if the doctype is HTML5. caniuse.com – Ben Racicot Oct 17 '14 at 17:12

9 Answers9

214
input[readonly]
{
    background-color:blue;
}

https://curtistimson.co.uk/post/css/style-readonly-attribute-css/

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • 27
    You should use `input[readonly]` instead as `readonly` is a Boolean attribute that's not designed to have a specific value. – BoltClock Apr 22 '13 at 15:43
  • 1
    @BoltClock Yes. Please see the resolution in the Answer below. – Pal R Apr 22 '13 at 15:53
  • How can I make it writeable again? Just removing the class style? – Renaro Santos Jan 06 '14 at 13:00
  • @RenaroSantos Thats a HTML change. Remove `readonly="readonly"` from your `input` element. – Curtis Jan 06 '14 at 16:15
  • w/ IE7 you can still use the selector with jQuery – ladieu Apr 30 '14 at 17:55
  • perhaps to fix the answer according to the @BoltClock's comment (for pleople that does not visit the link bellow)? – serge Jun 11 '15 at 09:37
  • does not correspont to the snippet, but is better now ;) – serge Jun 11 '15 at 10:01
  • finally, the answer is identical to the question! ;) – serge Jun 11 '15 at 10:06
  • I have tried all the possible solutions and none of them work on iOS :( specifically I wanted to adjust the color property, that is by default a shade of light grey. edit to my previous comment, it actually works on iOS as well, but the color is dimmed a little bit, therefore the text color doesn't get the exact color set in CSS. – bboydflo Aug 15 '17 at 10:04
71

Note that textarea[readonly="readonly"] works if you set readonly="readonly" in HTML but it does NOT work if you set the readOnly-attribute to true or "readonly" via JavaScript.

For the CSS selector to work if you set readOnly with JavaScript you have to use the selector textarea[readonly].

Same behavior in Firefox 14 and Chrome 20.

To be on the safe side, i use both selectors.

textarea[readonly="readonly"], textarea[readonly] {
...
}
kaka
  • 719
  • 5
  • 2
  • 20
    You may as well just use `textarea[readonly]` instead - every `textarea[readonly="readonly"]` is guaranteed to match that. – BoltClock Apr 22 '13 at 15:43
27

Loads of answers here, but haven't seen the one I use:

input[type="text"]:read-only { color: blue; }

Note the dash in the pseudo selector. If the input is readonly="false" it'll catch that too since this selector catches the presence of readonly regardless of the value. Technically false is invalid according to specs, but the internet is not a perfect world. If you need to cover that case, you can do this:

input[type="text"]:read-only:not([read-only="false"]) { color: blue; }

textarea works the same way:

textarea:read-only:not([read-only="false"]) { color: blue; }

Keep in mind that html now supports not only type="text", but a slew of other textual types such a number, tel, email, date, time, url, etc. Each would need to be added to the selector.

IAmNaN
  • 10,305
  • 3
  • 53
  • 51
  • 2
    Neither do I. LOL Yes, you are correct! For now. `:read-only` and other psuedo selectors have been added to the W3C standard and can be used with IE 10 preview builds 10547 and higher. – IAmNaN Oct 09 '15 at 21:06
22

To be safe you may want to use both...

input[readonly], input[readonly="readonly"] {
    /*styling info here*/
}

The readonly attribute is a "boolean attribute", which can be either blank or "readonly" (the only valid values). http://www.whatwg.org/specs/web-apps/current-work/#boolean-attribute

If you are using something like jQuery's .prop('readonly', true) function, you'll end up needing [readonly], whereas if you are using .attr("readonly", "readonly") then you'll need [readonly="readonly"].


Correction: You only need to use input[readonly]. Including input[readonly="readonly"] is redundant. See https://stackoverflow.com/a/19645203/1766230

Community
  • 1
  • 1
Luke
  • 18,811
  • 16
  • 99
  • 115
7

There are a few ways to do this.

The first is the most widely used. It works on all major browsers.

input[readonly] {
 background-color: #dddddd;
}

While the one above will select all inputs with readonly attached, this one below will select only what you desire. Make sure to replace demo with whatever input type you want.

input[type="demo"]:read-only {
 background-color: #dddddd;
}

This is an alternate to the first, but it's not used a whole lot:

input:read-only {
 background-color: #dddddd;
}

The :read-only selector is supported in Chrome, Opera, and Safari. Firefox uses :-moz-read-only. IE doesn't support the :read-only selector.

You can also use input[readonly="readonly"], but this is pretty much the same as input[readonly], from my experience.

Matthew Campbell
  • 1,118
  • 14
  • 25
4
input[readonly], input:read-only {
    /* styling info here */
}

Shoud cover all the cases for a readonly input field...

serge
  • 13,940
  • 35
  • 121
  • 205
  • input:read-only is the "mutability pseudo-class aiming at making form styling easier based on disabled, readonly and contenteditable HTML Attributes" As mentioned here, https://css-tricks.com/almanac/selectors/r/read-write-read/, various implementations are quite wonky. – peater Jan 11 '16 at 14:26
  • This is strange but only input[readonly] worked for me in FF 58 – Pavel_K Mar 11 '18 at 14:17
3

capitalize the first letter of Only

input[readOnly] {
      background: red !important;
    }
<input type="text" name="country" value="China" readonly="readonly" />
王小陌
  • 31
  • 3
2

If you select the input by the id and then add the input[readonly="readonly"] tag in the css, something like:

 #inputID input[readonly="readonly"] {
     background-color: #000000;
 }

That will not work. You have to select a parent class or id an then the input. Something like:

 .parentClass, #parentID input[readonly="readonly"] {
     background-color: #000000;
 }

My 2 cents while waiting for new tickets at work :D

softwareplay
  • 1,379
  • 4
  • 28
  • 64
  • "If you select the input by the id" – which the OP is not, so how is this relevant? Even if it was the case, you're wrong, you just need to remove the descendant combinator. – Quentin Feb 05 '15 at 10:07
  • AH ok you mean i have to type something like input[readonly="readonly"]#inputID ? /me fool.. you're right – softwareplay Feb 05 '15 at 14:33
1

Use the following to work in all browsers:

 var readOnlyAttr = $('.textBoxClass').attr('readonly');
    if (typeof readOnlyAttr !== 'undefined' && readOnlyAttr !== false) {
        $('.textBoxClass').addClass('locked');
    }
Pal R
  • 534
  • 4
  • 11