Bird
Raised Fist0
CSSmarkup~5 mins

Text alignment in CSS

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Text alignment helps you control how text lines up inside a box or container. It makes your content look neat and easy to read.
When you want text to start from the left side of a paragraph or heading.
When you want text to be centered in a button or title.
When you want text to line up on the right side, like dates or prices.
When you want to justify text so it fills the whole line evenly, like in newspapers.
When you want to change alignment for different screen sizes to improve readability.
Syntax
CSS
selector {
  text-align: value;
}
Replace selector with the HTML element or class you want to style.
The value can be left, right, center, or justify.
Examples
Aligns paragraph text to the left side.
CSS
p {
  text-align: left;
}
Centers the heading text.
CSS
h1 {
  text-align: center;
}
Aligns text with class 'price' to the right.
CSS
.price {
  text-align: right;
}
Justifies text in an article so lines fill the width evenly.
CSS
article {
  text-align: justify;
}
Sample Program
This example shows three paragraphs with different text alignments: left, right, and justify. Each has a background color to help you see the alignment clearly.
CSS
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Text Alignment Example</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 2rem;
    }
    header {
      text-align: center;
      margin-bottom: 1rem;
    }
    p.left {
      text-align: left;
      background-color: #e0f7fa;
      padding: 0.5rem;
    }
    p.right {
      text-align: right;
      background-color: #ffe0b2;
      padding: 0.5rem;
    }
    p.justify {
      text-align: justify;
      background-color: #c8e6c9;
      padding: 0.5rem;
    }
  </style>
</head>
<body>
  <header>
    <h1>Text Alignment Demo</h1>
  </header>
  <p class="left">This paragraph is aligned to the left. It looks like normal reading text starting from the left side.</p>
  <p class="right">This paragraph is aligned to the right. It lines up on the right side of the container.</p>
  <p class="justify">This paragraph is justified. The text stretches so both the left and right edges line up evenly, making it look tidy like in books or newspapers.</p>
</body>
</html>
OutputSuccess
Important Notes
Text alignment only affects inline content inside a block container.
Justify alignment can create uneven spaces between words on some lines.
For better accessibility, avoid center-aligning large blocks of text as it can be harder to read.
Summary
Use text-align to control horizontal text placement inside containers.
Common values are left, right, center, and justify.
Text alignment improves the look and readability of your web content.

Practice

(1/5)
1. What does the CSS property text-align: center; do to the text inside an element?
easy
A. Aligns the text to the left edge of the container
B. Centers the text horizontally within its container
C. Aligns the text to the right edge of the container
D. Justifies the text so both edges are aligned

Solution

  1. Step 1: Understand the text-align property

    This property controls horizontal alignment of inline content inside a block container.
  2. Step 2: Interpret the value center

    The value center places the text in the middle horizontally.
  3. Final Answer:

    Centers the text horizontally within its container -> Option B
  4. Quick Check:

    text-align: center = centered text [OK]
Hint: Center text with text-align: center; [OK]
Common Mistakes:
  • Confusing center with justify
  • Thinking it aligns text vertically
  • Mixing up left and right alignment
2. Which of the following is the correct CSS syntax to align text to the right inside a paragraph?
easy
A. p { text-align = right; }
B. p { align-text: right; }
C. p { text-align: right; }
D. p { text-align-right: true; }

Solution

  1. Step 1: Recall correct CSS property syntax

    CSS uses property: value; pairs inside curly braces for selectors.
  2. Step 2: Identify correct property and value

    text-align is the property, and right is the value. The syntax uses a colon and semicolon.
  3. Final Answer:

    p { text-align: right; } -> Option C
  4. Quick Check:

    Correct CSS syntax uses colon and semicolon [OK]
Hint: Use colon : not equals = for CSS properties [OK]
Common Mistakes:
  • Using = instead of :
  • Wrong property name like align-text
  • Missing semicolon at end
3. Given the CSS below, how will the text inside the <div> appear?
div {
  text-align: justify;
  width: 300px;
}
medium
A. Text lines will stretch to fill the width, aligning both left and right edges
B. Text will be centered horizontally inside the div
C. Text will align only to the left edge
D. Text will align only to the right edge

Solution

  1. Step 1: Understand text-align: justify;

    This value stretches each line so the text aligns evenly on both left and right edges.
  2. Step 2: Consider container width

    The fixed width of 300px means text lines will expand or contract to fill that width fully.
  3. Final Answer:

    Text lines will stretch to fill the width, aligning both left and right edges -> Option A
  4. Quick Check:

    justify aligns text edges both sides [OK]
Hint: Justify aligns text edges left and right [OK]
Common Mistakes:
  • Thinking justify centers text
  • Confusing justify with right alignment
  • Ignoring container width effect
4. Identify the error in this CSS snippet that aims to center text inside a header:
header {
  text-align center;
}
medium
A. Missing semicolon after property name
B. Incorrect property name, should be text-center
C. Value should be uppercase CENTER
D. Missing colon between property and value

Solution

  1. Step 1: Check CSS property syntax

    CSS requires a colon ':' between property and value.
  2. Step 2: Locate missing colon

    The snippet has text-align center; missing the colon after text-align.
  3. Final Answer:

    Missing colon between property and value -> Option D
  4. Quick Check:

    Property and value must be separated by colon [OK]
Hint: Always use colon : between property and value [OK]
Common Mistakes:
  • Omitting colon
  • Using wrong property name
  • Confusing semicolon placement
5. You want to align text inside a <section> so that the first line is centered, but all other lines are left aligned. Which CSS approach achieves this?
hard
A. Use text-align: left; on the section and text-align: center; on the first line with ::first-line pseudo-element
B. Use text-align: center; on the section and text-align: left; on a <p> inside
C. Use text-align: center; on the section and text-indent for the first line
D. Use text-align: center; on the section and text-align: left; on the ::first-line pseudo-element

Solution

  1. Step 1: Understand ::first-line pseudo-element

    This targets only the first line of text inside an element for styling.
  2. Step 2: Apply alignment logic

    Set the whole section text to left aligned, then override the first line to center using ::first-line.
  3. Step 3: Verify option correctness

    Use text-align: left; on the section and text-align: center; on the first line with ::first-line pseudo-element correctly applies text-align: center; to the first line and left alignment to the rest.
  4. Final Answer:

    Use text-align: left; on the section and text-align: center; on the first line with ::first-line pseudo-element -> Option A
  5. Quick Check:

    Use ::first-line to style first line separately [OK]
Hint: Use ::first-line to style first line differently [OK]
Common Mistakes:
  • Trying to set first line alignment on container only
  • Using text-indent instead of alignment
  • Applying conflicting styles on same element