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 Shrink


The flex-shrink property determines how much a flex item will shrink relative to other flex items when the container is too small to fit all items. The default value is 1, meaning items will shrink.

Example 1: Basic Shrinking

.flex-container {
  display: flex;
  width: 400px; /* Container is smaller than the combined width of items */
  border: 1px solid #000;
}

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

.shrink-item {
  flex-shrink: 2; /* This item will shrink more than others */
}

Explanation

When the .flex-container is not wide enough, .shrink-item will shrink at twice the rate of the other items because its flex-shrink value is 2, making it smaller.


Example 2: Proportional Shrinking

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

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

.item-1 {
  flex-shrink: 1; /* Shrinks by one proportion */
}

.item-2 {
  flex-shrink: 3; /* Shrinks three times as much as item-1 */
}

Explanation

Here, .item-2 will shrink three times as much as .item-1 when the container is overflowing. This demonstrates how flex-shrink distributes the negative space proportionally.


Example 3: Preventing Shrinking

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

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

.no-shrink {
  flex-shrink: 0; /* This item will not shrink and will overflow the container if necessary */
}

Explanation

By setting flex-shrink: 0, the .no-shrink item will maintain its original width even if the container is too small. This may cause the item to overflow its container.


Example 4: Equal Shrinking

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

.flex-item {
  width: 150px;
  height: 100px;
  background-color: gold;
  border: 1px solid #000;
  flex-shrink: 1; /* All items shrink equally, which is the default */
}

Explanation

With flex-shrink: 1 applied to all items, they will all shrink by the same amount when the container is smaller than their combined size. This ensures an even reduction in width across all items.


Example 5: Shrinking with Different Basis

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

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

.item-a {
  width: 300px;
  flex-shrink: 1;
}

.item-b {
  width: 200px;
  flex-shrink: 1;
}

Explanation

Even with the same flex-shrink value, the amount of shrinkage is also influenced by the item's initial size. The larger item, .item-a, will shrink more in absolute terms than the smaller .item-b.


Example 6: Fractional Shrink Values

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

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

.one {
  flex-shrink: 0.5; /* Shrinks at half the rate of the other item */
}

.two {
  flex-shrink: 1;
}

Explanation

flex-shrink also accepts fractional values. In this scenario, .one will shrink less aggressively than .two because its shrink factor is smaller.


Example 7: Shrinking with Flex Wrap

.flex-container {
  display: flex;
  flex-wrap: wrap; /* Allows items to wrap to the next line */
  width: 300px;
  border: 1px solid #000;
}

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

Explanation

When flex-wrap is set to wrap, items will move to the next line instead of shrinking if the container is too narrow. In this case, flex-shrink will have no effect as the items will wrap before they need to shrink.