Home >Web Front-end >CSS Tutorial >How to Center Align Container and Left Align Child Elements with Responsive Columns?

How to Center Align Container and Left Align Child Elements with Responsive Columns?

Susan Sarandon
Susan SarandonOriginal
2024-11-02 14:20:30282browse

How to Center Align Container and Left Align Child Elements with Responsive Columns?

Center Align Container and Left Align Child Elements

Your question aims to display a dynamic number of images on a web page that are centered and aligned in a row without resizing. The child elements should be grouped together and spaced apart.

To achieve this, you can leverage media queries in CSS. The provided code demonstrates how:

<code class="css">body {
    margin: 10px 0;
}
.outer {
    text-align: center;
}
.inner {
    font-size: 0; /* fix for inline gaps */
    display: inline-block;
    text-align: left;
}
.item {
    font-size: 16px; /* reset font size */
    display: inline-block;
    margin: 5px; /* gutter */
}
.item img {
    vertical-align: top;
}
@media (max-width: 551px) { /* ((100+5+5)x5)+1 */
    .inner {
        width: 440px; /* (100+5+5)x4 */
    }
}
@media (max-width: 441px) {
    .inner {
        width: 330px;
    }
}
@media (max-width: 331px) {
    .inner {
        width: 220px;
    }
}
@media (max-width: 221px) {
    .inner {
        width: 110px;
    }
}</code>

This code includes specific media queries for different screen sizes to ensure the inner div's width is adjusted dynamically. Note that you may need to adjust the gutter (margin) size based on your requirements.

The above is the detailed content of How to Center Align Container and Left Align Child Elements with Responsive Columns?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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