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 Shorthand


The flex property is a shorthand for flex-grow, flex-shrink, and flex-basis. It allows you to set all three properties at once, providing a more concise way to write your CSS.

Example 1: flex: 1

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

.flex-item {
  height: 100px;
  background-color: tomato;
  border: 1px solid #000;
  flex: 1; /* equivalent to flex: 1 1 0% */
}

Explanation

flex: 1 is a common shorthand that translates to flex-grow: 1, flex-shrink: 1, and flex-basis: 0%. This makes the flex items flexible and share the available space equally.


Example 2: flex: 0 0 200px

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

.flex-item {
  height: 100px;
  background-color: turquoise;
  border: 1px solid #000;
  flex: 0 0 200px; /* flex-grow: 0, flex-shrink: 0, flex-basis: 200px */
}

Explanation

This shorthand creates an inflexible item. flex-grow: 0 prevents it from growing, flex-shrink: 0 prevents it from shrinking, and flex-basis: 200px gives it a fixed size of 200px.


Example 3: flex: auto

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

.flex-item {
  width: 150px;
  height: 100px;
  background-color: violet;
  border: 1px solid #000;
  flex: auto; /* equivalent to flex: 1 1 auto */
}

Explanation

flex: auto is equivalent to flex: 1 1 auto. The items can grow and shrink, and their initial size is determined by their width or height property.


Example 4: flex: 2

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

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

.item-1 {
  flex: 1; /* flex: 1 1 0% */
}

.item-2 {
  flex: 2; /* flex: 2 1 0% */
}

Explanation

When a single number is used for flex, it represents the flex-grow value. Here, .item-2 will be twice as wide as .item-1 because its flex-grow is 2.


Example 5: flex: none

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

.flex-item {
  width: 200px;
  height: 100px;
  background-color: yellowgreen;
  border: 1px solid #000;
  flex: none; /* equivalent to flex: 0 0 auto */
}

Explanation

The none keyword for the flex property is equivalent to flex: 0 0 auto. This creates an inflexible item that will not grow or shrink, with its size determined by its width or height.


Example 6: Two Value Syntax

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

.flex-item {
  height: 100px;
  background-color: teal;
  border: 1px solid #000;
  flex: 1 200px; /* flex-grow: 1, flex-basis: 200px, flex-shrink defaults to 1 */
}

Explanation

When two values are provided, the first is flex-grow and the second is flex-basis. flex-shrink defaults to 1. The items will start at 200px and grow to fill the container.


Example 7: Responsive Flex Shorthand

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

.flex-item {
  height: 100px;
  background-color: slateblue;
  border: 1px solid #000;
  flex: 1 1 300px; /* Items will try to be 300px, but can grow and shrink */
}

@media (max-width: 768px) {
  .flex-item {
    flex: 1 1 100%; /* On smaller screens, items take up the full width */
  }
}

Explanation

This example demonstrates how the flex shorthand can be used in media queries for responsive design. On larger screens, items have a basis of 300px, but on smaller screens, they take up the full width of the container.