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

Margin


The CSS margin property is fundamental for controlling the space outside of an element's border. It creates a transparent gap between an element and its neighboring elements, playing a crucial role in layout and visual spacing on a webpage.


Example 1: Setting a Single Margin for All Sides

.box {
  /* This shorthand applies a 20px margin to the top, right, bottom, and left sides of the element. */
  margin: 20px;
  background-color: lightblue;
  border: 1px solid black;
}

Explanation: This code uses the shorthand margin property to apply a uniform 20-pixel margin around the entire element with the class .box. This is the simplest way to create equal spacing on all four sides.


Example 2: Setting Individual Margins for Each Side

.card {
  /* This demonstrates setting a different margin value for each side. */
  margin-top: 10px;
  margin-right: 20px;
  margin-bottom: 15px;
  margin-left: 25px;
  background-color: lightcoral;
  border: 1px solid black;
}

Explanation: For more precise control, you can use the longhand properties margin-top, margin-right, margin-bottom, and margin-left. This example applies distinct margin values to each side of the .card element.


Example 3: Shorthand with Four Values

.container {
  /* Shorthand for top, right, bottom, left (clockwise). */
  margin: 10px 20px 15px 5px;
  background-color: lightgreen;
  border: 1px solid black;
}

Explanation: The margin shorthand can accept four values, which are applied in a clockwise direction starting from the top: top (10px), right (20px), bottom (15px), and left (5px). This is a concise way to set different values for each side.


Example 4: Shorthand with Two Values

.item {
  /* Sets top/bottom to 10px and right/left to 30px. */
  margin: 10px 30px;
  background-color: gold;
  border: 1px solid black;
}

Explanation: When two values are provided to the margin shorthand, the first value sets the margin-top and margin-bottom, while the second value sets the margin-left and margin-right. This is useful for creating vertical and horizontal symmetry.


Example 5: Centering a Block Element with auto

.centered-block {
  /* 'auto' for left/right margin centers the block element horizontally. */
  width: 80%;
  margin: 0 auto;
  background-color: lightpink;
  border: 1px solid black;
}

Explanation: Setting the left and right margins to auto is a common and effective technique for horizontally centering a block-level element within its container. The browser calculates equal margins for both sides.


Example 6: Using Negative Margins

.overlapping-element {
  /* A negative top margin pulls the element up, causing it to overlap the element above it. */
  margin-top: -30px;
  background-color: lightseagreen;
  border: 1px solid black;
  position: relative; /* Often used with negative margins to control stacking. */
  z-index: 1;
}

Explanation: Negative margins can be used to pull an element in a specific direction, potentially causing it to overlap other elements. In this case, margin-top: -30px moves the element upwards by 30 pixels.


Example 7: Collapsing Margins

.element-one {
  margin-bottom: 25px; /* This margin will collapse with the one below. */
  background-color: lightslategrey;
  border: 1px solid black;
  height: 50px;
}
.element-two {
  margin-top: 30px; /* The actual space will be 30px, not 55px. */
  background-color: lightsteelblue;
  border: 1px solid black;
  height: 50px;
}

Explanation: Collapsing margins is a behavior where the adjacent vertical margins of two block-level elements combine. The resulting margin will be the size of the larger of the two individual margins, not their sum.