- How to Vertically Center Text with CSS
- Use the CSS vertical-align property
- Example of vertically aligning a text:
- How to Center Text in HTML
- Using CSS
- Using the Center Tag in HTML
- Community Q&A
- How to center text in HTML
- Using the tags
- Using a style sheet property
- Centering a few blocks of text
- Multiple blocks of text
- Examples of centered text and objects
- Centering text in a paragraph
- Centering an image
- Centering a URL hyperlink
- Centering text in a header tag Example of centered text in a header tag Example of centered text in a header tag Centering text in a tag Example of a centered sentence in a div tag. Example of a second centered sentence in a div tag.
How to center text with html
How to Vertically Center Text with CSS
Centering elements vertically with CSS often gives trouble. However, there are several ways of vertical centering, and each is easy to use.
Use the CSS vertical-align property
The vertical-align property is used to vertically center inline elements.
The values of the vertical-align property align the element relative to its parent element:
- Line-relative values vertically align an element relative to the entire line.
- Values for table cells are relative to the table-height-algorithm, which commonly refers to the height of the row.
It is important to note that it is possible to nudge text vertically up or down in CSS using the vertical-align property. This property sets the vertical alignment of an inline or table-cell element, and can be used to adjust the vertical position of text within its container.
Note that vertical-align only applies to inline or table-cell elements, so it may not work as expected on block-level elements such as or
. In such cases, you may need to wrap the text in an inline or table-cell element to apply vertical-align .
Example of vertically aligning a text:
html> html> head> title>Title of the document title> style> div < display: table-cell; width: 250px; height: 200px; padding: 10px; border: 3px dashed #1c87c9; vertical-align: middle; > style> head> body> div>Vertically aligned text div> body> html>
How to Center Text in HTML
This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions.
This article has been viewed 233,164 times.
This wikiHow teaches you how to center text in an HTML website using Cascading Style Sheets (CSS). Centering in HTML used to be done with the tag, but that tag is now considered obsolete and no longer works in most browsers. [1] X Research source
Using CSS
DOCTYPE html> html> body> style> div.a text-align: center; > style>
div class="a"> h1>Welcome to My Websiteh1> p>This website is primarily for the purpose of providing information about things.p> div>
style> div.a text-align: center; > style> div class="a"> h2>Donations Welcomeh2> p>pleasep> div>
DOCTYPE html> html> body> style> div.a text-align: center; > style> div class="a"> h1>Welcome to My Websiteh1> p>This website is primarily for the purpose of providing information about things.p> div> div class="a"> h2>Donations Welcomeh2> p>pleasep> div> body> html>
Using the Center Tag in HTML
Open your HTML document. This method describes how to use the HTML tag, which is now obsolete. As of December 2018, this tag may still work in some web browsers, however, it’s not advised to rely on this tag in the long run.
Find the text you want to center. Scroll down until you find the header, paragraph, or other text that you want to center.
center>h1>Welcome to My Websiteh1>center> center>Make yourself at home!center>
html> body> h1>center>Welcome to My Websitecenter>h1> center>Make yourself at home!center> p1>The purpose of this website is to display information about things.p1> body> html>
Community Q&A
You can simply add the space in the code. If you want the result to turn out with exactly the same spacing and line breaks as in the code itself, wrap the element in a tag.
Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow
To center an image in HTML, you can use the «align» attribute. But in HTML 5, you can’t use the align attribute; you’ve got to use CSS class instead.
Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow
How do I center-align a video I have inserted from my media library? I’ve made several stabs at it but none successful.
Set your video into a perimeter. An Example of this would be: Replacing the YouTube URL with the URL where your media/video is stored. You can move your video to any where you want by changing the position of the top or left perimeters of the first line of the code.
Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow
How to center text in HTML
Centering text is generally used for a title of a website or document. To center text using HTML (hypertext markup language), you can use the tag or use a CSS (cascading style sheets) property. To proceed, select the option you prefer and follow the instructions.
Using the tags
One way to center text or put it in the middle of the page is to enclose it in tags.
Using the example HTML code above yields the following result:
The tag is now considered deprecated. Although it may still work, it’s expected to be removed in favor of using CSS. We recommend you use the style sheet method (shown below) to center text in HTML.
Using a style sheet property
You can center text on a website with CSS by specifying the text-align property of the element to be centered.
Centering a few blocks of text
If you only have one or a few blocks of text to center, add the style attribute to the element’s opening tag and use the «text-align» property. In the example below, we added the attribute and property to the tag.
The «text-align» property is set to «center» to indicate the element is centered in the middle of the page or containing div.
Multiple blocks of text
If you have many blocks of text to center, use CSS inside tags in the head or in an external style sheet. See the example code below for how to set all text in the tags to be centered.
The text in every set of tags is centered on the page. If you want some paragraphs centered, while others are not, you can create a style class, as seen in the code below.
If you’re creating a center class, as shown in the example above, a paragraph can be centered using the code below, which «calls» the center class.
Once a class is created, it can be applied to any HTML tag containing words, images, and most other objects. For example, if you want a heading to be centered, you could add class=»center» to the tag or another heading tag.
Examples of centered text and objects
Below are examples of using the style attribute and «text-align» property to center text and objects on a web page.
Centering text in a paragraph
Example of centered text in a paragraph.
Example of centered text in a paragraph.
Centering an image
Centering a URL hyperlink
Centering text in a header tag
Example of centered text in a header tag
Example of centered text in a header tag
Centering text in a tag
Example of a centered sentence in a div tag.
Example of a second centered sentence in a div tag.
Example of centered sentence in a div tag.
Example of a second centered sentence in a div tag.
Related information
text-align
The text-align CSS property sets the horizontal alignment of the inline-level content inside a block element or table-cell box. This means it works like vertical-align but in the horizontal direction.
Try it
Syntax
/* Keyword values */ text-align: start; text-align: end; text-align: left; text-align: right; text-align: center; text-align: justify; text-align: justify-all; text-align: match-parent; /* Character-based alignment in a table column */ text-align: "."; text-align: "." center; /* Block alignment values (Non-standard syntax) */ text-align: -moz-center; text-align: -webkit-center; /* Global values */ text-align: inherit; text-align: initial; text-align: revert; text-align: revert-layer; text-align: unset;
The text-align property is specified in one of the following ways:
- Using the keyword values start , end , left , right , center , justify , justify-all , or match-parent .
- Using a value only, in which case the other value defaults to right .
- Using both a keyword value and a value.
Values
The same as left if direction is left-to-right and right if direction is right-to-left.
The same as right if direction is left-to-right and left if direction is right-to-left.
The inline contents are aligned to the left edge of the line box.
The inline contents are aligned to the right edge of the line box.
The inline contents are centered within the line box.
The inline contents are justified. Text should be spaced to line up its left and right edges to the left and right edges of the line box, except for the last line.
Same as justify , but also forces the last line to be justified.
Similar to inherit , but the values start and end are calculated according to the parent’s direction and are replaced by the appropriate left or right value.
When applied to a table cell, specifies the alignment character around which the cell’s contents will align.
Accessibility concerns
The inconsistent spacing between words created by justified text can be problematic for people with cognitive concerns such as Dyslexia.
Formal definition
Initial value | start , or a nameless value that acts as left if direction is ltr , right if direction is rtl if start is not supported by the browser. |
---|---|
Applies to | block containers |
Inherited | yes |
Computed value | as specified, except for the match-parent value which is calculated against its parent’s direction value and results in a computed value of either left or right |
Animation type | discrete |
Formal syntax
text-align =
start |
end |
left |
right |
center |
justify |
match-parent |
justify-all
Examples
Start alignment
HTML
p class="example"> Integer elementum massa at nulla placerat varius. Suspendisse in libero risus, in interdum massa. Vestibulum ac leo vitae metus faucibus gravida ac in neque. Nullam est eros, suscipit sed dictum quis, accumsan a ligula. p>
CSS
.example text-align: start; border: solid; >
How to center text in HTML?
The text in an HTML document can be horizontally centred by using the HTML tag. Since HTML5 did away with this tag, it is advised that you format the document’s text horizontally instead by using the CSS text-align property. The element is another name for this tag.
Note − Not supported in HTML5
Syntax
Following are the examples…
Example
In the following example we are using tag to make our text aligned to the center.
DOCTYPE html> html> body> p>center>Welcome to TutorialsPointcenter>p> p>The Best E-Way learning.p> body> html>
Output
On executing the above script, the text «welcome to tutorialspoint» will get displayed at the centre of our webpage.
Example: Using CSS
In the following example we are using css style (text-align:center) to make our text display at center.
DOCTYPE html> html> style> h1 p div style> body> h1>TUTORIALSPOINTh1> p>The E-Way Learning.p> div>Welcome To Tutorials.div> body> html>
Output
When we run the above script, the text is displayed in the centre of our webpage, as specified by the CSS property text-align: center.