HTML - 全域性樣式屬性



HTML 的**style 屬性**允許透過在 HTML 標籤內直接定義 CSS 屬性來對元素進行內聯樣式設定,從而影響其外觀和呈現。

為了確保您的網站可訪問且易於維護,請明智地使用它並考慮內容與表現分離的最佳實踐,儘管對於較小的樣式調整,可以使用 style 屬性。

語法

<element style="style_definitions">

style 屬性接受成對的屬性和屬性值。可以接受一個或多個用分號分隔的屬性和值。

應用於

由於 style 是 HTML 中的全域性屬性,因此所有可以包含文字的標籤都支援 style 屬性。一些例外是 <html>、<head>、<title> 等,因為它們是結構元素。

HTML style 屬性示例

以下示例將說明 HTML height 屬性,以及我們在哪裡以及如何使用此屬性!

使用 style 屬性為標題標籤設定樣式

在以下示例中,讓我們看看 style 屬性在 HTML 文件中的工作原理。如果我們與元素一起傳遞 style 屬性,則它會變成內聯 CSS。

<!DOCTYPE html>
<html>
<body style="text-align: center">
    <h3>HTML style Attribute</h3>
    <h1 style="color: green">
        Tutorials<span style="color: black">point</span>
    </h1>
</body>

</html>

單個 style 屬性上的多個屬性

考慮另一種情況,我們將使用 style 屬性內的多個內聯樣式,每個樣式用分號分隔。

<!DOCTYPE html>
<html>

<body style="text-align: center">
    <h3>HTML style Attribute</h3>
    <p style="color: darkblue; background-color: lightgray;
              font-size: 20px; font-style: italic; "> 
        HTML translate attribute is used to specify
        whether the content of an element should be 
        translated when the page is localized
    </p>
</body>

</html>

支援的瀏覽器

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