get value of sibling above
Hey, if i have a structure like so:
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
you could just add the 'a' class to b?
thank you, but i'm more looking for a method of grabbing a value from a sibling that's above it.
you could set it with a custom property then also set that property to b
just set the same class
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
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.
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
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.
so, for all elements inside the 2nd
.flow
it will always have that 10rem top margin?it could be any value, 10, 2, 8, etc.
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?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
i still think you're overcomplicating something simple
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?
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:
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:
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.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
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.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
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/QWPPjWRagain 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.
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 🙂
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
it's all good, no worries, you both were helpful, i feel this ones quite complex to achieve simply unfortunately
i would just use more than one custom property or change some classnames or mess with the selectors. or use gap. gap rocks