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

Padding


In CSS, padding is the space between an element's content and its border. It is a fundamental property for controlling the layout and spacing of elements on a webpage, ensuring content doesn't sit directly against the container's edge.


Example 1: Padding on all sides

/* This rule applies 20 pixels of padding to all four sides of any <p> element. */
p {
  padding: 20px;
}

Explanation

This code adds a uniform 20-pixel space on the top, right, bottom, and left of the paragraph's content, pushing the border outwards.


Example 2: Individual side padding

/* This rule individually sets the padding for each side of the <div> element. */
div {
  padding-top: 10px;
  padding-right: 20px;
  padding-bottom: 15px;
  padding-left: 25px;
}

Explanation

This demonstrates how to apply different padding values to each side of an element for more granular control over the internal spacing.


Example 3: Shorthand with four values

/* This shorthand rule applies padding in a clockwise direction: top, right, bottom, left. */
.four-value-box {
  padding: 10px 20px 15px 25px;
}

Explanation

This is a more concise way to set all four padding values in a single declaration, following the order: top, right, bottom, and then left.


Example 4: Shorthand with three values

/* This shorthand applies 10px to the top, 20px to the left and right, and 15px to the bottom. */
.three-value-box {
  padding: 10px 20px 15px;
}

Explanation

When three values are used, the first sets the top padding, the second sets the left and right padding, and the third sets the bottom padding.


Example 5: Shorthand with two values

/* This shorthand applies 10px to the top and bottom, and 20px to the left and right. */
.two-value-box {
  padding: 10px 20px;
}

Explanation

Using two values, the first value is applied to the top and bottom, while the second value is applied to the left and right sides.


Example 6: Padding with different units

/* This rule uses a percentage for horizontal padding and pixels for vertical padding. */
.mixed-units {
  padding: 20px 5%;
}

Explanation

This example shows the flexibility of CSS padding by mixing units. The vertical padding is a fixed 20 pixels, while the horizontal padding is a responsive 5% of the containing element's width.


Example 7: The padding-inline logical property

/* This logical property applies padding to the start and end of the element in its writing mode. */
.logical-padding {
  padding-inline: 30px;
}

Explanation

Logical properties like padding-inline are useful for multi-lingual sites. In a left-to-right language, this applies padding to the left and right. In a right-to-left language, it would still correctly apply to the left and right.