How can I make the height of the items in this Flexbox equal to the variable width?

How can I make the height of the items in this Flexbox equal to the variable width?


I want the width to be decided by the container width and the then the height of each item equal to the width. Can’t get it working though :confused: Here is a codepen.

Any help appreciated!

  <img class = "icon" src = "">
  <img class = "icon" src = "">
  <img class = "icon" src = "">
  <img class = "icon" src = "">
  <img class = "icon" src = "">
  <img class = "icon" src = "">
  <img class = "icon" src = "">
  <img class = "icon" src = "">

div {
  display : flex;
  flex-flow : row wrap;
  width : 200px;
  height : auto;
  border : solid;

.icon {
  margin : 2%;
  flex : 1 1 20%;
  height : auto;
  min-width : 0;
  max-width : 25%;
  min-height : 0;


add JQuery (with the cog icon as you did bootstrap in the quickadd), then add the following into JavaScript panel

var $width = $(".icon").prop("width");
$(".icon").css("height", $width);```


You would find this easier with CSS display: grid


Thanks for your comment, but I’m after a CSS solution ideally. I should have specified, my apologies!


This looks promising, I’ll check it out thanks!


Not sure if this is what you were attempting but this is one way to lay out 2 rows of boxes:


I think I’ve managed to figure out how to do what I’m after. In this codepen, you can see the same 8 boxes in the actual layout I’m trying to achieve. If you change the width on the outer wrapper, you can see that the icons (images) grow and shrink proportionally up to the 100px starting resolution, which is what I’m after. I’ve just got to sort out my max-widths/heights etc.

Thank you for the grid suggestion, I want to delve into this anyway as it has been on my radar, but I haven’t got round to checking it out. If you do happen to know how to achieve this result in grid, it would be a great jumping off point but thanks either way!


I know it may be a sore point, but w3schools is a fantastic reference point for css and html what you are looking for is the box model in css, you can create some interesting grids, although most of this can be done automatically with bootstrap, which is where i would probably start looking, unless you feel the absolute need to go poking around the depths of css…


Is w3schools a sore point for people? I use that site all the time, it’s really helpful. As for poking around the depths of CSS, that’s pretty accurate. I started out using Bootstrap and jQuery also, but decided to limit myself to vanilla CSS and JS to hopefully get more understanding. I do plan to start using them at some point though, because they make life much easier.


I believe you only need to apply display:flex on the container which is “flexing” the child components in it. You seem to have it on everything.
But if it works for you, it works.:+1:
You can use text-align: center to center text.


i wasn’t sure about how people receive information, some i’ve spoken to hate using w3schools, some love it as a reference point. I personally love it as reference.