mjr
mjr

Reputation: 2103

How to style a HTML label for disabled input

I would like the labels for my form elements to be greyed out if the input is disabled and am not able to get it to work for text inputs. I have tried the following:

input:disabled {
    background:#dddddd;
}

input:disabled+label{color:#ccc;}

<input type='checkbox' disabled id='check1'>
<label for='check1'>Check</label>
<br>
<label for='text1'>Text</label>
<input type='text' id='text1' disabled>

Js Fiddle

The styling works for the checkbox label, but not the text label. Are checkboxes the only input types that let you style their labels via css?

I testing with Firefox.

Upvotes: 56

Views: 133499

Answers (7)

Jerry
Jerry

Reputation: 1527

This is similar to Todd Hale's answer using the :has() selector, but uses a wrapper that finds any child that is disabled, and allows you to style it's children.

Using this technique you would want to wrap each label and input pair individually.

.form-field:has(:disabled) label {
  color: grey;
}
<div class="form-field">
  <label for="name">Label before, styled</label>
  <input id="name" disabled />
  <label for="name">Label after, also styled</label>
</div>

Upvotes: 1

Todd Hale
Todd Hale

Reputation: 571

If you want to leave your labels before your inputs and lighten your label, you can use the :has pseudo-class and ~ sibling selector:

label:has(~ :is([disabled],[readonly])) {
    color: rgba(0,0,0,0.54); /* or opacity: .5; */
}

Upvotes: 2

Sentary
Sentary

Reputation: 2016

I had the same issue: make a read-only input EXACTLY like label, I add a set of css styles to the input to get to that goal:

<input readonly class="inputLikeLabel" value="${myBean.property}"></input>

And in CSS:

.inputLikeLabel {
    background-color: #ffffff;
    text-align: center;
    border: none;
    cursor: none;
    pointer-events: none;
}

By the css style, the input has a white background with no border, no mouse cursor and no click event...similar to label by the end !

Upvotes: 0

gvee
gvee

Reputation: 17161

Based on the comment made by @andi:

input:disabled+label means that the label is immediately AFTER the input. In your HTML, the label comes BEFORE the text input. (but there's no CSS for "before".)

He's absolutely right. But that shouldn't stop us being able to solve the problem with a little trickery!

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:disabled {
  background: #dddddd;
}

input:disabled+label {
  color: #ccc;
}

input[type=text]+label {
  float: left;
}
<input type="checkbox" disabled="disabled" id="check1">
<label for="check1">Check</label>
<br />
<input type="text" id="text1" disabled="disabled">
<label for="text1">Text</label>
<br />
<input type="checkbox" id="check2">
<label for="check2">Check</label>
<br />
<input type="text" id="text2">
<label for="text2">Text</label>

Upvotes: 70

AlexVMM
AlexVMM

Reputation: 134

You can use atribute selectors in CSS, example https://jsfiddle.net/8pp6mpp5/1/

Html

<label disabled="disabled">Hola Mundo!</label></br>
<label>Hola Mundo!</label>`

CSS

label[disabled="disabled"]{
    background-color: #AAA;
}

Upvotes: 3

OneOfOne
OneOfOne

Reputation: 99225

You can also use floats and always put the label after the input Demo

You will have to wrap it in a span (or any other element really).

HTML :

<span>
    <input type='checkbox' disabled id='check1'>
    <label for='check1'>Check</label>
</span>
<br>
<span>
    <input type='text1' id='text1' disabled>
    <label for='check'>Text</label>
</span>

CSS :

span {
    display: inline-block;
    overflow: hidden;
}

input {
    float: right;
}

label {
    float: left;
}

input:disabled {
    background:#dddddd;
}

input + label {
    float: none;
}

input:disabled + label {
    color:#ccc;
}

Upvotes: 3

Dejan
Dejan

Reputation: 511

This selector input:disabled+label{color:#ccc;} targets label elements that are placed after an input element that is disabled

In this snippet the label is after a disabled input, so the label element is gray

<input type='checkbox' disabled id='check1'>
<label for='check1'>Check</label>

In this case, the label is before the input so the selector does not apply to it

<label for='text1'>Text</label>
<input type='text' id='text1' disabled>

Possible solution would be to wrap your elements in an extra div and apply a class name to the div, something like this

<div class='disabled'>
    <input type='checkbox' disabled id='check1'>
    <label for='check1'>Check</label>
</div>
<div class='disabled'>
    <label for='text1'>Text</label>
    <input type='text' id='text1' disabled>
</div>

And then you can write your css like this

.disabled label {
    color: #ccc;
}

Upvotes: 6

Related Questions