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 Template Columns


The grid-template-columns property defines the size and number of columns in a CSS Grid Layout. You specify a space-separated list of values to set the width of each column track.


Example 1: Using Pixels

/* Defines a grid with three columns */
.grid-container {
  display: grid;
  /* First column is 100px, second is 200px, and third is 150px wide */
  grid-template-columns: 100px 200px 150px;
}

Explanation This code creates a grid with three columns of fixed widths. Using pixel values provides precise control over the column sizes, but they are not responsive.


Example 2: Using Percentages

/* Defines a responsive three-column grid */
.grid-container {
  display: grid;
  /* Columns take up 25%, 50%, and 25% of the container's width respectively */
  grid-template-columns: 25% 50% 25%;
}

Explanation This example uses percentages to create a fluid grid layout. The column widths will scale relative to the total width of the grid container.


Example 3: Using Fractional Units (fr)

/* Defines a flexible three-column grid */
.grid-container {
  display: grid;
  /* The available space is divided into 4 parts; 1fr gets 1 part, 2fr gets 2 */
  grid-template-columns: 1fr 2fr 1fr;
}

Explanation The fr unit represents a fraction of the available space in the grid container. This allows for flexible columns that automatically adjust their size.


Example 4: Using the repeat() function

/* Defines a grid with 4 equal columns */
.grid-container {
  display: grid;
  /* Creates 4 columns, each taking up 1 fraction of the available space */
  grid-template-columns: repeat(4, 1fr);
}

Explanation The repeat() function is a concise way to define multiple columns or rows with the same size. This is efficient for creating uniform grid structures.


Example 5: Using minmax()

/* Defines a responsive column with a minimum and maximum size */
.grid-container {
  display: grid;
  /* The column will be at least 100px wide, and at most 1fr of the space */
  grid-template-columns: minmax(100px, 1fr);
}

Explanation The minmax() function defines a size range for a track. This is useful for creating responsive layouts that prevent content from becoming too compressed or too stretched.


Example 6: Using the auto keyword

/* Defines columns based on content size */
.grid-container {
  display: grid;
  /* The first and third columns are fixed, the middle one sizes to its content */
  grid-template-columns: 100px auto 100px;
}

Explanation The auto keyword sizes a column based on the width of its content. This ensures the column is just wide enough to hold its content without wrapping.


Example 7: Mixing Units

/* Defines a complex grid with mixed units */
.grid-container {
  display: grid;
  /* A combination of fixed, fractional, and content-based sizing */
  grid-template-columns: 150px 1fr auto;
}

Explanation You can mix different units like pixels, fr, and auto to create sophisticated and responsive grid layouts that adapt to various screen sizes and content lengths.