Home  >  Article  >  Web Front-end  >  How to center text (horizontally and vertically) within a div block?

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

王林
王林forward
2023-09-09 11:29:07890browse

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>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>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>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>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.com. If there is any infringement, please contact admin@php.cn delete