skip to Main Content

i’m not sure if it possible: i need to apply some CSS border based on elements present inside a parent div.

These parent div are generated dinamically, so, sometimes it has 2 children (ta-1 and ta-2), and sometimes only 1 child (ta-1 or ta-2)

For example (with both):

Here i want that ta-1 has: border radius only on top, and ta-2 border radius only on bottom:

<div class="parent">
    <textarea class="ta-1"></textarea>
    <textarea class="ta-2"></textarea>
</div>

CSS:

.parent .ta-1 { border-top-left-radius: 50%; border-top-right-radius: 50%; }
.parent .ta-2 { border-bottom-left-radius: 50%; border-bottom-right-radius: 50%; }

But if I’ve only a child, i need to apply border radius to all 4 corners of ta-...

<div class="parent">
    <textarea class="ta-2"></textarea>
</div>

CSS:

.parent .ta-1 { border-top-left-radius: 50%; border-top-right-radius: 50%; border-bottom-left-radius: 50%; border-bottom-right-radius: 50%; }
.parent .ta-2 { border-top-left-radius: 50%; border-top-right-radius: 50%; border-bottom-left-radius: 50%; border-bottom-right-radius: 50%; }

Is there a code that check if there are both children to apply radius only on 2 corner of each ta-..., and check if there is only one ta-... child apply to all 4 corners?

Thanks

2

Answers


  1. You can use :has with :not element of css.

    .parent .ta-1 {
      border-top-left-radius: 25%;
      border-top-right-radius: 25%;
    }
    
    .parent .ta-2 {
      border-bottom-left-radius: 25%;
      border-bottom-right-radius: 25%;
    }
    
    .parent1:has(:not(.t2)) .ta-1 {
      border-radius: 50%;
      border: 1px solid #F00;
    }
    
    .parent2:has(:not(.t1)) .ta-2 {
      border-radius: 50%;
      border: 1px solid #0F0;
    }
    <div class="parent">
      <textarea class="ta-1"></textarea>
      <textarea class="ta-2"></textarea>
    </div>
    
    <div class="parent1">
      <textarea class="ta-1"></textarea>
    </div>
    
    <div class="parent2">
      <textarea class="ta-2"></textarea>
    </div>
    Login or Signup to reply.
  2. .parent .ta-1 {
      border-top-left-radius: 25%;
      border-top-right-radius: 25%;
    }
    
    .parent .ta-2 {
      border-bottom-left-radius: 25%;
      border-bottom-right-radius: 25%;
    }
    
    .parent1:has(:not(.t2)) .ta-1 {
      border-radius: 50%;
      border: 1px solid #F00;
    }
    
    .parent2:has(:not(.t1)) .ta-2 {
      border-radius: 50%;
      border: 1px solid #0F0;
    }
    <div class="parent">
      <textarea class="ta-1"></textarea>
      <textarea class="ta-2"></textarea>
    </div>
    
    <div class="parent1">
      <textarea class="ta-1"></textarea>
    </div>
    
    <div class="parent2">
      <textarea class="ta-2"></textarea>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search