Home > Web Front-end > CSS Tutorial > How to Create a Centered Grid with a Left-Aligned Last Row Using Only CSS?

How to Create a Centered Grid with a Left-Aligned Last Row Using Only CSS?

Patricia Arquette
Release: 2024-12-13 11:29:15
Original
552 people have browsed it

How to Create a Centered Grid with a Left-Aligned Last Row Using Only CSS?

Centered Grid with Left-Aligned Last Row Without Flexbox

CSS with Inline-Block Display

Consider a simpler adaptive grid design with minimal markup and CSS, making it versatile for implementation and customization.

For a left-aligned last row in a centered grid where the columns vary dynamically based on browser width, CSS can achieve this effect without using Flexbox.

Here's the code:

html, body {
    margin:0;
    padding:0;
}
#container{
    font-size:0;
    margin:0 auto;
    width:1000px;
}
.block {
    font-size:20px;
    width: 150px;
    height: 150px;
    margin:25px;
    background: gold;
    display:inline-block;
}

@media screen and (max-width: 430px) {
    #container{
        width:200px;
    }
}

@media screen and (min-width: 431px) and (max-width: 630px) {
   #container{
        width:400px;
    }
}
@media screen and (min-width: 631px) and (max-width: 830px) {
   #container{
        width:600px;
    }
}
@media screen and (min-width: 831px) and (max-width: 1030px) {
   #container{
        width:800px;
    }
}
Copy after login
<div>
Copy after login

This solution automatically adjusts the number of columns and rows based on the available browser width while maintaining the centered alignment and left-aligning the last row. It is IE9 compatible and suitable for implementation in production environments.

The above is the detailed content of How to Create a Centered Grid with a Left-Aligned Last Row Using Only CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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