Simple Methods of Using Font Styles in CSS

Simple Methods of Using Font Styles in CSS: To begin, first refer – know-what-font-styles-are-in-css

Working with Font Families

In web design, font families define the kind of font that should be used for text in an HTML document. These font families are specified using CSS (Cascading Style Sheets).

Types of Font Families

Generic Font Families: Browsers will use the predefined default fonts from these categories if a particular font is unavailable.

  • serif: Fonts with decorative strokes (e.g., Times New Roman).
  • sans-serif: Fonts without decorative strokes (e.g., Arial, Helvetica).
  • monospace: Fonts with equal-width characters (e.g., Courier New).
  • cursive: Fonts that resemble handwriting (e.g., Comic Sans).
  • fantasy: Decorative fonts with unique styles (e.g., Papyrus).

Specific Font Families: These are groups of font names installed on the user’s computer or made available online (e.g., Roboto, Open Sans).

Syntax

The CSS font-family property specifies the font stack. A font stack includes:

  • Specific font names (e.g., “Arial”).
  • Generic font families as a fallback.

Example

Explanation
Body Rule: Sets the default font for the document.
Class Rules: Each class (serif, monospace, etc.) specifies a different font family.

Figure 1 – Displaying the output of using font families

Output
When viewed in a browser:

  • Arial or a sans-serif font will be used to render the h1 and first p elements.
  • The specified fonts will be used to render the following paragraphs.
  • When a user’s computer cannot support a particular font (like “Times New Roman”), the browser falls back to a generic font (like serif).
Using Absolute Values

Absolute values are used in web development to make precise specifications about the lengths, widths, and placements of items. These values establish precise, definite amounts that are independent of any parent element or viewport.

Common Absolute Units in CSS

  • Pixels (px):
    • The most commonly used unit.
    • Defines a fixed size in screen pixels.
  • Points (pt):
    • Common in print-based designs.
    • One point is approximately 1/72 of an inch.
  • Centimeters (cm):
    • Defines size in centimeters.
    • Used primarily for print.
  • Millimeters (mm):
    • Defines size in millimeters.
  • Inches (in):
    • Defines size in inches.

Syntax

  • property: The CSS property being modified (e.g., width, height).
  • value: A numeric value.
  • unit: An absolute unit (e.g., px, cm).

Example

Explanation

  • Pixel Box:
    • .box-px uses 150px width and 100px height.
    • Fixed size regardless of screen resolution.
  • Centimeter Box:
    • .box-cm is sized in centimeters.
    • Useful for print designs where exact dimensions are crucial.
  • Inch Box:
    • .box-in is sized in inches.
    • Also commonly used in print or specific design contexts.

Figure 2 – Displaying the output of using absolute values

Output
When rendered in a browser:

  • A light blue box of size 150px x 100px appears.
  • Below it, a light green box with dimensions 5cm x 3cm appears.
  • A 2in x 1in light coral box is shown at the bottom.

The absolute size of each box is displayed, guaranteeing uniform measurements on various devices.

Using Relative Values

Relative values are used in web development to specify sizes, locations, or other attributes in relation to another element or context, like the viewport, the parent element, or the root font size. Relative units are perfect for responsive design since they are adaptable to changes in parent element characteristics or screen size.

Common Relative Units in CSS

  • Em (em):
    • Relative to the font size of the parent element.
    • Example: If the parent font size is 16px, 1em equals 16px.
  • Rem (rem):
    • Relative to the font size of the root element (html).
    • Example: If the root font size is 16px, 1rem equals 16px.
  • Percentage (%):
    • Relative to the size of the parent element (e.g., width, height).
  • View Width (vw):
    • 1vw = 1% of the viewport’s width.
  • View Height (vh):
    • 1vh = 1% of the viewport’s height.
  • Viewport Min (vmin):
    • 1vmin = 1% of the smaller dimension (width or height) of the viewport.
  • Viewport Max (vmax):
    • 1vmax = 1% of the larger dimension (width or height) of the viewport.

Syntax

  • property: The CSS property being modified (e.g., width, height).
  • value: A numeric value.
  • unit: A relative unit (e.g., em, %).

Example

Explanation

  • Em Box:
    • .box-em is 10em wide and 5em tall.
    • Its dimensions are relative to the parent’s font size.
  • Rem Box:
    • .box-rem uses rem units, making its size relative to the root font size.
    • Rem units ensure consistency across nested elements.
  • Percentage Box:
    • .box-percent has a width of 50%, relative to its parent’s width.
  • Viewport Units Box:
    • .box-vw spans 50vw (50% of viewport width) by 20vh (20% of viewport height).
    • Ideal for fluid, viewport-responsive designs.

Figure 3 – Displaying the output of using relative values

Output
When viewed in a browser:

  • A light blue box is displayed sized relative to the parent’s font size (10em x 5em).
  • Below it is a light green box with the same dimensions but relative to the root font size (10rem x 5rem).
  • Half of the width of the parent container is occupied by a light coral box.
  • A light yellow box at the bottom dynamically changes to 20% of the viewport’s height and 50% of its width.

This illustrates how relative units are adaptable and responsive.

Using Percentage Values

Percentage values for size, spacing, and placement are frequently used in CSS. When building responsive layouts, these numbers are helpful because they are relative to another reference size. Depending on the CSS property being defined, the reference changes.

Key Features of Percentage Values

  • Width and Height:
    • Regarding width, the percentage is based on the parent element’s (the containing block’s) width.
    • For height: If the height of the parent block is specified explicitly, the percentage is in relation to the height of the containing block.
  • Margin and Padding:
    • The percentage values are computed in relation to the containing block’s width.
  • Positioning:
    • The percentage values for the top, left, right, and bottom are based on the containing block’s dimensions.

