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

Line Height


The line-height property in CSS controls the amount of space between lines of text. It is crucial for readability and creating a visually pleasing layout. It can be set using various units, including unitless values, pixels, and percentages.


Example 1: Using a Unitless Value

/* This rule targets all paragraph elements. */
p {
  /* It sets the line height to be 1.5 times the font size of the element. */
  line-height: 1.5;
}

Explanation

Using a unitless value is the recommended best practice for line-height as it is relative to the element's font-size. This ensures that the line height scales appropriately if the font size changes.


Example 2: Using Pixels

/* This rule applies to elements with the class "fixed-line-height". */
.fixed-line-height {
  /* It sets a fixed line height of 24 pixels. */
  line-height: 24px;
}

Explanation

Setting line-height in pixels provides a fixed space between lines, which might not be ideal for responsive or accessible designs.


Example 3: Using Percentages

/* This rule targets the body of the document. */
body {
  /* The line height will be 150% of the element's font size. */
  line-height: 150%;
}

Explanation

Percentages for line-height are relative to the element's font-size, similar to em units.


Example 4: Using the normal Keyword

/* This rule applies to all list items. */
li {
  /* It resets the line height to the browser's default, which is usually around 1.2. */
  line-height: normal;
}

Explanation

The normal keyword sets the line-height to the browser's default value. The exact value can vary between browsers.


Example 5: Using em Units

/* This rule targets all blockquote elements. */
blockquote {
  /* The line height will be 1.6 times the font size of the blockquote. */
  line-height: 1.6em;
}

Explanation

em units for line-height are also relative to the element's font-size. However, unitless values are generally preferred to avoid compounding issues in nested elements.


Example 6: Vertically Centering Single-Line Text

/* This rule targets a button element. */
.button {
  height: 50px;
  /* By setting the line-height equal to the height, the text inside will be vertically centered. */
  line-height: 50px;
}

Explanation

A common technique for vertically centering a single line of text within a container is to set the line-height equal to the height of the container.


Example 7: Using rem Units

/* This rule targets a specific div. */
div {
  /* The line height will be 2 times the root font size. */
  line-height: 2rem;
}

Explanation

Using rem units for line-height bases the line spacing on the root <html> element's font size, which can help with overall consistency in your design.