How to Center Text in HTML: CSS Examples and Fixes

by .

To center text in HTML, apply the CSS declaration text-align: center; to the block containing the text. For example, <p style="text-align: center;">Hello</p> centers that paragraph’s text horizontally. Use CSS for new work; the old center element is obsolete.

The common source of confusion is what you want to move. Centering the words inside a box, centering the box on the page and placing a group of content in the middle of a tall panel are different tasks. Pick the method that matches the object you are trying to position.

Three examples distinguishing centered text, a centered block and content centered on both axes
Choose the rule for the thing you want to center: text, a box or a content group.

How to center text in HTML with text-align

For one short experiment, put a style attribute directly on the paragraph. This makes it easy to see the connection between the element and the declaration. The property name and value are CSS, even though they are written inside an HTML attribute.

<p style="text-align: center;">Welcome to the workshop.</p>

The paragraph’s box still takes its normal place in the layout. Its text lines are centered within that box. If you add a border, the border does not shrink around the words or jump to the middle of the viewport. This is the distinction to check before trying more complicated layout rules.

In a maintained website, use a reusable class in a stylesheet instead of repeating the same inline style on many elements. If your stylesheet is not connected yet, our guide to linking CSS to HTML explains the document-head setup and common file-path problems.

Choose the centering method you actually need

Match the method to the task
Goal Use What moves
Center text lines in a paragraph text-align: center Inline content within the text container
Center a narrower block A width or max-width and auto inline margins The block within available horizontal space
Center one content group both ways Grid with place-items: center The grid item within its available area
Arrange and center a flexible group Flexbox alignment properties Flex items along the configured axes

You can combine methods. A card may use auto margins to sit in the middle of the page while its heading uses text-align to center the words inside it. There is no conflict: each declaration controls a different part of the result.

Start with the simplest rule. A heading that only needs centered text does not need a flex container. Adding an unnecessary layout mode can alter the behaviour of its children and make the next change harder to understand.

Center a heading or paragraph with a class

Put this CSS in your stylesheet and add the class to whichever heading or paragraph needs it. A class name is a label you choose. It has no built-in behaviour until a matching CSS rule exists. For a real example, NC State University Libraries lists a text-center class in its web style guide.

<!-- HTML -->
<h2 class="centered-text">Workshop details</h2>

/* CSS */
.centered-text {
  text-align: center;
}

Text alignment is inherited, so applying it to a section can also affect paragraphs and headings inside that section. This is useful for a short announcement. It is less useful when you accidentally center an entire article, including long paragraphs that are easier to read with a consistent starting edge.

To keep a centered heading above ordinary body text, apply the class to the heading alone. Alternatively, reset the paragraph’s alignment with text-align: start. The start value follows the document’s text direction, which is useful when a design must support more than one writing direction.

A normal inline span does not provide a full-width block in which to center its own text. Apply the alignment to its containing paragraph, or deliberately change the span’s layout role when that suits the design. Randomly adding a width to an inline element often leaves the underlying issue untouched.

Center the block separately

If the paragraph has a maximum width and should sit centrally, give the block auto inline margins. The University of Northern Iowa’s layout notes show the same space-sharing principle with margin: 0 auto. Leave its text aligned to the start if it is a longer passage. Readers can then scan from a stable edge while the content column remains balanced on the page.

.reading-column {
  max-width: 65ch;
  margin-inline: auto;
  text-align: start;
}

The max-width creates spare space on wider screens. Auto margins share that space. On a narrow screen the block can use the available width, so there may be no visible margin to distribute. That is expected rather than a failure of the centering rule.

A centered text line inside a wide block compared with a narrow block centered using margins
Text alignment and block positioning can be adjusted independently.

Center text vertically and horizontally

Text-align does not vertically center a paragraph in a tall panel. For one group of content, Grid offers a compact solution. Give the container some minimum height, use place-items to position its child, and use text-align inside the group if the lines should also be centered.

/* CSS */
.panel {
  min-height: 16rem;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}
.panel-content { text-align: center; }

<!-- HTML -->
<section class="panel">
  <div class="panel-content">
    <h2>Workshop details</h2>
    <p>Bring a notebook and a pencil.</p>
  </div>
</section>

The extra wrapper matters when several pieces belong together. Without it, a heading and paragraph would be separate grid items rather than one content group. A minimum height also leaves the panel free to grow when the text wraps or the visitor increases its size.

Flexbox is another option. Wellesley College’s Flexbox lesson demonstrates centering on the main and cross axes. With the default row direction, justify-content controls the horizontal main axis, while align-items controls the vertical cross axis. With a column direction, those axes change. Read the direction before copying an alignment rule from another component.

.flex-panel {
  min-height: 16rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 1.5rem;
}

