We can easily center text horizontally and vertically inside a div. Let’s take a look one by one.
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.
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>
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>
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 -
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>
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.
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!