32

I have sort of a strange situation. I'm trying to style a disabled input button because I have an annoying hover turning the text to white. This makes it confusing to the user because its acting like a normal button.

I have tried a few things, mainly css and a few jQuery things. I would like to keep this in css if at all posable.

This is my html, sorry it is in a larvel form helper.

{{ Form::submit('Change', array_merge($design_project->is_locked ? ['disabled' => 'disabled'] : [], ['class' => 'btn btn-blue span3'])) }}

and this is what the browser generates

<input disabled="disabled" class="btn btn-blue span3" type="submit" value="Change">

and I was working on something like this

.btn:hover input[disabled], .btn:active input[disabled], .btn:focus input[disabled]{
  color:green
}

Any help would be wonderful!

zazvorniki
  • 3,512
  • 21
  • 74
  • 122

3 Answers3

64

Use this CSS (jsFiddle example):

input:disabled.btn:hover,
input:disabled.btn:active,
input:disabled.btn:focus {
  color: green
}

You have to write the most outer element on the left and the most inner element on the right.

.btn:hover input:disabled would select any disabled input elements contained in an element with a class btn which is currently hovered by the user.

I would prefer :disabled over [disabled], see this question for a discussion: Should I use CSS :disabled pseudo-class or [disabled] attribute selector or is it a matter of opinion?


By the way, Laravel (PHP) generates the HTML - not the browser.

Community
  • 1
  • 1
ComFreek
  • 29,044
  • 18
  • 104
  • 156
  • Thank you, this worked perfectly for me. Me and my silly spaces! :) – zazvorniki Aug 26 '13 at 13:46
  • Are there any reasons for preference between using `[disabled]` and `:disabled` ? (which is also chainable → https://css-tricks.com/almanac/selectors/d/disabled/ ) – Frank N May 14 '17 at 07:38
  • @FrankNocke Indeed, :disabled seems to be more preferable, see my edit. – ComFreek May 14 '17 at 08:42
19

Let's just say you have 3 buttons:

<input type="button" disabled="disabled" value="hello world">
<input type="button" disabled value="hello world">
<input type="button" value="hello world">

To style the disabled button you can use the following css:

input[type="button"]:disabled{
    color:#000;
}

This will only affect the button which is disabled.

To stop the color changing when hovering you can use this too:

input[type="button"]:disabled:hover{
    color:#000;
}

You can also avoid this by using a css-reset.

Community
  • 1
  • 1
saad arshad
  • 259
  • 1
  • 10
  • 2
    Why was this downvoted? It's a perfectly valid solution for modern browsers. – Brigand Aug 26 '13 at 13:50
  • 5
    thanks mate. i was about to start thinking that my whole CSS career is a lie :) – saad arshad Aug 26 '13 at 13:52
  • @thesaadarshad I just received an upvote on my answer on this question. By accident I also saw your answer, which I downvoted for a reason I can't remember anymore. I made an edit to be able to revert the downvote. +1! – ComFreek Jul 01 '14 at 16:00
  • 1
    `:disabled` doesn't seem to work in IE9. `[disabled]` works. – Victor Zakharov Jan 24 '17 at 14:57
1

A space in a CSS selector selects child elements.

.btn input

This is basically what you wrote and it would select <input> elements within any element that has the btn class.

I think you're looking for

input[disabled].btn:hover, input[disabled].btn:active, input[disabled].btn:focus

This would select <input> elements with the disabled attribute and the btn class in the three different states of hover, active and focus.

Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102