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

Comments


CSS comments are used to add notes within your stylesheets, making your code more readable and understandable. They are ignored by the browser and do not affect the rendering of your web page.

Example 1: Single-line Comment

/* This is a single-line comment */
body {
  background-color: lightblue; /* This comment explains the background color */
}

Explanation This example demonstrates a single-line comment using /* ... */. It's useful for brief notes or explaining a single property.


Example 2: Multi-line Comment

/*
 * This is a multi-line comment.
 * It can span across multiple lines
 * to provide more detailed explanations.
 */
h1 {
  color: navy; /* Sets the heading text color */
}

Explanation This code showcases a multi-line comment, also enclosed within /* ... */. This format is ideal for longer descriptions or block-level comments.


Example 3: Commenting Out Code

p {
  font-size: 16px;
  /* color: gray; /* This line is commented out and won't be applied */ */
}

Explanation Here, a CSS property color: gray; is commented out. This technique is often used for debugging or temporarily disabling styles.


Example 4: Inline Comment

div {
  width: 100px; /* Specifies the width */
  height: 100px; /* Specifies the height */
}

Explanation This example shows inline comments placed on the same line as the CSS property. This is a compact way to explain individual declarations.


Example 5: Nested Comments (Avoid)

/*
  This is an outer comment.
  /* This is a nested comment - generally avoid this as it can break parsing */
*/
.container {
  border: 1px solid black;
}

Explanation While some parsers might handle this, nesting CSS comments can lead to unexpected behavior and is generally discouraged. Stick to clear, non-nested comment structures for better maintainability.