Separate conditions depending on children

How do i specify separate conditions if element has different children? For example - if a .button only has .icon - set the aspect-ratio to 1/1, if it has .icon and .text - unset the aspect-ratio and set the width to 100%
19 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
aolko
aolko2y ago
i use scss you seem to modify the children, but the point is to modify the parent
MarkBoots
MarkBoots2y ago
You can use the new :has() selector (but it does not have full support yet on all browsers)
.button { width: 100% }
.button:has(:not(.text)) { width: unset; aspect-ratio: 1/1 }
.button { width: 100% }
.button:has(:not(.text)) { width: unset; aspect-ratio: 1/1 }
aolko
aolko2y ago
i tried using has but got stuck on if it can pull out multiple elements or not
MarkBoots
MarkBoots2y ago
can you make a small example of what you have (html,css) of the button preferably codepen
aolko
aolko2y ago
<div class="button-group">
<a href="#" class="button"><span class="icon"><i class="fa-regular fa-envelope fa-fw"></i></span><span class="text">Contact us</span></a>
<a href="#" class="button"><span class="icon"><i class="fa-regular fa-envelope fa-fw"></i></span></a>
</div>
<div class="button-group">
<a href="#" class="button"><span class="icon"><i class="fa-regular fa-envelope fa-fw"></i></span><span class="text">Contact us</span></a>
<a href="#" class="button"><span class="icon"><i class="fa-regular fa-envelope fa-fw"></i></span></a>
</div>
should be
this one is always square
v
[✉ Contact us] [✉]
^ this button scales
this one is always square
v
[✉ Contact us] [✉]
^ this button scales
MarkBoots
MarkBoots2y ago
here simple example of has. if the .button has a .text, style the .button with color:red it works multiple levels down
aolko
aolko2y ago
🤔 here's a solution w/o has (+1 class)
.button{
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
background: var(--clr__block__btn-bg);
color: var(--clr__block__btn);
padding: 8px;
border-radius: 8px;
text-decoration: unset;
width: 100%;
&.--icon{
display: inline-flex;
aspect-ratio: 1/1;
height: 40px;
width: auto;
}
}
.button-group{
display: flex;
flex-wrap: wrap;
gap: 10px;
>:not(.button.--icon){
flex: 1;
}
&.--v{
flex-direction: column;
>*{
width: 100%;
}
}
}
.button{
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
background: var(--clr__block__btn-bg);
color: var(--clr__block__btn);
padding: 8px;
border-radius: 8px;
text-decoration: unset;
width: 100%;
&.--icon{
display: inline-flex;
aspect-ratio: 1/1;
height: 40px;
width: auto;
}
}
.button-group{
display: flex;
flex-wrap: wrap;
gap: 10px;
>:not(.button.--icon){
flex: 1;
}
&.--v{
flex-direction: column;
>*{
width: 100%;
}
}
}
MarkBoots
MarkBoots2y ago
no idea what your end result should look like.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MarkBoots
MarkBoots2y ago
its really unclear for me. like so? https://codepen.io/MarkBoots/pen/NWBwWbe?editors=1100 (not FF)
aolko
aolko2y ago
not really (the height is too large)
MarkBoots
MarkBoots2y ago
that you can do yourself. i saw 8px padding (0.5rem) and a height of 40px (2.5rem) for the icon. together that is what i have
aolko
aolko2y ago
and ideally if you add more icon buttons they should bump the regular button until it can't scale anymore
MarkBoots
MarkBoots2y ago
that it will
MarkBoots
MarkBoots2y ago