-2

The pseudo-class :read-only should apply to any element that does not match the :read-write selector, which means, any element that is not editable.

Well then, why is it that when I add disabled to an input element, the :read-only pseudo-class doesn't apply to it?

.pseudo-test input:read-write {
  color: blue;
}
.pseudo-test input:read-only {
  color: red;
}
<div style="margin-top:10px" class="pseudo-test">
  <form action="another-action.php">
    <input type="search" value="What do you want to search for?" size="100" disabled>
  </form>
</div>

results in: enter image description here

Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68
  • 1
    `disabled` and `readonly` are different, you can't use this `attr` as selector until you add this to element, simple math – Abhishek Pandey Jul 06 '20 at 11:24
  • I know they're different, but the description of "read-only" pseudo-class is that it applies to any element that cannot be editable. Well, "disabled" elements are not editable, so why doesn't it apply to it? And what "attr" are you talking about? –  Jul 06 '20 at 11:27

2 Answers2

1

readonly and disabled are different, that's why :read-only will not work with disabled.

Check here Best Practice - ReadOnly vs Disabled in Form Controls

Now your next question: "...but the description of "read-only" pseudo-class is that it applies to any element that cannot be editable..."

One use of readonly form controls is to allow the user to check and verify information that they may have entered in an earlier form (for example, shipping details), while still being able to submit the information along with the rest of the form...

Source :read-only

If you connect 1st answer and the highlighted description here you can see, it won't be working for disabled (The value of the "disabled" control will not be submitted with the form)

What if you have both types of control in form and you only wants to style read-only. Then it won't be possible with one fits all approach

To style readonly you may need to add readonly attribute to the desired elements.

.pseudo-test input:read-write {
  color: blue;
}
.pseudo-test input:read-only {
  color: red;
}
<div style="margin-top:10px" class="pseudo-test">
  <form action="another-action.php">
    <input type="search" value="What do you want to search for?" size="100" readonly>
  </form>
</div>
Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68
  • This is a precise and detailed answer so I don’t know why the downvote... I guess to OP just doesn’t like that readonly doesn’t mean what they want it to mean :) Have an upvote! – FluffyKitten Jul 06 '20 at 12:00
  • 1
    @TemaniAfif which part? The quoted explanations from the sources are correct, unless the interpretation is wrong? – FluffyKitten Jul 06 '20 at 12:04
  • @FluffyKitten what is highlighted from the sources is not relevant to the actual issue. Adding readonly attribute is not needed to trigger the :read-only selector (see my answer below) – Temani Afif Jul 06 '20 at 12:05
  • @TemaniAfif It's in a reference to input or form control not text. – Abhishek Pandey Jul 06 '20 at 12:07
  • :read-only selector apply to any elements and what the OP is having is a bug. Test his code in Firefox and you will see the text in red which will make your explanation wrong because we don't need readonly attribute to trigger :read-only selector. The disabled attribute should also trigger read-only – Temani Afif Jul 06 '20 at 12:10
  • 1
    Your answer only proves that readonly and disabled attributes are different. At no point do you ever explain why is the pseudo-class ":read-only" not working for "disabled" elements. Nowhere in the "read-only" pseudo-class explanation does it say that the class does not work for elements that are not editable if their content is not submitted. –  Jul 06 '20 at 12:17
  • 1
    Maybe it's a bug or maybe chrome rendering it in a right, though I gone through the doc you shared, I can't find any support that it should work with disabled, anyway I'll edit my answer, thank you for pointing out – Abhishek Pandey Jul 06 '20 at 12:18
  • 1
    @FluffyKitten because this answer does not explain why the pseudo-class "read-only" does not work for disabled elements. This answer only explains why readonly and disabled attributes are different. –  Jul 06 '20 at 12:19
  • @TemaniAfif Fair enough. But even if it’s the spec, in practical terms Chrome is the most widely used browser in the world, so if it doesn’t support it the OP is going to have to deal with the way it *does* work rather than *should* work. :) – FluffyKitten Jul 06 '20 at 12:19
  • 1
    @IloveCoffee because it's not supposed to work with it – Abhishek Pandey Jul 06 '20 at 12:20
  • @FluffyKitten we should explain to the OP why it's not working. This answer is saying that :read-only works only if you add the readonly attribute which is wrong BUT since it's a bug, you can consider the readonly attribute to make it working on Google Chrome (which is a different story) – Temani Afif Jul 06 '20 at 12:21
  • @AbhishekPandey the OP cant downvote your answer because he doesn't have the required Rep for it, you need at least 150 Rep to downvote – Temani Afif Jul 06 '20 at 12:29
  • 1
    @TemaniAfif I did not complaint for the downvote, least matter, I just wanted to help him, if the answer is incorrect it should be updated by comments the way you did, collaboration is key of SO, but OP is in the mood of personal attacks :D – Abhishek Pandey Jul 06 '20 at 12:34
1

If you test in Firefox, you will see your code working fine so I assume it's a bug or a lack of support for Google Chrome

.pseudo-test input:read-write {
  color: blue;
}
.pseudo-test input:read-only {
  color: red;
}
<div style="margin-top:10px" class="pseudo-test">
  <form action="another-action.php">
    <input type="search" value="What do you want to search for?" size="100" disabled>
  </form>
</div>

enter image description here

To confirm that it should work fine, you can see in the specification:

The :read-write pseudo-class must match any element falling into one of the following categories, which for the purposes of Selectors are thus considered user-alterable: [SELECTORS]

  • input elements to which the readonly attribute applies, and that are mutable (i.e. that do not have the readonly attribute specified and that are not disabled)
  • textarea elements that do not have a readonly attribute, and that are not disabled
  • elements that are editing hosts or editable and are neither input elements nor textarea elements

The :read-only pseudo-class must match all other HTML elements.

So :read-write should apply to input if it doesn't have readonly and disabled otherwise the :read-only apply (like in your case)


The same issue happen with textarea (working on Firefox and not Chrome)

.pseudo-test textarea:read-write {
  color: blue;
}
.pseudo-test textarea:read-only {
  color: red;
}
<div style="margin-top:10px" class="pseudo-test">
  <form action="another-action.php">
    <textarea  disabled>What do you want to search for?</textarea>
  </form>
</div>

It works fine for both with non-form elements:

.pseudo-test p:read-write {
  color: blue;
}
.pseudo-test p:read-only {
  color: red;
}
<div style="margin-top:10px" class="pseudo-test">
  <form action="another-action.php">
    <p >What do you want to search for?</p>
  </form>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • 3
    @IloveCoffee, please review the [code of conduct](https://stackoverflow.com/conduct#unfriendly-language) for this site and refrain from making personal or rude comments as it is not acceptable and could trigger a disciplinary action. – FluffyKitten Jul 06 '20 at 12:47
  • 1
    @IloveCoffee don't reply to any comment, flag a comment if you think it's rude and moderator will look at it (and Abhishek is not a moderator, he's a simple user like you and me) – Temani Afif Jul 06 '20 at 13:09
  • @IloveCoffee We are not moderators, we are just regular users exactly the same as you are. That wasn’t a warning, I’m trying to help by guiding you, as a new user, on what is acceptable. Extended or off-topic discussions are also discouraged, so unless there are more questions or information directly related to the answers, there is no more to be said here (from anyone) :) – FluffyKitten Jul 06 '20 at 13:31