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 Box


The CSS margin box is the outermost layer of the CSS box model, creating transparent space around an element, outside of its border. It is used to control the distance between an element and its neighboring elements.


Example 1: Setting a Uniform Margin

Code:

HTML

<div class="box uniform-margin">This box has a uniform margin.</div>

CSS

.uniform-margin {
  /* Sets a 20-pixel margin on all four sides */
  margin: 20px;
  background-color: lightblue;
  border: 1px solid black;
}

Explanation: This code applies a 20-pixel margin to all four sides of the div element. The margin shorthand property with a single value is a common way to create equal space around an element.


Example 2: Top and Bottom Margins

Code:

HTML

<div class="box vertical-margins">This box has top and bottom margins.</div>

CSS

.vertical-margins {
  /* Sets a 30px margin for top/bottom and 10px for left/right */
  margin: 30px 10px;
  background-color: lightcoral;
  border: 1px solid black;
}

Explanation: When two values are used with the margin property, the first value sets the top and bottom margins, and the second sets the left and right margins. This example creates more vertical space than horizontal space.


Example 3: Individual Side Margins

Code:

HTML

<div class="box individual-margins">This box has specific side margins.</div>

CSS

.individual-margins {
  /* Sets a specific margin for the top, right, bottom, and left sides */
  margin-top: 15px;
  margin-right: 25px;
  margin-bottom: 15px;
  margin-left: 25px;
  background-color: lightgreen;
  border: 1px solid black;
}

Explanation: This code utilizes individual margin properties (margin-top, margin-right, margin-bottom, margin-left) for precise control over each side's spacing. This is useful when asymmetrical spacing is required.


Example 4: Margin Shorthand with Four Values

Code:

HTML

<div class="box shorthand-four">A box with different margins on each side.</div>

CSS

.shorthand-four {
  /* Top: 10px, Right: 20px, Bottom: 30px, Left: 40px */
  margin: 10px 20px 30px 40px;
  background-color: gold;
  border: 1px solid black;
}

Explanation: The margin shorthand property can accept four values, which are applied in a clockwise direction starting from the top. This provides a concise way to set unique margins for all four sides of an element.


Example 5: Centering a Block Element

Code:

HTML

<div class="box centered-element">This block element is centered.</div>

CSS

.centered-element {
  /* Auto value for left and right margins centers the block element */
  width: 50%;
  margin: 0 auto;
  background-color: mediumpurple;
  border: 1px solid black;
}

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


Example 6: Negative Margins

Code:

HTML

<div class="box negative-margin">This box overlaps the one below.</div>
<div class="box">Another box.</div>

CSS

.negative-margin {
  /* A negative bottom margin pulls the subsequent element up */
  margin-bottom: -20px;
  background-color: sandybrown;
  border: 1px solid black;
}

Explanation: Negative margins can be used to pull elements closer together or to create overlapping effects. In this case, the negative bottom margin causes the second box to shift upwards, overlapping the first.


Example 7: Margin Collapsing

Code:

HTML

<div class="box collapse-one">Top box</div>
<div class="box collapse-two">Bottom box</div>

CSS

.collapse-one {
  /* This box has a bottom margin of 20px */
  margin-bottom: 20px;
  background-color: lightseagreen;
}
.collapse-two {
  /* This box has a top margin of 30px */
  margin-top: 30px;
  background-color: skyblue;
}

Explanation: When the top margin of one element touches the bottom margin of another, the larger of the two margins is used. Here, the 30px top margin of the second box "collapses" with the 20px bottom margin of the first, resulting in a single 30px margin between them.