Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector for disabled elements

I'm working with AngularJS to set image buttons disabled/enabled.
My css selector to show them transparent isn't working.
I've started with a try it that selects a disable on an input element and there it does indeed apply the css, but not in case of my div elements.
I've added my div elements that don't work, resulting in the following code:

<!DOCTYPE html>
<html>
<head>
<style> 
input:enabled {
    background: #ffff00;
}

input:disabled {
    background: #dddddd;
}
div:disabled {
    opacity: 0.4;
    filter: alpha(opacity=40); /* For IE8 and earlier */
}
</style>
</head>
<body>

<form action="">
First name: <input type="text" value="Mickey"><br>
Last name: <input type="text" value="Mouse"><br>
Country: <input type="text" value="Disneyland" disabled><br>
Password: <input type="password" name="password" value="psw" disabled><br>
E-mail: <input type="email" value="[email protected]" name="usremail">
</form>
<div disabled="disabled">should be transparent</div>
</body>
</html>

The disabled is getting added/removed for my AngularJS html elements. So how do I get the css to apply to a div with disabled added to it?

Note: I know it's possible to duplicate the elements, use ng-if to show/hide them and apply the transparency to it with a class, but that's very ugly.

like image 308
MrFox Avatar asked Aug 30 '17 06:08

MrFox


People also ask

How do I select a disabled element in CSS?

The :disabled CSS pseudo-class represents any disabled element. An element is disabled if it can't be activated (selected, clicked on, typed into, etc.) or accept focus. The element also has an enabled state, in which it can be activated or accept focus.

What is the correct selector for targeting all text inputs that are disabled?

The :disabled selector matches every disabled element (mostly used on form elements).

How do you apply style to only those input tag which have disabled attributes to them?

First step: swap the HTML elements order so that the <label> appears after the <input> . This will allow the styling rules to work as desired. Then for the fun bit: use CSS to position the labels for text inputs on the left hand side! input[type=radio]:disabled+label works like a sharm!


3 Answers

:disabled pseudo selector will work only for input elements. For div, use div[disabled] to apply css

Use

div[disabled] {
    opacity: 0.4;
    filter: alpha(opacity=40); /* For IE8 and earlier */
}

Demo

input:enabled {
    background: #ffff00;
}

input:disabled {
    background: #dddddd;
}
div[disabled] {
    opacity: 0.4;
    filter: alpha(opacity=40); /* For IE8 and earlier */
}
<form action="">
First name: <input type="text" value="Mickey"><br>
Last name: <input type="text" value="Mouse"><br>
Country: <input type="text" value="Disneyland" disabled><br>
Password: <input type="password" name="password" value="psw" disabled><br>
E-mail: <input type="email" value="[email protected]" name="usremail">
</form>
<div disabled="disabled">should be transparent</div>
like image 62
Muthu Kumaran Avatar answered Oct 01 '22 16:10

Muthu Kumaran


Select all disabled input elements (such as input, textarea, select, option, radio, checkbox, button) :

*:disabled{
    //enter code here
}

Select all other disabled elements (such as div, section, p, etc):

*[disabled]{
    //enter code here
}
like image 26
daniellalasa Avatar answered Oct 01 '22 16:10

daniellalasa


Use the attribute selector [attribute='value'], which will work on all types of elements, compared to the pseudo-class :disabled, which only works on form elements

And in your case, where the attribute disabled doesn't have a value, you can omit it [disabled]

Note, when not using the value part in the selector, it will target elements both with and without, but as you can see the with last CSS rule, where the value part is used, it won't.

Stack snippet (here I used it on all, but you can of course keep :disabled for the input's)

input:not([disabled]) {
  background: #ffff00;
}

input[disabled] {
  background: #dddddd;
}

div[disabled] {
  opacity: 0.4;
  filter: alpha(opacity=40);      /* For IE8 and earlier */
}

div[disabled='disabled'] {
  color: red;
}
<form action="">
  First name: <input type="text" value="Mickey"><br>
  Last name: <input type="text" value="Mouse"><br>
  Country: <input type="text" value="Disneyland" disabled><br>
  Password: <input type="password" name="password" value="psw" disabled><br>
  E-mail: <input type="email" value="[email protected]" name="usremail">
</form>

<div disabled>
  should be transparent, but doesn't have red colored text
</div>

<div disabled='disabled'>
  this will both be transparent and have red colored text
</div>
like image 40
Asons Avatar answered Oct 01 '22 18:10

Asons