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

Absolute Units


Understanding how to size elements is crucial for responsive web design and creating visually appealing layouts. We'll explore both absolute (and relative) length units with practical code examples.

Length units in CSS specify the size of elements and their properties. They are fundamental for controlling layout, typography, and spacing in your web designs. Choosing the right unit is key for adaptability and maintainability.

Absolute: px, pt, cm, in

Absolute length units are fixed and represent a physical measurement. They are not relative to anything else and remain the same size regardless of the screen size or resolution. While useful for specific print-like layouts, they are generally less flexible for responsive web design.

Example 1: Using px (Pixels)

px (pixels) are the most common absolute unit, representing a single point on a display. They are widely used for precise control over element sizes, especially for borders, shadows, and spacing.

.pixel-box {
  width: 200px; /* Sets the width to exactly 200 pixels */
  height: 100px; /* Sets the height to exactly 100 pixels */
  border: 1px solid black; /* A 1-pixel thick border */
}

Explanation This code creates a box with a fixed width of 200 pixels and a height of 100 pixels. The border is also set to a precise 1 pixel, ensuring consistent sizing across different viewports.


Example 2: Using px for Font Size

px can also be used for font-size, providing exact control over text rendering. While precise, it's often less adaptable for users who might need to scale text for accessibility.

.pixel-text {
  font-size: 16px; /* Sets the font size to exactly 16 pixels */
  margin-bottom: 10px; /* Adds 10 pixels of bottom margin */
}

Explanation Here, the text inside .pixel-text will always be rendered at 16 pixels. This is useful for design systems requiring pixel-perfect typography.


Example 3: Using pt (Points)

pt (points) are primarily used in print media, where 1 point equals 1/72 of an inch. In web design, px is generally preferred, but pt can be seen in older stylesheets or for specific print-oriented layouts.

.point-paragraph {
  font-size: 12pt; /* Sets the font size to 12 points */
  line-height: 1.5; /* Line height is relative to the font size */
}

Explanation This example demonstrates setting font size using points, which is more common in print design than web design. The line-height ensures readability.


Example 4: Using pt for Layouts (Less Common)

While not typical, pt can theoretically be used for layout dimensions, though px or relative units are far more common for screen-based designs.

.point-div {
  width: 72pt; /* Roughly 1 inch wide */
  height: 36pt; /* Roughly 0.5 inches tall */
  background-color: lightblue;
}

Explanation This illustrates how pt can define div dimensions, although this approach is rarely seen in modern responsive web development due to its fixed nature.


Example 5: Using cm (Centimeters)

cm (centimeters) are absolute units. 1cm equals 37.8 pixels. They are rarely used in web development for screen display due to their physical nature and lack of responsiveness.

.cm-ruler {
  width: 5cm; /* Sets width to 5 centimeters */
  height: 2cm; /* Sets height to 2 centimeters */
  border: 0.1cm solid red; /* A thin red border */
}

Explanation This code snippet defines a box with dimensions in centimeters. While precise, this unit is generally avoided for responsive web layouts as it doesn't adapt to screen sizes.


Example 6: Using cm for Print Stylesheets

cm can be more relevant in print stylesheets where physical dimensions are important for output on paper.

@media print {
  .print-page-margin {
    margin: 2cm; /* Sets page margins for printing */
  }
}

Explanation This demonstrates using cm within a print media query to define margins when a page is printed, ensuring consistent physical spacing on paper.


Example 7: Using in (Inches)

in (inches) are another absolute unit, with 1 inch equating to 96 pixels. Similar to centimeters, inches are not commonly used for screen-based web design.

.inch-block {
  width: 1in; /* Sets width to 1 inch */
  height: 0.5in; /* Sets height to 0.5 inches */
  background-color: lightgreen;
}

Explanation This example shows defining a block's dimensions using inches. This method is rigid and unsuitable for responsive web design, making it a rare choice for screen displays.


Example 8: Using in for Print Layouts

Like cm and pt, in can find some utility in print-specific CSS, particularly when designing for specific paper sizes.

@page {
  size: letter; /* Defines page size for printing */
  margin: 1in; /* Sets 1-inch margins around the printed content */
}

Explanation This CSS snippet, typically used within an @page rule, sets the print margin of a document to 1 inch, useful for controlling the layout of printed web pages.