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

Font Size


The font-size property in CSS controls the size of the text. There are various units you can use to specify the size, including pixels (px), em units (em), rem units (rem), and percentages (%). Choosing the right unit is important for creating scalable and accessible web designs.


Example 1: Using Pixels

/* This rule targets all paragraph elements. */
p {
  /* It sets a fixed font size of 16 pixels. */
  font-size: 16px;
}

Explanation

This code sets an absolute font size of 16 pixels for all <p> elements. This size will not change based on the user's browser settings.


Example 2: Using Em Units

/* This rule applies to a div with the id "container". */
#container {
  font-size: 20px;
}

/* This rule targets paragraphs inside the container. */
#container p {
  /* The font size of the paragraph will be 1.2 times the font size of its parent, which is 24px (20px * 1.2). */
  font-size: 1.2em;
}

Explanation

This example uses em units, which are relative to the font size of the parent element. This allows for scalable text sizes within a specific section of your page.


Example 3: Using Rem Units

/* This rule sets the base font size for the entire document on the root HTML element. */
html {
  font-size: 16px;
}

/* This rule targets all h1 elements. */
h1 {
  /* The font size of the h1 will be 2 times the root font size, which is 32px (16px * 2). */
  font-size: 2rem;
}

Explanation

rem units are relative to the font size of the root <html> element. This makes it easier to manage and scale font sizes across your entire website consistently.


Example 4: Using Percentages

/* This rule applies to the body of the document. */
body {
  font-size: 100%; /* This is often equivalent to the browser's default of 16px. */
}

/* This rule targets elements with the class "large-text". */
.large-text {
  /* The font size will be 150% of the parent's font size. */
  font-size: 150%;
}

Explanation

Percentages work similarly to em units, as they are relative to the parent element's font size. A value of 100% is typically the browser's default font size.


Example 5: Using Viewport Width (vw)

/* This rule targets all h2 elements. */
h2 {
  /* The font size will be 5% of the viewport's width. */
  /* This creates responsive text that scales with the browser window size. */
  font-size: 5vw;
}

Explanation

Viewport width (vw) units make the font size responsive to the width of the browser window. This is a powerful tool for creating fluid and responsive typography.


Example 6: Using Keywords

/* This rule applies to elements with the class "small-text". */
.small-text {
  /* It uses a predefined keyword to set the font size. */
  font-size: small;
}

Explanation

CSS provides several keywords like small, medium, and large for setting font sizes. These are relative to the user's default font size.


Example 7: Using the calc() Function

/* This rule targets the main content area of a page. */
main {
  /* It calculates the font size by combining a relative unit (rem) with a fixed unit (px). */
  font-size: calc(1rem + 2px);
}

Explanation

The calc() function allows you to perform calculations to determine a CSS property value. In this case, it creates a flexible font size that has a base rem value with a small px adjustment.