Home >Web Front-end >CSS Tutorial >How Can I Create a CSS Grid of Perfect Squares Using Flexbox?
CSS Grid of Squares Using Flexbox
In your provided code snippet, you have created a grid of squares using flexbox. However, they are not square in shape and they also change their height when the viewport height changes.
To achieve your desired layout, you need to apply a CSS property named aspect-ratio to each of the square elements. The aspect-ratio property forces the element to maintain a specific width-to-height ratio. In your case, you want your squares to be square, so you should set the aspect-ratio to 1 / 1.
Here's how your updated code would look like:
.flex-item { aspect-ratio: 1 / 1; /* Rest of your styles here */ }
By setting the aspect-ratio property, your squares will always maintain a square shape, regardless of the width or height of the viewport.
The above is the detailed content of How Can I Create a CSS Grid of Perfect Squares Using Flexbox?. For more information, please follow other related articles on the PHP Chinese website!