#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS3 Fonts

CSS font properties control which typeface your text uses and how it looks: its size, weight, style and line spacing. Getting fonts right affects readability more than almost any other styling choice, so these are some of the first properties you set on a new site, usually on the body element so every child inherits them.

Choosing a typeface with font-family

The font-family property takes a list of fonts separated by commas. The browser uses the first one that is available and falls back down the list. Always finish with a generic family such as sans-serif, serif or monospace, so there is a sensible default when nothing else loads.

Font names that contain spaces should be wrapped in quotes, for example "Open Sans". A popular modern choice is the system font stack, which uses the operating system's own interface font and needs no download at all.

Size, weight, style and line height

  • font-size sets the size. Prefer rem units, which are relative to the root font size and respect the user's browser setting. A fixed px body size can override that preference.
  • font-weight accepts keywords like normal and bold or numbers from 100 to 900. A number only works if that weight exists in the font you loaded.
  • font-style is usually normal or italic.
  • line-height controls space between lines. A unitless value such as 1.5 is best, because it scales with each element's own font size.

The font shorthand combines these, but it requires at least a size and a family, and it resets any part you leave out. Many developers set the properties individually to avoid surprises.

Loading your own fonts with @font-face

To use a font that is not installed on the visitor's device, you declare it with @font-face and point to the font files. WOFF2 is the format to use; it is compressed and supported by all current browsers.

@font-face {
  font-family: "Inter";
  src: url("/fonts/inter-var.woff2") format("woff2");
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

body {
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 1rem;
  line-height: 1.6;
}

h1, h2 {
  font-weight: 700;
  line-height: 1.2;
}

code {
  font-family: ui-monospace, Consolas, monospace;
}

The range 100 900 tells the browser that this one file is a variable font covering every weight in that range, instead of needing a separate file per weight.

The flash of invisible text and how to avoid it

While a web font downloads, some browsers hide the text for a moment. On a slow connection, the page looks empty even though the content has arrived. The font-display: swap descriptor fixes this by showing a fallback font immediately and swapping in your font once it is ready.

The swap can cause the text to shift slightly if the two fonts have different widths. Choosing a fallback with similar proportions, and loading only the weights you actually use, keeps that shift small.

Fake bold and italic

If you set font-weight: 700 but only loaded the regular file, the browser draws a "synthetic" bold by thickening the letters. It often looks smudged. The same happens with italic. Load the real bold and italic files, or a variable font, when you need them.

Good defaults to remember

  • Set the font family, size and line height once on body.
  • Use rem for sizes and unitless line-height.
  • Serve WOFF2 with font-display: swap and end every stack with a generic family.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course