如何將文字(水平和垂直)居中放置在 div 塊內?
我們可以輕鬆地將文字水平和垂直居中放置在 div 內。讓我們逐一看看它們。
使用 text-align 屬性水平居中 div 中的文字
要水平居中 div 中的文字,請使用 text-align 屬性。text-align 屬性決定塊級元素內行框的對齊方式。以下是可能的取值:
left - 每個行框的左邊緣與塊級元素內容區域的左邊緣對齊。
right - 每個行框的右邊緣與塊級元素內容區域的右邊緣對齊。
center - 每個行框的中心與塊級元素內容區域的中心對齊。
justify - 每個行框的邊緣應與塊級元素內容區域的邊緣對齊。
string - 列中單元格的內容將與給定的字串對齊。
示例
現在讓我們使用 text-align 屬性水平居中 div 中的文字:
<!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>
使用 justify-content 屬性水平居中 div 中的文字
示例
要水平居中 div 中的文字,請使用 justify-content 屬性。現在讓我們來看一個例子。
<!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>
使用 padding 屬性垂直居中 div 中的文字
要垂直居中 div 中的文字,請使用 padding 屬性。padding 屬性允許您指定元素的內容與其邊框之間應留出多少空間。可以使用以下 CSS 屬性來控制列表。您還可以使用以下屬性為框的每一側設定不同的填充值:
- padding-bottom 指定元素的底部填充。
- padding-top 指定元素的頂部填充。
- padding-left 指定元素的左側填充。
- padding-right 指定元素的右側填充。
- padding 作為前面屬性的簡寫。
示例
現在讓我們來看一個使用 padding 屬性垂直居中 div 中文字的例子:
<!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>
使用 line-height 屬性垂直居中 div 中的文字
要垂直居中 div 中的文字,請使用 line-height 屬性。line-height 屬性修改構成文字行的內聯框的高度。
以下是可能的取值:
normal - 指示瀏覽器將元素中行的“合理”距離設定為高度。
number - 元素中行的實際高度是此值乘以元素的字型大小。
length - 元素中行的高度是給定的值。
percentage - 元素中行的高度計算為元素字型大小的百分比。
示例
現在讓我們來看一個使用 line-height 屬性垂直居中 div 中文字的例子:
<!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>
© .
All rights reserved.
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP