I have read all the stackoverflow posts about this problem but all have a fixed amount of columns or divs in the width (unless I missed it).
I am designing an online store. Each product is presented on a product tile. Each product tile floats (left). At the browser window's widest, there are three product tiles on each line. When sizing the browser down to 680px, there are only two product tiles on each line and at 400px only one on each line.
If one tile contains a product name that takes up two lines, it increases the height of the product tile. Therefor, the tiles beneath it won't float. They'll get stuck at the right bottom corner of the higher product tile above it.
I wrote some javascript/jquery to check for the highest tile and then set all the tiles to the same height. This works well on load (eventlistener), but for some reason not on resize.
Can someone explain why this function does not work on resize? And if someone knows a way to only set all product tiles in a row to the same height as the highest product tile in that row, versus setting all product tiles to the highest product tile, that would really make my day. (don't forget the amount of tiles in a row depends on the window width)
This is the html for a product tile.
<div class="productTile"> <!-- stuff goes here --> </div> This is the CSS for a product tile.
.productTile { background:grey; width:33%; float:left; } @media only screen and (max-width:680px) { .productTile { width:50%; } } @media only screen and (max-width:400px) { .productTile { width:100%; } } This is the jQuery
function setProdTiles() { var prodTileH = 0; // set variable to catch largest height $(".productTile").each(function(){ H = $(this).outerHeight(); // height current productTile if (H > prodTileH) // get highest { prodTileH = H; } }); $(".productTile").outerHeight(prodTileH); } window.addEventListener("load", setProdTiles); window.addEventListener("resize", setProdTiles); Thank you in advance.
tablein HTML. All rows are the same height, automatically resizing to the maximum height of the content in each cell. No javascript! Significantly less overhead in comparison to what you're trying to do.