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
  • 700,868
  • 160
  • 1,392
  • 1,356
Nitin Shinde
  • 1,154
  • 4
  • 16
  • 26
  • 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

11

Try to use the Attribute Selector:

div[disabled]{}

See for more Information:

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

Wolfgang Blessen
  • 900
  • 10
  • 29
4

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,216
  • 4
  • 29
  • 51
3

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
  • 346,931
  • 104
  • 581
  • 701
2

css to Attribute

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