search
HomeWeb Front-endHTML TutorialHow to center text (horizontally and vertically) within a div block?

How to center text (horizontally and vertically) within a div block?

Sep 09, 2023 am 11:29 AM
Center verticallyCenter horizontallyCenter text

How to center text (horizontally and vertically) within a div block?

We can easily center text horizontally and vertically inside a div. Let’s take a look one by one.

Use the text-align attribute to center the text in the Div horizontally

To center text in a div horizontally, use the text-align attribute. The text-align attribute determines the alignment of line boxes within block-level elements. The following are possible values ​​-

  • left - The left edge of each line box is aligned with the left edge of the block-level element's content area.

  • right - The right edge of each line box is aligned with the right edge of the block-level element's content area.

  • center - The center of each line box is aligned with the center of the block-level element's content area.

  • justify - The edge of each line box should be aligned with the edge of the block-level element's content area.

  • String - The contents of the cells in the column will be aligned on the given string.

Example

Now let’s center the text in the div horizontally using the text-align attribute -

<!DOCTYPE html>
<html>
<head>
   <title>Align Horizontally</title>
   <meta name="viewport" content="width=device-width,
   initial-scale=1.0">
   <style>
      .demo {
         background-color: orange;
         border: 3px solid yellow;
         text-align: center;
      }
   </style>
</head>
<body>
   <h1 id="Software-Quality-Management">Software Quality Management</h1>
   <p>Software Quality Management ensures the required level of software quality is achieved.
   </p>
   <div class="demo">
      <p>This text in div is centered horizontally.</p>
   </div>
</body>
</html> 

Use the justify-content property to center text horizontally in a Div

Example

To center text in a div horizontally, use the justify-content property. Now let us see an example

<!DOCTYPE html>
<html>
<head>
   <title>Align Horizontally</title>
   <meta name="viewport" content="width=device-width,
   initial-scale=1.0">
   <style>
      .demo {
         background-color: orange;
         border: 3px solid yellow;
         display: flex;
         justify-content: center;
      }
   </style>
</head>
<body>
   <h1 id="Software-Quality-Management">Software Quality Management</h1>
   <p>Software Quality Management ensures the required level of software quality is achieved.
   </p>
   <div class="demo">
      <p>This text in div is centered horizontally.</p>
   </div>
</body>
</html> 

Use the padding property to vertically center text in a Div

To center the text in a div vertically, use the padding attribute. The padding property allows you to specify how much space should appear between an element's content and its border. The following CSS properties can be used to control lists. You can also set different values ​​for the padding on each side of the box using the following properties -

  • padding-bottom Specifies the bottom padding of the element.
  • padding-top Specifies the top padding of the element.
  • padding-left Specifies the left padding of the element.
  • padding-right Specifies the right padding of the element.
  • padding is used as shorthand for the aforementioned properties.

Example

Now let’s see an example of vertically centering text in a div using the padding attribute -

<!DOCTYPE html>
<html>
<head>
   <title>Align Vertically</title>
   <meta name="viewport" content="width=device-width,
   initial-scale=1.0">
   <style>
      .demo {
         background-color: orange;
         border: 3px solid yellow;
         padding: 50px 0;
      }
   </style>
</head>
<body>
   <h1 id="Software-Quality-Management">Software Quality Management</h1>
   <p>Software Quality Management ensures the required level of software quality is achieved.
   </p>
   <div class="demo">
      <p>This text in div is centered vertically.</p>
   </div>
</body>
</html>

Use the line-height property to vertically center text in a Div

To center the text in a div vertically, use the line-height property. The line-height property modifies the height of the inline boxes that make up a line of text.

The following are possible values ​​-

  • Normal - Instructs the browser to set the line height within the element to a "reasonable" distance.

  • number - The actual height of the row in the element is this value multiplied by the element's font size.

  • length - The height of the row in the element is the given value.

  • Percentage - The height of rows within an element is calculated as a percentage of the element's font size.

Example

Now let’s see an example of vertically centering text in a div using the line-height property -

<!DOCTYPE html>
<html>
<head>
   <title>Align Vertically</title>
   <meta name="viewport" content="width=device-width,
   initial-scale=1.0">
   <style>
      .demo {
         background-color: orange;
         border: 3px solid yellow;
         line-height: 150px;
         height: 200px;
      }
   </style>
</head>
<body>
   <h1 id="Software-Quality-Management">Software Quality Management</h1>
   <p>Software Quality Management is a process that ensures the required level of software
   quality is achieved.</p>
   <div class="demo">
      <p> This text in div is centered vertically.</p>
   </div>
</body>
</html>

The above is the detailed content of How to center text (horizontally and vertically) within a div block?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
The Versatility of HTML: Applications and Use CasesThe Versatility of HTML: Applications and Use CasesApr 30, 2025 am 12:03 AM

HTML is not only the skeleton of web pages, but is more widely used in many fields: 1. In web page development, HTML defines the page structure and combines CSS and JavaScript to achieve rich interfaces. 2. In mobile application development, HTML5 supports offline storage and geolocation functions. 3. In emails and newsletters, HTML improves the format and multimedia effects of emails. 4. In game development, HTML5's Canvas API is used to create 2D and 3D games.

What is the root tag in an HTML document?What is the root tag in an HTML document?Apr 29, 2025 am 12:10 AM

TheroottaginanHTMLdocumentis.Itservesasthetop-levelelementthatencapsulatesallothercontent,ensuringproperdocumentstructureandbrowserparsing.

Are the HTML tags and elements the same thing?Are the HTML tags and elements the same thing?Apr 28, 2025 pm 05:44 PM

The article explains that HTML tags are syntax markers used to define elements, while elements are complete units including tags and content. They work together to structure webpages.Character count: 159

What is the significance of <head> and <body> tag in HTML?What is the significance of <head> and <body> tag in HTML?Apr 28, 2025 pm 05:43 PM

The article discusses the roles of <head> and <body> tags in HTML, their impact on user experience, and SEO implications. Proper structuring enhances website functionality and search engine optimization.

What is the difference between <strong>, <b> tags and <em>, <i> tags?What is the difference between <strong>, <b> tags and <em>, <i> tags?Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

Please explain how to indicate the character set being used by a document in HTML?Please explain how to indicate the character set being used by a document in HTML?Apr 28, 2025 pm 05:41 PM

Article discusses specifying character encoding in HTML, focusing on UTF-8. Main issue: ensuring correct display of text, preventing garbled characters, and enhancing SEO and accessibility.

What are the various formatting tags in HTML?What are the various formatting tags in HTML?Apr 28, 2025 pm 05:39 PM

The article discusses various HTML formatting tags used for structuring and styling web content, emphasizing their effects on text appearance and the importance of semantic tags for accessibility and SEO.

What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?Apr 28, 2025 pm 05:39 PM

The article discusses the differences between HTML's 'id' and 'class' attributes, focusing on their uniqueness, purpose, CSS syntax, and specificity. It explains how their use impacts webpage styling and functionality, and provides best practices for

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools