Match width sizing on flex containers?

I was wondering if anybody knew a neat way to grab the width of a flex-item container and apply it to a sibling flex container inside the same flex parent. I have a screenshot here showing a search component ("enter an address") and then below it a second component with a "browse" button. I was thinking there's got to be a simple way to make the browse button component as wide as the search input component. (Each component has the purple-gray background.) I am sure I could switch to grid or use a different flex solution, but I only want the browse component to match the width of the search input component, and for other siblings to flex the default way. CSS/SCSS for reference, the parent has:
align-items: center;
display: flex;
flex: 1; // the parent is also a flex item
flex-direction: column;
gap: $size-4;
justify-content: center;
align-items: center;
display: flex;
flex: 1; // the parent is also a flex item
flex-direction: column;
gap: $size-4;
justify-content: center;
Both child components are grid containers with grid items. Thanks!
No description
35 Replies
b1mind
b1mind•4w ago
display: grid
b1mind
b1mind•4w ago
Oh guess I kinda half read that 😂 updated, still using grid <,<;;
elbi3
elbi3•4w ago
thank you for the codepen b1mind, I always like your opinions they're always good I mean
b1mind
b1mind•4w ago
np not sure its the best solution for ya, just kinda lean on grid when I need things to fill.
elbi3
elbi3•4w ago
this doesn't really help me unfortunately yea I already have grid similarly set up on both the components, it' hmm idk I guess it's a design wish, everything is where it needs to be and is responsive and all that, I just don't like that the bg shrinks on the lower button and I don't want to widen the button or, create content to make it the same width if that makes sense? I can always brute force the width with padding but I was hoping there was an answer to my question because it would be elegant idk
b1mind
b1mind•4w ago
Guess I'm not quite sure what you are after, can you provide a codepen with more of a complete example? I just went off* your screen shot
b1mind
b1mind•4w ago
No description
elbi3
elbi3•4w ago
literally, can one locate(?) the width of a "flex-item A" and apply it to another "flex-item B". So flex-item A still shrinks to fit its content, but flex-item B then widens to match the width of flex-item A
b1mind
b1mind•4w ago
no You can give them both a basis or set their min-widths* though 🤔
elbi3
elbi3•4w ago
this is good enough for me, I just wanted to know if it was possible thanks : ) yea I'll just manually set their widths then
b1mind
b1mind•4w ago
again I'd just use grid xD
elbi3
elbi3•4w ago
oh maybe if I borrow SCSS variables from the top component and apply them to a width in the other, that might work yea this is the rational choice, I just got a bug in my brain
b1mind
b1mind•4w ago
not a fan xD anytime I see flex direction too I question if it shouldn't just be grid 😉
elbi3
elbi3•4w ago
yea I don't like that the button stretches
b1mind
b1mind•4w ago
I thought you wanted that just the parent? (.bg)
elbi3
elbi3•4w ago
yea just the parent this is my struggle, because it shrinks to the width of the button
b1mind
b1mind•4w ago
updated grid solution 😉
elbi3
elbi3•4w ago
I feel that I am pulling you down a road to madness and I don't want you to have to put more brainpower toward this 😛
b1mind
b1mind•4w ago
na I enjoy it Cause I look for patterns or situations when I would really want to use flex/grid Yea the other reason I don't like using flex direction I come to realize is having the axis change https://codepen.io/b1mind/pen/MWdEyKq Having to justify with align feels so dirty 90% of the time 🤣 It make sense only if you are changing it for a media query imo (not to use it as default behavior) In most those cases though I'm just going to use flex-wrap xD
elbi3
elbi3•4w ago
I just like the shrink-behavior of flexbox in this case
b1mind
b1mind•4w ago
Right but you get it it with more power in Grid in this case if you look at the example I updated 😉
elbi3
elbi3•4w ago
yes I looked at it, it's good! we are on the same page about Grid, I am just hung up on this for some reason
b1mind
b1mind•4w ago
haha yea not sure, for me its an obvious solution >.>;; use grid.
elbi3
elbi3•4w ago
I'll probably wake up in the morning, look at this conversation again, look at your codepens again, and change it to grid lol it's too late for me, I'm just gonna go make some svgs lol
clevermissfox
clevermissfox•4w ago
If you’re using flex, don’t forget to harness the power of flex basis and flex-grow. Seems like you just want a flex-grow on the element holding the Browse button ?
elbi3
elbi3•4w ago
this doesn't really help me with my issue as I am trying to mirror the size of its sibling element while still allowing its sibling element to naturally shrink around its content. Flex-basis/grow isn't going to help when I don't know what to set it to
clevermissfox
clevermissfox•4w ago
You would set flex-grow:1 so it’s Browse element matches the size of its sibling even when the sibling shrinks. If you post a codepen with your current code I can show you an example. Or just remove align items center from your browse element and it will stretch to the width you want by default depending on how you’ve structured your html
elbi3
elbi3•4w ago
this is the only solution I am looking for I am very good at flex and grid, and I can make the sizing happen, I just wanted to know if the way I mentioned was a possibility there are other items in this flex box, as well. I'll probably just take the SCSS variable from my input width and make a div around the button that's the same width. It's hacky, but at least I'll get the responsive visual I want
b1mind
b1mind•4w ago
taps the sign "Grid does this" by setting the place-content: center; the children won't dense (like flex) but by default fill the max-content width of any of the children. just saying again Like for example if we make the heading the largest child, the .bg's are going to still fill to match
b1mind
b1mind•4w ago
No description
b1mind
b1mind•4w ago
You can ofc put this as a child or max-width the full grid as well to give more control (not to mention assign template to it for more control), as missfox mentioned though without a example its really hard to understand how you have it structured.
clevermissfox
clevermissfox•4w ago
If you’re going to just set its width anyway and you’re in a column you may as well remove flex or grid and just use utilize block width 100% on the element with the browse button. You’re fighting against flex instead of having it work for you with what its properties can do. If you’re intent on setting and tracking a width, use JavaScript and event listeners and custom properties.