get value of sibling above

Hey, if i have a structure like so:
<p class="a">...</p>
<p class="b">...</p>
<p class="a">...</p>
<p class="b">...</p>
.a {
color: blue;
}
.a {
color: blue;
}
in terms of the p tag with the class of b, would it be possible to get the value from the color property from the sibling above it so i can utilize that within the styles of b? Thanks in advance.
23 Replies
clevermissfox
clevermissfox8mo ago
you could just add the 'a' class to b?
snxxwyy
snxxwyyOP8mo ago
thank you, but i'm more looking for a method of grabbing a value from a sibling that's above it.
clevermissfox
clevermissfox8mo ago
you could set it with a custom property then also set that property to b
ἔρως
ἔρως8mo ago
just set the same class
snxxwyy
snxxwyyOP8mo ago
thank you but that's still not quite what i'm looking for unfortunately, i'll provide some further context: I have a layout like so that's utilizing a flow utility
<div class="flow">
...
<div class="flow">
...
</div>
</div>
<div class="flow">
...
<div class="flow">
...
</div>
</div>
.flow > * + * {
margin-top: var(--flow-space, 1rem);
}
.flow > * + * {
margin-top: var(--flow-space, 1rem);
}
If the parent div's flow-space value is 5rem, which sets a 5rem gap between it's children, and i'd like to set the child's flow-space value to 10rem, it sets a 10rem gap between it's children like expected but due to the way the property cascades it changes it's flow-space value in the parent div making a 10rem space above it instead of keeping the 5rem space which isn't wanted. I wanted to see if i could set the margin-top property of an element with a flow utility that happens to be a child of an element utilizing it also to either it's parent's of the child above it's value.
.flow:has(> .flow) .flow {
margin-top: ...;
}
.flow:has(> .flow) .flow {
margin-top: ...;
}
ἔρως
ἔρως8mo ago
don't try to get fancy and build overly overcomplicated spaghetti lasagna with onions that you can't debug in a month keep it simple if you want to set the top margin, just set the top margin or have a css variable for it
snxxwyy
snxxwyyOP8mo ago
i was trying to make it versatile so i didn't have to do that, if it's a child of a parent also using the same value it would just automatically make the top margin the same as the child's above it.
ἔρως
ἔρως8mo ago
so, for all elements inside the 2nd .flow it will always have that 10rem top margin?
snxxwyy
snxxwyyOP8mo ago
it could be any value, 10, 2, 8, etc.
clevermissfox
clevermissfox8mo ago
why cant you just set the child's margin to calc(var(--flow-space) * 2)? or get the value of --flow-space since its a custom property and do what you need to with JS?
snxxwyy
snxxwyyOP8mo ago
i could indeed do that for this example, but as i mentioned, they both can be any value, it's unlikely the child's property value would be double the parents. The parent's could be 2.5rem and the child's 7.3rem
ἔρως
ἔρως8mo ago
i still think you're overcomplicating something simple
clevermissfox
clevermissfox8mo ago
your example was that the parent's was 5rem, and the child's was 10rem so thats the info we are going off of. once you have the value of the parent's whether its 10rem or 2.5rem, what are you trying to do with the childs margin?
snxxwyy
snxxwyyOP8mo ago
I'll try to explain the whole thing a little better if you put a flow utility on an element and specify the flow-space custom property value, it adds margin-top to the children with that value to space the children out. Using this as an example:
<div class="flow">
<p>...</p>
<p>...</p>
<p>...</p>
</div>
<div class="flow">
<p>...</p>
<p>...</p>
<p>...</p>
</div>
div {
--flow-space: 2rem;
}
div {
--flow-space: 2rem;
}
there would be a 2rem gap between all the p tags. You can change the individual spacing of an element by changing it's --flow-space value, so if i did this:
p:nth-of-type(2) {
--flow-space: 5rem;
}
p:nth-of-type(2) {
--flow-space: 5rem;
}
it would create a larger space above the second p tag. If i had a child of a parent that's using a flow utility, with the parent also using a flow utility, specifying a value for the child's --flow-space property adds spacing between it's children as usual however since it's a child of a parent using the utility, it also changes it's personal property changing the space above it too. I trying to find a method to make the margin-top of the child basically not do that and to remain as it was.
clevermissfox
clevermissfox8mo ago
the parents margin-top should not increase if you redfine/increase the custom-property value on the child. custom properties dont inherit up maybe make a codepen with the issue youre experiencing because the parents --flow-space value should not be affected by increasing the childs --flow-space value
snxxwyy
snxxwyyOP8mo ago
the parent's flow space value isn't being affected, it's just the child div's since as well as adding space between it's children, it's redefining it's individual flow-space value like i demonstrated with the nth-of-type example.
clevermissfox
clevermissfox8mo ago
okay, i see. thats how it works. i would make two custom properties then, one for the parent and direct-children's margin's and one for the grandchildren margins. or use different class names. or use an @property set to not inherit for the properties you dont want to inherit. or change your selectors from .flow:has(.flow). Youre using the same class names and the same custom properties so everything is going to trickle down. or just use grid and gap
snxxwyy
snxxwyyOP8mo ago
i unfortunately don't think the @property works, i couldn't get it to work with rem which was odd so i used colors for an example, it still seems to inherit to a div it shouldn't if i've understood it correctly. https://codepen.io/deerCabin/pen/QWPPjWR
clevermissfox
clevermissfox8mo ago
again this example is working as it should. You have used the custom property on the selector > * + * which means any direct child of the .flow that has anything after it (whether a p tag or a div or any element) . the inherits on color @property sets whether it inherits by default. but css by design will inherit the value of the color of its parent.
snxxwyy
snxxwyyOP8mo ago
ah that's a shame then, i don't think what i was looking for the way i was looking for it is possible haha, thank you all for your help 🙂
clevermissfox
clevermissfox8mo ago
sorry i cant be more helpful, everything you described as behviour is the behaviour of css given the code youre writing. you cant use the same custom properties and the same classes and expect it to yield different results. maybe :nth-of-type ranges with a combination of > and + selectors can get you to target the exact elements you want to set
snxxwyy
snxxwyyOP8mo ago
it's all good, no worries, you both were helpful, i feel this ones quite complex to achieve simply unfortunately
clevermissfox
clevermissfox8mo ago
i would just use more than one custom property or change some classnames or mess with the selectors. or use gap. gap rocks
Want results from more Discord servers?
Add your server