CSS Font Properties: Family, Style, Size, and Shorthand

Posted on : 2023-10-30
CSS Font Properties: Family, Style, Size, and Shorthand

Share

CSS provide a powerful set of tools for controlling how text appears on web pages. In this article, we'll explore CSS font properties, including font family, font style, font size, and shorthand font declarations.

Font Family

Font family is a fundamental CSS property that defines the typeface for your text. It allows you to specify a list of fonts in order of preference. This ensures that if the user's browser doesn't have the first choice font, it will try the second, and so on until a suitable font is found. For example:

font-family: Arial, Helvetica, sans-serif;

In this example, the browser will first attempt to render the text in Arial. If Arial is not available, it will use Helvetica. If neither is available, the browser will fall back to a generic sans-serif font.

Font Style

CSS provides three primary font style properties: normal, italic, and oblique. These styles can be applied to text as follows:

font-style: normal; /* Default, no special style */
font-style: italic; /* Italicized text */
font-style: oblique; /* Text slants to the right */

By adjusting the font style, you can emphasize or de-emphasize text to match your design preferences.

Font Size

Font size is another crucial CSS property. It allows you to set the size of your text in various units, such as pixels (px), ems, percentages (%), or keywords like 'small,' 'medium,' or 'large.' For example:

font-size: 16px; /* Sets the font size to 16 pixels */
font-size: 1.2em; /* Increases the font size by 20% */
font-size: large; /* Uses a predefined size (browser-dependent) */

Selecting the right font size is essential for readability and the overall aesthetic of your website.

Shorthand Font

While CSS properties like font family, style, and size can be set individually, you can also use the shorthand font property to combine them into a single declaration. The shorthand font property allows you to set font properties in a specific order: style, variant, weight, size, line height, and font family.

Here's an example of how the shorthand font property works:

font: italic small-caps bold 18px/1.5 Arial, sans-serif;

In this single line of code, the font style is set to italic, the variant to small-caps, the weight to bold, the size to 18 pixels, the line height to 1.5, and the font family to Arial or any available sans-serif font if Arial is not present.

Using the shorthand font property can streamline your CSS code and make it more concise.