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 Basis


The flex-basis property defines the default size of an element before the remaining space is distributed. It can be a length (e.g., px, %, em) or the keyword auto, which respects the item's width or height property.

Example 1: Setting a Pixel Basis

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

.flex-item {
  height: 100px;
  background-color: peru;
  border: 1px solid #000;
  flex-basis: 150px; /* Sets the initial size of the item to 150px */
}

Explanation

Here, flex-basis: 150px sets the initial main size of the flex items to 150 pixels. If there is remaining space, flex-grow would apply; if there's not enough space, flex-shrink would come into play.


Example 2: Using auto

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

.flex-item {
  width: 120px;
  height: 100px;
  background-color: plum;
  border: 1px solid #000;
  flex-basis: auto; /* The item's width property is used as the basis */
}

Explanation

With flex-basis: auto, the item's width of 120px is used as its initial size. This is the default behavior if flex-basis is not explicitly set.


Example 3: Percentage Basis

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

.flex-item {
  height: 100px;
  background-color: powderblue;
  border: 1px solid #000;
  flex-basis: 25%; /* Each item's initial size is 25% of the container's width */
}

Explanation

Using a percentage for flex-basis makes the initial size of flex items relative to their container's size. Here, each of the four items will have a base width of 25% of the 800px container.


Example 4: flex-basis with flex-grow

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

.flex-item {
  height: 100px;
  background-color: rosybrown;
  border: 1px solid #000;
  flex-basis: 100px;
  flex-grow: 1; /* Items start at 100px and then share the remaining space */
}

Explanation

In this scenario, each item starts with a flex-basis of 100px. The remaining space in the container is then distributed equally among the items because their flex-grow is set to 1.


Example 5: flex-basis with flex-shrink

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

.flex-item {
  height: 100px;
  background-color: darkkhaki;
  border: 1px solid #000;
  flex-basis: 200px; /* Initial size is larger than what can fit */
  flex-shrink: 1;
}

Explanation

The total flex-basis of the items (600px) is greater than the container's width (400px). Because flex-shrink is 1, the items will shrink from their 200px basis to fit inside the container.


Example 6: flex-basis: 0

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

.flex-item {
  height: 100px;
  background-color: tan;
  border: 1px solid #000;
  flex-basis: 0;
  flex-grow: 1;
}

.double {
  flex-grow: 2;
}

Explanation

Setting flex-basis: 0 tells the items to disregard their content size. The entire space of the container is then distributed according to the flex-grow factor, making the .double item twice the size of the others.


Example 7: flex-basis and content

.flex-container {
  display: flex;
  width: auto; /* Container width is determined by content */
  border: 1px solid #000;
}

.flex-item {
  height: 100px;
  background-color: thistle;
  border: 1px solid #000;
  flex-basis: content; /* The item's size is based on its content */
}

Explanation

The content keyword for flex-basis sizes the flex item based on its content. This is useful when you want the item's size to be determined by the text or other elements inside it.