Text and Box Styles

Explore text and box styling properties and learn how to apply them.

🎯 Introduction

Styling text and boxes is an essential part of frontend development. Whether you are designing a layout or improving readability, these styles help you build more engaging interfaces. This article explores text and box styling techniques and their practical applications, so you can use them confidently in your projects.

✏️ Styling Text

Text styling includes font family, size, color, line height, and letter spacing. These properties improve both appearance and readability. Common text styling properties include:

  • font-family: Sets the font family.
  • font-size: Controls the text size.
  • color: Defines the text color.
  • line-height: Sets the line height, affecting the spacing between lines.
  • letter-spacing: Adjusts the spacing between letters.
.example {
    font-family: Arial, sans-serif;
    font-size: 16px;
    color: #333;
    line-height: 1.5;
    letter-spacing: 0.5px;
}

📏 Common Font Sizes

SizePixels (px)Description
Small12pxOften used for secondary text
Normal16pxDefault text size
Medium20pxHeadings or emphasized text
Large24pxMain headings
Extra large32pxProminent headings or taglines

📦 Handling Text Overflow

When text exceeds the bounds of its box, you can handle the overflow with these properties:

  • overflow: Set it to hidden, scroll, or auto.
  • text-overflow: Often used with overflow; set it to ellipsis to display an ellipsis.
  • white-space: Controls whether text wraps.
.example {
    width: 200px;
    height: 50px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

🔢 Limiting the Number of Lines

Sometimes you need to limit how many lines of text are visible. For example, use -webkit-line-clamp to show only the first two lines, as in this example:

.example {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2; /* Limit text to two lines */
    overflow: hidden;
}

🎨 Background Basics

Backgrounds are an important part of a design. Use properties such as background-color, background-image, and background-size to style them.

.example {
    background-color: #f2f2f2;
    background-image: url('image.jpg');
    background-size: cover;
}

🧊 Transparent Backgrounds: rgba vs. opacity

There are two main options for adding transparency: rgba and opacity.

  • rgba: Sets the transparency of a color; here, it affects the background color.
  • opacity: Sets the transparency of the entire element, including its text and background.
.example {
    background-color: rgba(255, 255, 255, 0.5); /* Semi-transparent background */
}

.example-opacity {
    opacity: 0.5; /* Make the entire element semi-transparent */
}

✂️ Box Spacing: margin and padding

  • margin: Controls the space outside a box, affecting its distance from other elements.
  • padding: Controls the space inside a box, between its content and its edge.
.example {
    margin: 20px; /* Outer spacing */
    padding: 10px; /* Inner spacing */
}

📐 Margin Property Reference

SpacingProperty or shorthand
Topmargin-top
Rightmargin-right
Bottommargin-bottom
Leftmargin-left
All sidesmargin: 10px;

❓ Knowledge Check

Q1: How do you set the line height of text?

Use line-height. A value of 1.5 times the font size often improves readability.

Q2: How do you control the space outside and inside a box?

Use margin for outer spacing and padding for inner spacing.

Q3: What is the main difference between rgba and opacity?

When used for a background color, rgba affects only that color's transparency, while opacity affects the entire element, including its text and background.

🧪 Exercises

Exercise 1: Style Text

Define a text style that specifies a font family, size, and color.

.text-style {
    font-family: 'Helvetica', sans-serif;
    font-size: 18px;
    color: #4a4a4a;
}

Copy the code into your editor and preview the result!

Exercise 2: Set a Background and Adjust Transparency

Create a box with a background and experiment with its transparency.

.box-style {
    background-color: rgba(0, 0, 255, 0.3);
    width: 150px;
    height: 150px;
}

Copy the code into your editor and preview the result!


🎉 You have learned to style text and boxes and create transparent backgrounds!