Home > Web Front-end > CSS Tutorial > How to Create a Responsive 4-Column Grid Layout with Equal-Sized Squares Using CSS Grid?

How to Create a Responsive 4-Column Grid Layout with Equal-Sized Squares Using CSS Grid?

Susan Sarandon
Release: 2024-12-26 09:18:10
Original
808 people have browsed it

How to Create a Responsive 4-Column Grid Layout with Equal-Sized Squares Using CSS Grid?

CSS Grid Square Layout

Want to create a grid layout composed of squares, with each row containing four squares . These squares do not deform when the screen size changes and always maintain the same width and height (fixed values ​​are not desirable). CSS grid is required. Here's how to do it:

Using CSS, you can always maintain an aspect ratio of 1:1 via a pseudo-element, or use the new property aspect-ratio, for example:

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-gap: 5px;
}
.container div {
  background-color: red;
  aspect-ratio: 1;
}
Copy after login
<div class="container">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
Copy after login

The above is the detailed content of How to Create a Responsive 4-Column Grid Layout with Equal-Sized Squares Using CSS Grid?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template