1. Home
css

CSS Tutorial: A Comprehensive Guide

Master CSSS with our in-depth tutorials. From beginner to advanced topics, explore essential concepts and upskill yourself.

  • 23
  • 4
right-top-arrow
6

CSS Text

Updated on 18/09/2024436 Views

As I sat at my desk one morning, I faced a big challenge. I had to make sure the wording on the new website I was creating for a nearby art museum looked perfect. It had to be elegant and simple to read at the same time. I began studying CSS text styling, which is the process of making text on web pages seem nice. Not only did I collect knowledge in choosing typefaces, structuring text, and applying technical methods, but also, with the advancement of my project each day, I could learn more about it.

Knowing how to work with advanced features, layout properties, and font styles enables developers to create visually appealing typography that improves user experience. In this tutorial, we'll examine the foundations of CSS text style.

Understanding Text Styling In CSS

In CSS, text within an element is contained within the element's content box. It typically begins at the top-left corner of the content area (or the top-right for right-to-left languages) and flows horizontally until the end of the line. If the content extends beyond the width of the box, it wraps to the next line. This process repeats until all content is accommodated.

Text styling in CSS is primarily achieved through two categories of properties:

  • Font styles: These properties govern aspects such as font family, size, weight, and style. They determine the visual appearance of the text in terms of its typeface and formatting.
  • Text layout styles: These properties control the spacing and layout features of the text. They include adjustments for line height, letter spacing, word spacing, and text alignment within the content box.

Font Styling Properties

Font styling properties in CSS text, such as font style, weight, and font family, enable precise control over text appearance.

Choosing font families

The font-family attribute is essential for determining how text appears on a webpage. You can designate the typeface or font family you want to use for your text. By providing a list of font family names, separated by commas, you can define fallback options in case the preferred font is not available on the user's device.

For example:

body {

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

}

Here, the browser will first attempt to render the text using the "Helvetica Neue" font. If that font is not available, it will fall back to Arial, and if Arial is also unavailable, it will resort to a generic sans-serif font, ensuring readability across different environments.

Adjusting font size

The font size property determines the size of the text. You can specify the size using varying units, including pixels (px), ems (em), or percentages (%). 

For instance:

h1 {

  font-size: 24px;

}

This rule sets the font size of all <h1> elements to 24 pixels, allowing for precise control over the visual hierarchy and readability of text content.

Controlling font-weight

The font-weight property in CSS text sets the thickness or boldness of the text. It accepts values like normal, bold, bolder, and lighter, as well as numeric values ranging from 100 to 900. 

For example:

h2 {

  font-weight: bold;

}

This declaration makes the text within all <h2> elements bold, enhancing emphasis and making headings stand out more prominently.

Setting font style

The font-style property specifies the font style, such as italic or normal.

For instance:

em {

  font-style: italic;

}

This rule makes all <em> elements italicized, adding emphasis to specific text segments and conveying a sense of emphasis or distinction.

Exploring font variants

The font-variant property allows you to control the usage of small caps for the text. It can be set to either normal or small-caps. 

For example:

blockquote {

  font-variant: small-caps;

}

This declaration renders the text within <blockquote> elements in small capitals, providing a stylistic variation that can enhance typographic aesthetics.

Color styling properties

By default, HTML elements appear in standard black and white, but adding color can greatly enhance visual appeal. CSS text color provides straightforward ways to alter both text and background colors, making webpages more engaging and readable.

Text color

To change the text color CSS, you can use the color property. Simply specify the desired color value. For instance:

h1 {

  color: blue;

}

This rule would render the text of <h1> elements in blue. Similarly, you can style other elements like paragraphs <p>, buttons <button>, and more.

Background color

Beyond text, you can also colorize backgrounds using the text background-color property. 

For example:

body {

  background-color: lightgrey;

}

This CSS rule would set the background of the entire webpage to light gray.

Using hex codes

