react-grid-layout: rowHeight calculate error!
var layouts = {
lg: [
{i: '1', x: 0, y: 0, w: 1, h: 2},
{i: '2', x: 1, y: 0, w: 1, h: 1},
{i: '3', x: 2, y: 0, w: 1, h: 1}
]
};
return (
<ResponsiveReactGridLayout className="layout" rowHeight={10} margin={[10,10]} layouts={layouts}
breakpoints={{lg: 1200}}
cols={{lg: 12}}>
<div className="rcBlock" key={"1"} style={{height:20}}>1</div>
<div className="rcBlock" key={"2"}>2</div>
<div className="rcBlock" key={"3"}>3</div>
</ResponsiveReactGridLayout>
)
Hi, i have a question, i run this code, i thought the (div of key=1)'s height should be 20px, but whatever i resize the (div of key=1), the (div of key=1)'s height cannot be set to 20px ,the mini size is 30px, even i set the height style, the (div of key=1) cannot be set to 20px, why???can anybody help me? thanks very much!!!
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 17
height is equal to h * rowHeight + (h - 1) * marginThis should be written in documentation to save debugging time.You can’t. It’s a grid, in order for items to be swappable between rows, all the rows need to be the same height.
If you’re like me and you know the actual height in px but you need the calculated h value for setting an item to the height equivalent of the full grid you can use.
h = (y + m) / (r + m)
with y being the height of the grid in px.
@JohnGrisham You’re correct that the formula needs to be adjusted to account for margins. Given
yas the height of the grid in pixels, the formula for calculating the grid item heighthshould be:Where
yis the height of the grid in pixels,mis the vertical margin, andris the row height.However, the correct formula should consider subtracting the two outer margins as well. Therefore, the new formula becomes
This formula subtracts the vertical margin
mfrom the height of the gridyto account for the outer margins, and then divides the result by the sum of the row heightrand the vertical marginm.Using this corrected formula, you can calculate the grid item height
hto set an item to the height equivalent of the full grid.@STRML It is a bit difficult for me to understand why
react-grid-layoutcomputes the height of a grid item this way, and I think the same goes for other people who first approach to this library. Could you please explain why e.g. the height of a grid item withh: 3whenrowHeight={120}isn’t just120 * 3 = 360px? Anyway, thank you for this library! 😃Please see above. There is no other way it could be: height is equal to
h * rowHeight + (h - 1) * margin. It is intentional design such that the grid lines up, and other grid libraries work the same way.I’m not… but the library doesn’t calculate the container height correctly… Create a grid with the
marginset to[10,10]and therowHeightset to10. Then create an item where thehis set to5. Inspect theheightof the actual element, it will be set to90px.What happens with
margin: [10,10]:What I expected:
For the second image I set the
marginof the child div to5pxcontainerPadding: 5andmargin: [0,0], that essentially gives me the margin I’m looking for, thehbecomes7instead of5but the user can adjust the layout as they see fit.