CSS Font Size

The CSS font-size property controls the size of the text within an element. It plays a significant role in readability and the overall aesthetic of a webpage. Properly chosen font sizes ensure that the content is easy to read across various devices and screen sizes.

Key Points on CSS Font Size:

Syntax for CSS Font Size:

Syntax Example

/* Font size syntax */
        .element {
            font-size: value;
                 }
        
        /* Example */
        p {
            font-size: 16px;
          }

Examples of CSS Font Size:

The following examples demonstrate how to use font-size in various units:

Code Example: Font Size in Pixels


            p {
            font-size: 18px;
        }

Output

This paragraph has a font size of 18px.

Code Example: Font Size in Em


             p {
             font-size: 2em;
               }

Output

This paragraph has a font size of 2em, which is relative to its parent element’s font size.

Code Example: Font Size in Rem


             p {
             font-size: 1.5rem;
               }

Output

This paragraph has a font size of 1.5rem, which is relative to the root element’s font size.

Code Example: Font Size in Percentage


             p {
             font-size: 120%;
               }

Output

This paragraph has a font size of 120%, which is relative to the parent element’s font size.

Code Example: Font Size in Viewport Width (vw)


             p {
             font-size: 5vw;
               }

Output

This paragraph has a font size of 5vw, which is relative to the viewport width.

Common Font Size Properties:

Selecting the right font size is crucial for readability. Ensure that your font sizes are scalable for responsive design, and provide an accessible experience by using relative units like em or rem instead of fixed units like px.