Text Formatting in HTML and CSS: A 5 Step Tutorial

When it comes to web development, formatting text is a fundamental skill. HTML and CSS are significant building blocks for developing beautifully formatted text on web pages. In this tutorial, we’ll take you through the process of formatting text in HTML and CSS, covering the essential tags and properties you need to know.

What is HTML and CSS, and why are they important for text formatting?

HTML (Hypertext Markup Language) is the mainstream markup language for creating web pages, whereas CSS (Cascading Style Sheets) defines the presentation and formatting of those web pages. HTML structures content, while CSS enhances its visual appearance, making it essential for text formatting on the web.

You can write HTML and CSS in any ordinary text editor, such as Notepad on Windows or TextEdit on macOS. There are also code editors designed for web development, like Visual Studio Code, Sublime Text, and Atom, which provide helpful features for coding.

text formatting

Also Read: Mastering HTML Language: A Roadmap To Building Incredible Websites

Tutorial For Formatting Text In HTML and CSS

1. Setting Up Your HTML Document

Before you start formatting text, you’ll need to set up your HTML document. Chalk out a new HTML file and add the following basic structure:

<!DOCTYPE html>

<html>

<head>

    <title>Text Formatting Tutorial</title>

    <link rel="stylesheet" type="text/css" href="styles.css">

</head>

<body>

    <!-- Your content goes here -->

</body>

</html>

The fundamental framework encompasses the HTML5 doctype declaration and establishes a connection to an external CSS file named “styles.css,” which is where we will specify our text formatting styles.

  • The <!DOCTYPE html> declaration is the document type declaration for HTML5. It informs the browser that this is an HTML5 doc.
  • <html>: It is signified as the root element of an HTML page.
  • <head>: It contains metadata about the document, like the page title and links to external resources like CSS stylesheets.
  • <title>: It sets the title of the web page, which pops in the browser’s title bar or tab.
  • <link>: It is used to link an external CSS file (styles.css) to your HTML document.
  • <body>: This is where the main body of your web page resides.

2. Using HTML Tags for Text Formatting

HTML offers six levels of headings in text formatting, from <h1> (the largest and most imperative) to <h6> (the smallest). Use them to structure your content and add hierarchy to your text.

<h1>This is a Heading 1</h1>

<h2>This is a Heading 2</h2>

<!-- ... -->

<h6>This is a Heading 6</h6>

<h1> to <h6>: These tags represent different levels of headings. Use them to structure your content hierarchically, with <h1> being the most important and <h6> the least important.

Paragraphs

Use the <p> tag to create paragraphs of text. Paragraphs are block-level elements, meaning they start on a new line and create a visual separation between content.

<p>This is a paragraph of text. You can format it using CSS.</p>

Text Styling

To add emphasis or style to text, you can use tags like <em> for emphasis and <strong> for strong importance.

<p>This text is <em>emphasized</em> and this text is <strong>strongly emphasized</strong>.</p>

<em> is used to emphasize text, typically rendering it in italics, while <strong> is used for strong emphasis, often displayed as bold text. These tags provide semantic meaning to the text.

Lists

HTML provides ordered and unordered lists.

<ul>

    <li>Item 1</li>

    <li>Item 2</li>

</ul>

<ol>

    <li>Item 1</li>

    <li>Item 2</li>

</ol>

<ul> is for creating unordered lists (bulleted lists), and <ol> is for ordered lists (numbered lists). Use <li> within these lists to define list items.

3. Using CSS for Text Formatting

To style your text further, you’ll need CSS. Create a file named styles.css and link it to your HTML file, as shown earlier.

Font Properties

Change font properties such as family, size, and color using CSS.

p {

    font-family: Arial, sans-serif;

    font-size: 16px;

    color: #333;

}

font-family: Specifies the font family to be used for the selected elements. It can include a list of font names to provide fallback options.

font-size: Sets the size of the text.

color: Defines the color of the text.

Text Alignment

You can align text within its container using the text-align property.

h1 {

    text-align: center;

}

text-align: Determines the horizontal alignment of text within its container. Common values include left, center, right, and justify.

Text Decoration

Change text decoration like underline or overline with text-decoration.

a {

    text-decoration: underline;

}

text-decoration: Controls decorations like underlines and overlines. It can be set to values like underline, overline, line-through, or none to remove any decoration.

Text Transform

Change the case of text using text-transform.

h2 {

    text-transform: uppercase;

}

text-transform: Alters the case of text. It can be set to values like uppercase, lowercase, or capitalized to change the appearance of the text.

4. Combining HTML and CSS

Now, apply the CSS styles to your HTML elements by selecting the appropriate HTML tags and applying your CSS properties.

CSS Selectors

CSS selectors (e.g., p, h1, .centered) are used to target specific HTML elements for styling. Classes (e.g., .centered, .emphasized, .link) are preceded by a period (.) in CSS to select elements with specific class attributes.

<h1 class="centered">Welcome to My Website</h1>

<p class="emphasized">This is some important information.</p>

<a href="#" class="link">Visit our Blog</a>

Applying Styles

CSS rules inside your styles.css file target specific HTML elements and apply styling properties to them. For example, .centered is used to center-align text, .emphasized makes text bold, and .link changes the color and adds an underline to links.

.centered {

    text-align: center;

}

.emphasized {

    font-weight: bold;

}

.link {

    color: blue;

    text-decoration: underline;

}

5. Experiment and Refine

Web development involves a lot of experimentation in text formatting. Try different combinations of fonts, colors, sizes, and styles to achieve the desired look and feel for your website. Consistency is crucial in web design. Ensure that your text formatting styles are applied consistently across your web pages for a polished appearance.

Conclusion 

By following these detailed steps and experimenting with various HTML and CSS properties, you can gain a strong foundation in text formatting for web development. Remember that web development is a continuous learning process, and refining your skills over time will lead to more impressive and professional-looking websites.

FAQs 

Can I use images as text in HTML and CSS?

Answer: Yes, you can use images as text in HTML by incorporating them as part of your content. However, it’s generally recommended to use actual text whenever possible for better accessibility and search engine optimization (SEO). You can style text to look like images using CSS properties like background-image and text-indent

What’s the difference between <em> and <strong> tags in HTML?

Answer: <em> is used to emphasize text, typically rendering it in italics, while <strong> is used for strong importance, often displayed as bold text. These tags provide semantic meaning to text, indicating different levels of emphasis.

How can I create a bulleted or numbered list in HTML?

Answer: You can create an unordered (bulleted) list with the <ul> tag and ordered (numbered) lists with the <ol> tag. Within these list containers, use <li> tags to define list items.

What is the purpose of using external CSS files?

Answer: External CSS files, linked with the <link> tag, separate the content (HTML) from the presentation (CSS). This separation improves maintainability and allows you to apply consistent styles across multiple web pages by modifying a single CSS file.

Are there other CSS properties I can use for text formatting?

Answer: Yes, there are many other CSS properties you can use for text formatting, including line-height, letter-spacing, text-shadow, and more. These properties provide fine-grained control over text appearance.

How can I ensure that my text formatting styles are consistent across all my web pages?

Answer: To maintain consistency, use external CSS files across all your web pages. Ensure that class names and IDs are consistent in your HTML and CSS, and consider creating a style guide or using a CSS preprocessor like SASS to manage your styles more efficiently.

Want faster WordPress?

WordPress Speed Optimization

Try our AWS powered WordPress hosting for free and see the difference for yourself.

No Credit Card Required.

Whitelabel Web Hosting Portal Demo

Launching WordPress on AWS takes just one minute with Nestify.

Launching WooCommerce on AWS takes just one minute with Nestify.