Styling a disabled input with css only

前端 未结 3 1653
北恋
北恋 2020-12-16 10:29

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

相关标签:
3条回答
  • 2020-12-16 10:59

    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.

    0 讨论(0)
  • 2020-12-16 10:59

    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.

    0 讨论(0)
  • 2020-12-16 11:12

    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.

    0 讨论(0)
提交回复
热议问题