Avoid fixing the line-height to match a box’s height as a general centering technique. It may appear to work for one line, then produce awkward spacing or clipping when the text wraps. The problem becomes obvious on a phone or after text enlargement.

A complete example you can save and test

Save this document as centered-text.html and open it in a browser. It combines a centered page panel, a centered content group and a readable text width. Resize the window and replace the short paragraph with a longer sentence to see whether the layout still makes sense.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Centered text example</title>
  <style>
    * { box-sizing: border-box; }
    body { margin: 0; padding: 1rem; font-family: system-ui, sans-serif; }
    .panel {
      width: 100%; max-width: 52rem; margin-inline: auto;
      min-height: 20rem; padding: 2rem;
      display: grid; place-items: center;
      background: #edf5ff; border: 1px solid #9dbde0;
    }
    .panel-content { text-align: center; }
    .panel-content p { max-width: 45ch; margin-inline: auto; line-height: 1.6; }
  </style>
</head>
<body>
  <main class="panel">
    <div class="panel-content">
      <h1>Weekend workshop</h1>
      <p>Bring a notebook and a pencil. The session includes time to practise and ask questions.</p>
    </div>
  </main>
</body>
</html>

The body uses padding so the panel does not touch the viewport edge. The panel has a flexible width with an upper limit. Its minimum height creates room for vertical centering, while the inner wrapper keeps the heading and paragraph together. The paragraph can wrap without requiring a manual line break.

Try removing text-align from the inner wrapper. The group remains positioned centrally, but its text no longer has the same alignment. Put the declaration back, then remove place-items. The difference helps separate text formatting from item positioning.

This example does not need a framework. For related image behaviour, see our guide to centering an image in HTML. Images can participate in different layout modes, so check their display behaviour before assuming a text rule will move the image box.

The same centered workshop panel at a wide and narrow viewport with naturally wrapped text
Flexible dimensions let the same content wrap on a narrow screen.

Bootstrap, Tailwind and Bulma equivalents

If a framework is already installed, use its existing utility classes consistently. Do not add a whole framework merely to center one sentence. A single CSS declaration can do that job.

  • Bootstrap’s text utilities provide text-center for centered text, alongside responsive alignment classes.
  • Tailwind’s text-align utilities also use text-center. Breakpoint variants can change alignment at a chosen screen width.
  • Bulma’s typography helpers use has-text-centered and offer responsive variants.

These class names are not part of HTML itself. They work when the appropriate framework CSS is present. Copying a class from a tutorial into a plain document without the corresponding stylesheet does not install the framework or create its rules.

For a responsive design, test the actual breakpoint behaviour in your project. Framework configuration can alter defaults. A class that centers text on one screen size may intentionally leave another size aligned to the start.

Why is the text still not centered?

Inspect the element in the browser’s developer tools and check its computed text-align value. If it is not center, look for a missing stylesheet, a class-name mismatch or a rule that wins in the cascade. If it is center, inspect the size and location of the containing box.

Common centering problems
Symptom Likely explanation Next check
Nothing changes The rule is missing or overridden Inspect the computed style and stylesheet request
Text is centered in the wrong place The containing box is narrower or offset Outline the container and inspect its dimensions
Text stays at the top Only horizontal text alignment was applied Use an appropriate Grid or Flexbox layout
The desktop layout fails on mobile A fixed size or breakpoint rule changes the result Test wrapping and smaller widths

A temporary outline on the container is often revealing. It shows where the browser thinks the box begins and ends. The words may be correctly centered inside a box that is itself positioned somewhere unexpected.

Check for long unbroken strings too. A long address or identifier may overflow even when ordinary sentences wrap correctly. Solve that content-wrapping problem rather than adding more alignment declarations. Also confirm that inherited alignment has not been reset by a more specific child rule.

Questions and self-check

Can I use the center tag?

Old browsers and current browsers may still render legacy center markup, but the element is obsolete. For new pages, use CSS so the document’s meaning and its presentation remain easier to maintain.

Does text-align center a div?

It centers the div’s inline content, including its text lines. It does not, by itself, center the div’s own box within its parent. Use an appropriate sizing and layout method for that separate job.

Should all paragraphs be centered?

Centered text can suit short headings and brief notices. For sustained reading, a stable starting edge is often easier to follow. Choose alignment for the content rather than applying it to every element because it looks symmetrical.

Self-check: a paragraph is centered horizontally but remains at the top of a tall card. Why?

Text-align controls inline text alignment, not vertical positioning. The card needs a suitable layout rule, such as Grid with place-items: center, and enough available height for that positioning to be visible.

Self-check: why does a text-center class do nothing in a plain HTML file?

A class name alone has no styling. Define a matching CSS rule or load the framework stylesheet that supplies that utility.

Comments are closed.