HTML - 對齊屬性



HTML align 屬性用於指定 HTML 元素內容的對齊方式(居中、左對齊或右對齊)。

但是,在現代 HTML 中,align 屬性已被棄用,取而代之的是使用 CSS 進行佈局和對齊。CSS 是現代 Web 開發中用於設定元素樣式和定位的標準方法,因為它提供了對對齊和顯示的更精確控制。

語法

<tag align = "value"></tag>

應用於

下面列出的元素允許使用 HTML align 屬性。

元素 描述
<img> HTML <img> 標籤用於在文件中新增影像。
<table> HTML <table> 標籤允許我們透過提供行和列功能以組織化的方式放置資料。
<iframe> HTML <iframe> 是一個內聯框架,允許我們在當前 HTML 文件中嵌入另一個文件。
<caption> HTML <caption> 標籤用於為表格元素指定標題。
<col> HTML <col> 標籤用於提供有關列的資訊。
<applet> HTML <applet> 標籤用於在 HTML 文件中嵌入 Java applet。
<colgroup> HTML <colgroup> 標籤用於描述表格中一個或多個列的集合,以便進行格式化。
<hr> HTML <hr> 標籤用於在網頁上建立水平線。
<legend> HTML <legend> 標籤用於指定 fieldset 元素的標題。
<tbody> HTML <tbody> 標籤用於在 HTML 表格中建立一個單獨的語義塊,定義表格的主體內容。
<td> HTML <td> 標籤用於定義表格的資料單元格,用作 tr 元素的子元素。
<tfoot> HTML <tfoot> 標籤用於在 HTML 表格中對頁尾內容進行分組。
<th> HTML <th> 標籤指定 HTML 表格的表頭單元格。
<thead> HTML <thead> 標籤用於對 HTML 表格的表頭內容進行分組。
<tr> HTML <tr> 標籤用於定義表格中的每一行。

HTML align 屬性示例

在以下示例中,我們討論瞭如何使用 align 屬性的不同方法。

使用 h1 和 p 標籤的 align 屬性

以下程式碼演示瞭如何使用 align 屬性對齊 h1 和 p 標籤。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML align attribute</title>
</head>
<body>
   <!--example of the align attribute-->
   <h1 align="left">This is h1 heading.</h1>
   <p align="center">Text within the p tag.</p>
</body>

</html>

使用 div 標籤的 align 屬性

考慮另一種情況,我們將對 div 元素使用 align 屬性。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML align attribute</title>
   <style>
      div {
         color: green;
         font-weight: bolder;
      }
   </style>
</head>

<body>
   <!--example of the align attribute-->
   <div align="right">
      <p>
      Text within the p tag(it will display
      the right side of the screen view 
      because we have used align = "right").
      </p>
   </div>
</body>

</html>

使用 form 標籤的 align 屬性

讓我們看看以下示例,我們將對錶單使用 align 屬性。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML align attribute</title>
   <style>
      form {
         width: 300px;
         height: 210px;
         background-color: rgb(21, 114, 185);
         color: white;
         border-radius: 10px;
      }

      form button {
         width: 100px;
         padding: 5px;
      }
   </style>
</head>

<body>
   <!--example of the align attribute-->
   <form align="center">
      <h1 align="center">Login</h1> 
      Username: <input type="text">
      <br>
      <br> Password:<input type="password">
      <br>
      <br>
      <button>Login</button>
   </form>
</body>

</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
align
html_attributes_reference.htm
廣告