7

I am trying to style disabled state for div I have code

<div disabled> Welcome </div>
<style>
    div:disabled{
      background-color:#f1f1f1;
    }
</style>

I don't have any class or ID present on the element. It is system generated. I want make the background color light gray.

BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
Nitin Shinde
  • 926
  • 4
  • 13
  • 25
  • 1
    div doesn't have enabled/disabled semantics. You don't have a div in a disabled state. You just happen to have a div with an attribute called disabled. That's why the :disabled pseudo doesn't work. If you want to add a pretend disabled attribute to your div, use data-disabled or something. – BoltClock May 10 '17 at 11:42

4 Answers4

9

Try to use the Attribute Selector:

div[disabled]{}

See for more Information:

https://www.w3schools.com/css/css_attribute_selectors.asp

Wolfgang Blessen
  • 826
  • 8
  • 27
3

This can be achieved using attribute selectors, in this case "disabled" is the given attribute:

div[disabled] {
    background-color: #f1f1f1;
}

For more information, here is a very useful reference guide to using data attributes on MDN

Here is the specific guide on Attribute Selectors

Attribute selectors select an element using the presence of a given attribute or attribute value.

Matt D. Webb
  • 3,006
  • 4
  • 28
  • 51
2

In the div, disabled is an attribute. So use an attribute selector.

<div disabled> Welcome </div>
<style>
      div[disabled] {
         background-color:#f1f1f1;
      }
</style>

https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

Michael Benjamin
  • 307,417
  • 93
  • 525
  • 644
1

css to Attribute

div[disabled]{
     background-color:#f1f1f1;
  }
chirag solanki
  • 387
  • 2
  • 8