Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a parent element’s style based on its child’s attributes in CSS?

Tags:

css

For example when input is disabled, parent element should have Black background otherwise White.

<div><input type="text" disabled="disabled" /></div>

div input[type="text", disabled="disabled"] {
    background: Black;
}

I want to style parent element instead of child.

like image 538
Sadegh Avatar asked Dec 01 '25 08:12

Sadegh


2 Answers

You can select this elements parent with css has selector;

For Example;

Lats say we have an div and div have inside to input element;

.hasan-ablak {
            padding: 5px;
            background-color: green;
        }

.hasan-ablak:has(input:disabled) {
  background-color: orange;
}
.hasan-ablak:has(input:disabled) {
            background-color: red;
        }
<div class="hasan-ablak">
            <input type="text">
</div>
    
<div class="hasan-ablak">
  <input type="text" disabled>
</div>
like image 130
Hasan ABLAK Avatar answered Dec 03 '25 23:12

Hasan ABLAK


Not possible using CSS.

It is possible with JavaScript though.

like image 33
PeeHaa Avatar answered Dec 04 '25 00:12

PeeHaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!