im new to html and css and ive made some toggle buttons that im trying to dynamically align to the center of the screen.
I found this thing called flexbox which seems to make doing this rather easy however, my only issue is that when the window that my html file is loaded on becomes narrower, the buttons separate unevenly and im not a big fan of this aesthetically.
does anyone know how to either utilise flexbox to make it so that the buttons will always have an even amount on top and bottom (i.e. only ever one row of 8, two rows of 4, four rows of 2 etc)
otherwise if someone can tell me how to do this without flexbox? just using html+css that would be much appreciated.
thank you
i tried using flexbox my code is as follows:
.center {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
}
it looks great on full-size wide screens but not great on medium screens when there are 6 on top and 2 on bottom for example
2
Answers
check this hopefully it is helpful foryou if you like this answer you can accept if you have another issue please comment.
There’s an article by Temani Afif that provides a nice solution to this — using CSS Grid without media queries — which I’ve adapted for your situation below:
You could use media queries instead if you find that easier, although this is a very cool method. You can adjust the breakpoints in the code above to taste. But Grid is a better option here than Flexbox, however you approach it. (And there are other solutions without either + media queries, but these newer layout options are nicer to use.)
In the demo above, you could also hardcode all those variables if you prefer, but I’ve left them as provided in the article for now.