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 Family


The font-family property in CSS is used to specify a prioritized list of one or more font family names and/or generic family names for an element. The browser will go through the list of fonts from left to right and use the first font it can find on the system. This ensures that if a user's system does not have the primary font, a fallback font will be used instead.


Example 1: Using a Specific Font

/* This CSS rule targets all paragraph elements. */
p {
  /* It sets the font for the paragraphs to "Arial". */
  font-family: "Arial";
}

Explanation

This code sets the font for all <p> elements to "Arial". If Arial is not available, the browser will use its default serif font.


Example 2: Font Family with a Fallback

/* This rule applies to all heading level 1 elements. */
h1 {
  /* It specifies "Helvetica" as the preferred font. */
  /* If Helvetica is not available, the browser will use any sans-serif font. */
  font-family: "Helvetica", sans-serif;
}

Explanation

This CSS rule sets the font for all <h1> elements to "Helvetica". If the user's system does not have Helvetica, a generic sans-serif font will be used as a fallback.


Example 3: Using a Web Safe Font Stack

/* This CSS rule targets the entire body of the HTML document. */
body {
  /* This is a common font stack for maximum compatibility across browsers and operating systems. */
  font-family: "Times New Roman", Times, serif;
}

Explanation

This code defines a font stack for the <body> element. It first tries "Times New Roman", then "Times", and finally any available serif font.


Example 4: Incorporating a Custom Web Font

/* This rule applies to elements with the class "custom-text". */
.custom-text {
  /* It sets a custom font named "MyCustomFont". */
  /* A generic cursive font is provided as a fallback. */
  font-family: "MyCustomFont", cursive;
}

Explanation

This example sets a custom web font named "MyCustomFont" for elements with the class "custom-text". You would need to import this font using @font-face for it to work.


Example 5: Using a Monospaced Font for Code

/* This rule targets all <code> and <pre> elements. */
code, pre {
  /* This font stack is ideal for displaying code, as all characters have the same width. */
  font-family: "Courier New", Courier, monospace;
}

Explanation

This CSS rule ensures that all <code> and <pre> elements are displayed in a monospaced font, which is standard for presenting code.


Example 6: Prioritizing a Less Common Font

/* This rule applies to elements with the class "special-heading". */
.special-heading {
  /* It prioritizes the "Garamond" font. */
  /* A generic serif font is the fallback. */
  font-family: "Garamond", serif;
}

Explanation

This code sets the font for elements with the class "special-heading" to "Garamond", a classic serif font. If Garamond is unavailable, a default serif font will be used.


Example 7: A Modern Sans-Serif Font Stack

/* This rule targets the root HTML element. */
html {
  /* This modern font stack is popular for its clean and readable appearance. */
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

Explanation

This comprehensive font stack is designed to use the native system font of the user's operating system for a more integrated look and feel.