April 19, 201610 yr I am having an issue when using the flexbox for a layout. I have 4 sections which are in rows. What I would like is for section 4 to be right below section 2 instead of being inline with section 3. This is not happening at the moment as the height of section 2 is the same as section 1. Is there a way to get the height of section 2 to be the the actual height of the element and not stretch to match section 1? I have attached an image to try and explain what I mean. If anyone can help, I would be grateful.
April 19, 201610 yr This is what you need: http://thenewcode.com/844/Easy-Masonry-Layout-With-Flexbox
April 19, 201610 yr Author Thanks for your reply. What I have is a containting div which has the following styles: display: flex; min-height: 100vh; flex-flow: row wrap; Each section is a seperate div with a width on each div. I can't seem to get the each div to have its own height. What is happening is the divs are taking the height of the tallest div, then wrapping.
April 19, 201610 yr As far as I know, that's what flex row is supposed to do. I think what you need is align-items: flex-start: https://css-tricks.com/almanac/properties/a/align-items/
April 19, 201610 yr Author Once again, thanks for your reply. I've tried to use the align-items: flex-start on my container, but it doesn't seem to work. The issue I think is that as section 3 has wrapped to a new row, it can no longer affect sections 1 and 2. Section 4 is on the same row as section 3, so it cannot affect section 2.
April 19, 201610 yr make two columns instead of two rows - my flexbox i really patchy but i think you could try with col (?) instead of row Edited April 19, 201610 yr by dap
April 19, 201610 yr You don't need flexbox for that kind of layout, I'd say inline blocks are best suited to doing a grid containing items of uneven height.
April 20, 201610 yr I agree with @@rbrtsmith display: inline-block would be best for this. I will just add two issues you should know about: You will might have to set a vertical-align property (with a value of "top") to have the elements work like you want Whitespace in the mark-up between inline-block-elements will also be visible in the rendered page and add to the width (just like a space in inline elements) You might need to remove any linebreaks or spaces between the elements in your markup to get the result you want
April 20, 201610 yr Author Thanks for your replies. The reason that I am using flexbox is that I want the items to display stacked and in a different order when on smaller screens. I have it all working fine on the smaller screens. It's just this last issue that I cannot seem to fix.
April 20, 201610 yr I agree with @@rbrtsmith display: inline-block would be best for this. I will just add two issues you should know about: You will might have to set a vertical-align property (with a value of "top") to have the elements work like you want Whitespace in the mark-up between inline-block-elements will also be visible in the rendered page and add to the width (just like a space in inline elements) You might need to remove any linebreaks or spaces between the elements in your markup to get the result you want Niller is right with both points. I will expand on his last one. If you don't need to support older (much older) Android devices you can set font-size to zero on the element that wraps the inline blocks, then on the inline blocks themselves set the font-size to what you desire. That will remove the whitespace but you then lose the font-size cascade. + You get much cleaner markup - No font-size cascade - more limited browser support if you want to remove the whitespace in the markup directly my preferred method is to use comments like this: <ul class="grid"> <li class="grid__item"> Lorem ipsum </li><!-- --><li class="grid__item"> Lorem ipsum </li><!-- --><li class="grid__item"> Lorem ipsum </li><!-- --><li class="grid__item"> Lorem ipsum </li> </ul> OR if you're lucky enough to be doing some server-side rendering of React using JSX and minifying the output then the HTML it sends upto the client will all be void of whitespace so the problem just goes away, despite your JSX looking all nice and clean
April 20, 201610 yr Author Thanks for your reply @rbrtsmith. What you wrote makes sense. I am using the flexbox to try and reorder elements when on smaller screens.
Create an account or sign in to comment