Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

CSS Flexbox and Grid

Lesson 11 of 34 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

CSS Flexbox and Grid

CSS Flexbox

Flexbox (Flexible Box Layout) is a one-dimensional layout system for arranging items in a row or column.

.container {
    display: flex;
    flex-direction: row;        /* row | column | row-reverse | column-reverse */
    justify-content: center;    /* main axis alignment */
    align-items: center;        /* cross axis alignment */
    flex-wrap: wrap;            /* allow wrapping */
    gap: 16px;
}

.item {
    flex: 1;                    /* grow and shrink equally */
    flex-basis: 200px;
    align-self: flex-start;
}

justify-content Values

ValueEffect
flex-startPack at start
flex-endPack at end
centerCenter items
space-betweenEqual space between
space-aroundEqual space around
space-evenlyEven spacing everywhere

CSS Grid

CSS Grid is a two-dimensional layout system for rows AND columns.

.grid-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);   /* 3 equal columns */
    grid-template-rows: auto;
    gap: 20px;
    padding: 20px;
}

/* Span multiple columns */
.header {
    grid-column: 1 / -1;    /* spans all columns */
}

.sidebar {
    grid-column: 1 / 2;
    grid-row: 2 / 4;
}

Named Grid Areas

.layout {
    display: grid;
    grid-template-areas:
        "header header header"
        "sidebar main main"
        "footer footer footer";
    grid-template-columns: 200px 1fr 1fr;
}

header { grid-area: header; }
.sidebar { grid-area: sidebar; }
main { grid-area: main; }
footer { grid-area: footer; }

Flexbox vs Grid

FeatureFlexboxGrid
Dimension1D (row or column)2D (rows and columns)
Use caseComponents, navbarsPage layouts
Browser supportExcellentExcellent
Alignment controlGoodExcellent

Responsive Layout Example

.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 24px;
}

This creates a responsive grid that automatically adjusts columns based on available space.

Key Takeaway: Flexbox excels at one-dimensional layouts (navbars, card rows). CSS Grid is ideal for two-dimensional page layouts. Combining both gives you full layout control.