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

Flex Grow


Flex item properties are CSS properties that are applied to the children of a flex container. They control how individual flex items behave within the flexbox layout, determining their size, order, and alignment. Understanding these properties is crucial for building responsive and complex web layouts.


The flex-grow property specifies how much a flex item will grow relative to the rest of the flex items in the flex container when there is extra space. A value of 0, the default, prevents the item from growing.

Example 1: Basic Growth

.flex-container {
  display: flex;
  width: 600px;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  height: 100px;
  background-color: lightblue;
  border: 1px solid #000;
}

.grow-item {
  flex-grow: 1; /* This item will take up the available space */
}

Explanation

In this example, the .grow-item has a flex-grow value of 1. This allows it to expand and fill any remaining space within the .flex-container, while the other flex items maintain their original width.


Example 2: Proportional Growth

.flex-container {
  display: flex;
  width: 800px;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  height: 100px;
  background-color: lightcoral;
  border: 1px solid #000;
}

.item-1 {
  flex-grow: 1; /* Grows by one proportion */
}

.item-2 {
  flex-grow: 2; /* Grows by two proportions, making it twice as large as item-1 */
}

Explanation

Here, both .item-1 and .item-2 can grow. However, .item-2 will grow twice as much as .item-1 because its flex-grow value is double, demonstrating how available space is distributed proportionally.


Example 3: Equal Growth for All Items

.flex-container {
  display: flex;
  width: 100%;
  border: 1px solid #000;
}

.flex-item {
  height: 100px;
  background-color: lightgreen;
  border: 1px solid #000;
  flex-grow: 1; /* All items will grow equally */
}

Explanation

By setting flex-grow: 1 for all .flex-item elements, they will each expand equally to fill the container. This results in items of the same size, regardless of their initial content.


Example 4: No Growth

.flex-container {
  display: flex;
  width: 500px;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  height: 100px;
  background-color: lightgoldenrodyellow;
  border: 1px solid #000;
  flex-grow: 0; /* This is the default value, items will not grow */
}

Explanation

With flex-grow: 0, none of the flex items will expand to fill the available space in the container. The extra space will remain empty, which is the default behavior if flex-grow is not specified.


Example 5: Growth with Different Basis

.flex-container {
  display: flex;
  width: 700px;
  border: 1px solid #000;
}

.flex-item {
  height: 100px;
  background-color: lightpink;
  border: 1px solid #000;
}

.item-a {
  width: 150px;
  flex-grow: 1;
}

.item-b {
  width: 100px;
  flex-grow: 1;
}

Explanation

Even though both items have flex-grow: 1, they will not have the same final width. The growth is distributed after their initial width is accounted for, so .item-a will be larger than .item-b.


Example 6: Fractional Growth Values

.flex-container {
  display: flex;
  width: 600px;
  border: 1px solid #000;
}

.flex-item {
  width: 80px;
  height: 100px;
  background-color: lightseagreen;
  border: 1px solid #000;
}

.one {
  flex-grow: 0.5; /* Grows at half the rate of the next item */
}

.two {
  flex-grow: 1;
}

Explanation

flex-grow accepts fractional values. In this case, .one will grow, but only half as much as .two for every unit of available space, leading to a smaller final size.


Example 7: Dynamic Growth on Hover

.flex-container {
  display: flex;
  width: 500px;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  height: 100px;
  background-color: lightslategray;
  border: 1px solid #000;
  transition: flex-grow 0.3s ease; /* Smooth transition for the growth effect */
}

.flex-item:hover {
  flex-grow: 1; /* The item grows when the mouse hovers over it */
}

Explanation

This example demonstrates a common interactive effect. When a user hovers over a .flex-item, its flex-grow property changes to 1, causing it to expand and fill the available space smoothly.