My attempt to create a responsive grid layout is lacking in the mathematical department... Seriously, I'm not a math genius.
This is my goal:
- Determine the width of the container holding the grid elements.
- Apply a 20px right and bottom margin to all grid elements.
- Adjust the number of cells based on the container's width; for example, have 5 cells if the width exceeds 1200px.
- The last element in each row should have a margin-right set to 0.
- Calculate the percentage width of each cell.
If the last element keeps the margin-right, everything functions as intended. However, the margin needs to be removed in this scenario.
This represents the extent of my mathematical prowess...^-^
var marginRight = 20, viewportWidth = 1200, numcells = 5;
var substract = marginRight / ( viewportWidth / 100 );
width = 100 / numcells - substract;
As mentioned before, this solution works if the last cells maintain the margin. Unfortunately, they do not, and I'm unsure how to address this issue. Perhaps some wisdom from the Stack Overflow community can shed light on the matter? That would be greatly appreciated. Thank you.