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

Box Sizing Property


The CSS box-sizing property is fundamental for creating predictable and consistent layouts. It defines how the total width and height of an element are calculated, preventing common layout issues.


Example 1: Default Behavior (content-box)

.content-box {
  /* The default value, where width and height apply only to the content area */
  box-sizing: content-box;
  width: 200px;
  padding: 20px;
  border: 5px solid black;
}

Explanation

With content-box, the actual width of this element is 250px (200px width + 40px padding + 10px border). This can make sizing elements tricky as you have to account for padding and border separately.


Example 2: Two Adjacent content-box Elements

.parent {
  width: 400px;
}
.child {
  /* Each child's calculated width is 202px (200px + 2px border) */
  box-sizing: content-box;
  width: 200px;
  border: 1px solid red;
  float: left;
}

Explanation

Two of these .child elements will not fit side-by-side inside the 400px .parent because their rendered width is 202px each, totaling 404px. This demonstrates a common layout challenge with the default box-sizing value.


Example 3: Border-box for Predictable Sizing

.border-box {
  /* Padding and border are included in the element's total width and height */
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  border: 5px solid blue;
}

Explanation

Using border-box, the total width of this element is exactly 200px. The padding and border are subtracted from the content area, making the layout far more intuitive and manageable.


Example 4: Two Adjacent border-box Elements

.parent {
  width: 400px;
}
.child {
  /* Each child's calculated width is exactly 200px */
  box-sizing: border-box;
  width: 200px;
  border: 1px solid green;
  float: left;
}

Explanation

Here, two .child elements fit perfectly within the 400px .parent container. Their total width remains 200px each because the border is included within that specified width.


Example 5: Universal Border-box Application

/* A common and highly recommended practice */
html {
  box-sizing: border-box;
}
*, *:before, *:after {
  box-sizing: inherit;
}

Explanation

This code snippet applies border-box to every element on the page. This modern CSS reset ensures a consistent box model throughout your entire project, preventing unexpected sizing issues.


Example 6: Responsive Grids with border-box

.grid-container {
  display: flex;
}
.grid-item {
  /* Flex items with padding and border maintain their percentage-based width */
  box-sizing: border-box;
  flex: 0 0 50%; /* Each item takes up 50% of the container's width */
  padding: 15px;
  border: 2px solid purple;
}

Explanation

When creating responsive grids, border-box is essential. It allows you to set percentage-based widths for grid items while also adding padding and borders without breaking the layout.


Example 7: Input Element Sizing

input[type="text"] {
  /* Ensures the input field's width is exactly as specified */
  box-sizing: border-box;
  width: 100%;
  padding: 10px;
}

Explanation

Form elements often have default padding and borders that can cause them to exceed their container's width. Applying border-box ensures the input field, including its padding, respects the width: 100% declaration.