While words like "red" or "blue" suffice for basic colors, for precise hues, hexadecimal color codes, or hex codes, are used to change the font color. Hex codes are six-character combinations of letters and numbers preceded by a hash symbol (#). 

For example:

section {

  background-color: #FF5733;

}

Here, #FF5733 represents a specific shade of orange. Hex codes offer precise control over color selection.

Text Layout Techniques:

Text layout in CSS involves various CSS text properties to control alignment, spacing, and other aspects of text presentation. Let's explore some essential techniques:

Aligning text

The text-align property determines how the text aligns within its container. Available values include:

  • left: Aligns text to the left.
  • right: Aligns text to the right.
  • center: Centers text horizontally.
  • justify: Distributes space evenly between words, creating a justified appearance.

Example:

h1 {

  text-align: center;

}

Managing line height

The line height property determines the height of each text line. It can take length units, percentages, or unitless values, which act as multipliers of the font size.

Example:

p {

  line-height: 1.6;

}

Modifying letter and word spacing

The letter-spacing and word-spacing properties adjust the spacing between letters and words, respectively. They accept most length and size units.

Example:

p::first-line {

  letter-spacing: 4px;

  word-spacing: 4px;

}

Direction

This property specifies the direction of text within an element's content area. It controls whether the text flows from left to right (ltr) or from right to left (rtl). In languages such as English, the default value is ltr, while languages such as Arabic or Hebrew typically use rtl. It's essential for correctly displaying languages with different writing directions.

.rtl-text {

    direction: rtl; /* Right-to-left direction */

}

Text transform

The text-transform property controls the capitalization or case of text. It allows you to transform text to uppercase or lowercase, capitalize the first letter of each word, or leave it unchanged. Useful for maintaining consistent typography and text style CSS across different sections of a website.

.uppercase-text {

    text-transform: uppercase; /* Converts text to uppercase */

}

Text emphasis

This property adds visual emphasis to text by applying marks or symbols above or below the characters. It's often used to highlight specific sections of text or to add decorative elements for aesthetic purposes. Values include none, filled, open, dot, circle, double-circle, and more.

.emphasized-text {

    text-emphasis: dot; /* Adds dots above the text */

}

Text indentation

The text-indent property sets the indentation for the first line of text within an element.

It's commonly used to create visually appealing text layouts, such as paragraphs with an indentation at the beginning. Negative values are also allowed to create hanging indents.

.indented-text {

    text-indent: 20px; /* Indents the first line by 20 pixels */

}

Letter spacing

This property of CSS text adjusts the spacing between characters in text. It can be used to increase or decrease the space between letters for better readability or aesthetic purposes. Useful for adjusting the overall appearance of text and improving legibility.

.spaced-text {

    letter-spacing: 2px; /* Increases spacing between characters by 2 pixels */

}

Word spacing

The word-spacing property adjusts the spacing between words in the text. It's similar to letter spacing but affects the spacing between entire words rather than individual characters. Useful for fine-tuning the appearance of text and improving readability.

.word-spaced-text {

    word-spacing: 5px; /* Increases spacing between words by 5 pixels */

}

Font Shorthand

In CSS text, the font shorthand property allows you to set multiple font-related properties in a single declaration. This shorthand is convenient for compactly specifying various font characteristics. Here's how it works:

The font shorthand property follows this order of values:

  1. Font style: Specifies the font style (e.g., italic, oblique).
  2. Font variant: Controls the font-variant (e.g., normal, small-caps).
  3. Font weight: Sets the font weight (e.g., normal, bold).
  4. Font stretch: Adjusts the width of the font (e.g., normal, condensed).
  5. Font size: Defines the font size.
  6. Line height: Sets the height of each text line.
  7. Font family: Specifies the font family or a list of font families.

Among these CSS text properties, only font size and font family are required when using the font shorthand.

Conclusion

Learning CSS text styling techniques enhances web content's aesthetics and readability. By selecting fonts, adjusting sizes, and managing weights and styles, developers create captivating typography. Adhering to best practices ensures design consistency, hierarchy clarity, and accessibility. Testing on various devices and optimizing performance is crucial. Using web-safe fonts improves compatibility. These methods elevate content quality, enhancing user pleasure and engagement. Effective communication and lasting impressions result from implementing excellent text formatting standards.

Frequently Asked Questions  

  1. What is CSS in text?

CSS (Cascading Style Sheets) in text refers to the use of CSS properties and rules to style and format text content on web pages. It allows developers to control various aspects of text appearance, such as font, size, color, spacing, alignment, and decoration.

  1. Can I write text in CSS?

CSS is not used to write text content. Instead, CSS is used to style and format existing text on web pages. Text content is typically written in HTML markup, while CSS is applied to enhance its presentation.

  1. How will you apply CSS to text?

To apply CSS to text, you use CSS properties and rules in a CSS stylesheet or within HTML <style> tags. You target specific text elements using selectors (e.g., class, id, element type) and then define the desired styling properties such as font family, font size, color, etc.

  1. How to print text in CSS?

To print text using CSS, you typically include the text content within HTML elements (e.g., <p>, <span>) and apply CSS styling to those elements as desired. When the webpage is printed, the text and its associated CSS styles will be rendered on the printed page.

  1. What is text size in CSS?

Text size in CSS refers to the dimension of text content displayed on a webpage. It is controlled using the font-size property, which specifies the size of the font used to render text.

image

Mukesh

Working with upGrad as a Senior Engineering Manager with more than 10+ years of experience in Software Development and Product Management.

Talk to Career Expert
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...