CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Grid Display


The display: grid property is a powerful CSS layout model that allows you to create complex, two-dimensional layouts with rows and columns. It provides a more straightforward and flexible way to design web page structures compared to older methods like floats or positioning. By declaring display: grid on a container element, you can then define the structure and arrangement of its direct children, known as grid items.


Example 1: Basic Grid Container

.grid-container {
  display: grid; /* Defines the element as a grid container */
  grid-template-columns: auto auto auto; /* Creates three columns of equal width */
  background-color: #2196F3;
  padding: 10px;
}

.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(0, 0, 0, 0.8);
  padding: 20px;
  font-size: 30px;
  text-align: center;
}

Explanation

This code establishes a grid container with three columns. The grid-template-columns property with the value auto auto auto tells the browser to automatically create three columns that share the available space equally. The direct children of .grid-container will become grid items and flow into the available grid cells.


Example 2: Sizing Columns with fr Units

.grid-container {
  display: grid; /* Activates the grid layout module */
  grid-template-columns: 1fr 2fr 1fr; /* Distributes space in a 1:2:1 ratio */
  gap: 10px; /* Adds a 10px gap between grid items */
}

Explanation

The fr unit represents a fraction of the available space in the grid container. In this example, grid-template-columns: 1fr 2fr 1fr; divides the container into four fractional units. The first and third columns each take up one fraction, while the second column is twice as wide, taking up two fractions of the space.


Example 3: Explicitly Placing Items with grid-column and grid-row

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* Creates 3 flexible columns */
}

.item1 {
  grid-column: 1 / 3; /* Item starts at column line 1 and ends at column line 3 */
  grid-row: 1; /* Item is placed in the first row */
}

Explanation

You can precisely place grid items using grid-column and grid-row. Here, .item1 is instructed to start at the first vertical grid line and span across to the third, effectively occupying the space of the first two columns. It is also explicitly placed in the first row.


Example 4: Naming Grid Areas with grid-template-areas

.grid-container {
  display: grid;
  grid-template-areas:
    'header header header'
    'menu main right'
    'footer footer footer'; /* Defines a layout with named areas */
}

.item-header { grid-area: header; } /* Assigns this item to the 'header' area */
.item-menu { grid-area: menu; }
.item-main { grid-area: main; }
.item-right { grid-area: right; }
.item-footer { grid-area: footer; }

Explanation

The grid-template-areas property allows you to name grid cells and create a visual representation of your layout in the CSS. Each string represents a row, and the words define the named areas. You then assign elements to these named areas using the grid-area property.


Example 5: Using minmax() for Responsive Columns

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); /* Creates responsive columns */
  gap: 15px;
}

Explanation

This example creates a responsive grid without media queries. repeat(auto-fit, ...) tells the grid to fit as many columns as possible. The minmax(200px, 1fr) function ensures each column will be at least 200px wide but can grow to an equal fraction of the available space if there's room.


Example 6: Aligning Items with justify-items and align-items

.grid-container {
  display: grid;
  grid-template-columns: 150px 150px;
  height: 200px;
  justify-items: center; /* Centers items horizontally within their cells */
  align-items: center; /* Centers items vertically within their cells */
}

Explanation

The justify-items property aligns the content inside a grid item along the row (inline) axis, while align-items aligns the content along the column (block) axis. Setting both to center will perfectly center each grid item within its designated grid cell.


Example 7: Controlling Content Flow with grid-auto-flow

.grid-container {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(2, 50px);
  grid-auto-flow: column; /* Fills the grid column by column */
}

Explanation

By default, grid items fill the grid row by row. The grid-auto-flow: column; property changes this behavior, causing items to be placed in order along the column axis first. If there aren't enough explicitly defined columns, new columns will be created automatically.