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

Border Box


The CSS border-box sizing model is a crucial concept for intuitive layout control. When you set an element's box-sizing property to border-box, the width and height properties include the content, padding, and border. This prevents the element from growing larger than you intended when you add padding or a border.


Example 1: Basic Border Box

HTML

<div class="box border-box-example">Content</div>

CSS

/* This CSS demonstrates the effect of border-box. */
.box {
  width: 200px;
  padding: 20px;
  border: 5px solid black;
}

.border-box-example {
  box-sizing: border-box; /* The total width remains 200px */
}

Explanation With box-sizing: border-box;, the div's total width is exactly 200px. The padding and border are calculated within this width, making layout predictions simpler and more consistent.


Example 2: Comparing Box Models

Code:

HTML

<div class="box content-box">Content-Box</div>
<div class="box border-box">Border-Box</div>

CSS

/* This code compares content-box and border-box. */
.box {
  width: 150px;
  height: 150px;
  padding: 15px;
  border: 10px solid green;
  margin-bottom: 10px;
}

.content-box {
  box-sizing: content-box; /* Default model, width is 150px + padding + border */
}

.border-box {
  box-sizing: border-box; /* Width is 150px total */
}

Explanation The first box's total width is 200px (150px + 30px padding + 20px border), while the second box's total width is precisely 150px because border-box contains the padding and border within the specified width.


Example 3: Form Inputs

Code:

HTML

<input type="text" class="input-style" placeholder="Your Name">

CSS

/* Using border-box for consistent input field sizing. */
.input-style {
  width: 100%;
  padding: 10px;
  border: 2px solid #ccc;
  box-sizing: border-box; /* Ensures padding and border are inside the width */
}

Explanation By applying border-box to the input, it will span the full width of its container without overflowing. The padding and border are accommodated internally, ensuring predictable sizing in forms.


Example 4: Responsive Grids

Code:

HTML

<div class="grid-container">
  <div class="grid-item">Item 1</div>
  <div class="grid-item">Item 2</div>
</div>

CSS

/* Border-box is essential for creating reliable grid systems. */
.grid-container {
  display: flex;
}

.grid-item {
  width: 50%;
  padding: 20px;
  border: 1px solid blue;
  box-sizing: border-box; /* Allows two 50% items to fit perfectly */
}

Explanation border-box is critical for responsive grids. Here, two elements set to 50% width fit perfectly side-by-side because their padding and borders do not add to their total width, preventing a layout break.


Example 5: Padded Buttons

Code:

HTML

<button class="styled-button">Click Me</button>

CSS

/* Consistent button sizing with border-box. */
.styled-button {
  width: 120px;
  padding: 15px 25px;
  border: 3px solid #4CAF50;
  box-sizing: border-box; /* The button's width remains 120px */
  text-align: center;
}

Explanation This example ensures that the button maintains a fixed width of 120px, regardless of the padding applied. This is useful for creating a uniform set of buttons in a user interface.


Example 6: Images with Borders

Code:

HTML

<img src="https://via.placeholder.com/200" alt="placeholder" class="image-border-box">

CSS

/* Applying border-box to an image to control its total size. */
.image-border-box {
  width: 200px;
  height: auto;
  padding: 10px;
  border: 5px solid red;
  background-color: #f0f0f0; /* So padding is visible */
  box-sizing: border-box; /* The final width is 200px */
}

Explanation Here, the border-box property ensures that the total width of the image element, including its padding and border, is constrained to 200px. This prevents the element from unexpectedly expanding and disrupting the page layout.


Example 7: Universal Border-Box

Code:

CSS

/* A common and highly recommended CSS reset. */
html {
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit; /* All elements now inherit border-box from html */
}

Explanation This universal approach sets border-box on the html element and has all other elements inherit it. This is a modern best practice that simplifies layout management across an entire website, making it more intuitive.