Syntax

  • property: The CSS property being modified (e.g., width, height).
  • percentage: A numeric value followed by %.

Example

Explanation

  • Container:
    • .container, centered with margin: 0 auto, occupies 80% of the viewport’s width.
    • Its padding guarantees that the content is spaced out within the container.
  • Box:
    • .box has a fixed height of 150 pixels and a width of 50% in relation to its parent container.
    • margin: 10% auto centers horizontally and establishes a vertical margin of 10% in relation to the width of the container.
  • Nested Box:
    • 50% of the parent.box dimensions make up the .nested-box width and height.
    • shows percentage values that are nested inside of another element.

Figure 4 – Displaying the output of using percentage values

Output
When rendered in a browser:

  • The container, which has some padding and a black border, takes up 80% of the viewport’s width.
  • A light blue box inside the container occupies half of its width, has a fixed height of 150 pixels, and is spaced vertically 10% of the width of the container.
  • Percentage-based nesting is demonstrated by the light coral nested box inside the blue box, which occupies 50% of its parent’s dimensions.

This shows how percentage values can create flexible, responsive layouts that adapt to different screen sizes and parent dimensions.

Working with the font-size-adjust Property

Adjusting the font size Using the CSS attribute, the font size is changed according to the font’s x-height, maintaining the text’s readability. Text readability is greatly influenced by the x-height, which is the height of lowercase characters like “x.”

Why Use font-size-adjust?

Variations in the x-heights of different typefaces with the same font-size might make them appear larger or smaller. You can improve text clarity when using a fallback font by using font-size-adjust to make sure the x-height is the same across fonts.

Syntax

  • : A ratio of x-height to font size. Typically between 0 and 1.
  • none: Default value. No adjustment is made.

How It Works
The browser uses the given ratio to determine the font size for the fallback font if the primary font is not available.
The ratio guarantees that the x-height of the backup font is equal to that of the original font.

Example

Explanation

  • Default Behavior:
    • The fallback font’s smaller x-height can make the text without font-size-adjust (.no-adjust) look too small.
  • Adjusted Font:
    • To maintain readability, the text with font-size-adjust: 0.5 makes sure that the x-height of the fallback font is in line with the desired proportions.

Figure 5 – Displaying the output of using the font-size-adjust Property

Output
When rendered in a browser:

  • The first paragraph displays normally and is in Arial.
  • The second paragraph uses “Times New Roman” without font-size-adjust. The x-height of the backup font may change if “Times New Roman” is not available, which could cause the text to appear too small or large.
  • The third paragraph uses “Times New Roman” with font-size-adjust: 0.5. The size of the backup font is changed to preserve the same x-height ratio in the event that “Times New Roman” is not available, enhancing text consistency.

This demonstrates how font-size-adjust helps maintain consistent text readability across different fonts.

Working with the font-style property

The font-style CSS property determines if a font should be shown in an oblique, italic, or normal manner. In quotes, headings, and other stylistic contexts, it is frequently employed to highlight text.

Values of font-style

  • normal:
    • Default value.
    • The text is displayed in its standard, upright font style.
  • italic:
    • The text is displayed in an italicized style.
    • Uses a specific italic version of the font, if available.
  • oblique:
    • The text is displayed in a slanted style.
    • The font is artificially slanted if an oblique variant is unavailable.
  • initial:
    • Resets the property to its default value (normal).
  • inherit:
    • Inherits the font style from the parent element.

Syntax:

Example

Explanation

  • Normal Style:
    • The .normal class ensures the text is displayed in the standard, upright style.
    • This is the default style for most fonts.
  • Italic Style:
    • The .italic class displays text in an italic style.
    • Italics use a specifically designed font variant, which improves readability and aesthetics.
  • Oblique Style:
    • The .oblique class slants the text at an angle.
    • If the font lacks an italic version, the browser may simulate the effect by slanting the normal font.

Figure 6 – Displaying the output of using the font-style Property

Output
When rendered in a browser:

  • The first paragraph is presented straight and in the standard font style.
  • Italicizing the second paragraph gives it a more formal or focused appearance.
  • Similar to italics but with a somewhat distinct appearance, the third paragraph displays text at an oblique angle.

This illustration shows how to apply typographic emphasis and diversity in design using font-style.

Working with Web Fonts

Custom fonts can be used on your website by embedding them directly or by connecting to an online font file. In addition to conventional system typefaces, this allows you to build visually beautiful and distinctive typography.

Methods to Use Web Fonts

  • Using @font-face Rule:
    • Embed custom fonts by specifying their source files.
  • Using Google Fonts or Other Font Services:
    • Link to fonts hosted by providers like Google Fonts.

Example 1: Using @font-face
The @font-face rule lets you define custom fonts by linking to font files.

Example 2: Using Google Fonts
Google Fonts is an easy way to include web fonts by adding a <link> in your HTML.

Explanation

  • Making use of @font-face:
    • The font family (OpenSans) and its source (a URL to the.ttf font file) are specified by the @font-face rule.
    • ‘OpenSans’ is the font family to which the custom font is applied.
  • Making Use of Google Fonts:
    • A URL to add fonts to your HTML can be found on Google Fonts.
    • The selected Google Font (Roboto) is applied to components via the font-family property.

Figure 7 – Displaying the output of using Web fonts (@font-face)

Figure 8 – Displaying the output of using Web fonts (Google fonts)

Output
When viewed in a browser:

Example 1:

The second paragraph uses the “OpenSans” font loaded via @font-face.
Example 2:

  • The first paragraph uses the default browser font.
  • The second paragraph uses the “Roboto” font in normal weight.
  • The third paragraph uses the “Roboto” font in bold weight.

These examples demonstrate how web fonts enhance typography and allow for consistent, professional designs across devices.

Scroll to Top