HTML style 属性允许通过直接在 HTML 标签中定义 CSS 属性来对元素进行内联样式设置,从而影响其外观和呈现。
为确保您的网站易于访问和维护,请明智地使用它并考虑将内容和呈现方式分开的最佳实践,尽管对于微小的样式调整,可以使用样式属性。
语法
<element style="style_definitions">
style 属性接受 pair 属性和 properties value 中的值。用分号分隔的一个或多个属性和值是可以接受的。
适用对象
由于 style 是 HTML 中的全局属性,因此所有可以包含文本的标签都支持 style 属性。一些异常是 <html>、<head>、<title> 等,因为它们是结构元素。
HTML 样式属性示例
以下示例将说明 HTML height 属性,我们应该在哪里以及如何使用此属性!
使用 style 属性设置标题标签的样式
在以下示例中,让我们看看 HTML 文档中样式属性的工作原理。如果我们将样式属性与元素一起传递,那么它就会变成内联 CSS。
<!DOCTYPE html>
<html>
<body style="text-align: center">
<h3>HTML style Attribute</h3>
<h1 style="color: green">
Qikepu<span style="color: black">.com</span>
</h1>
</body>
</html>
单个样式属性上的多个属性
考虑另一种情况,我们将在样式属性中使用多个内联样式,每个样式由半列分隔。
<!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>
支持的浏览器
浏览器 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
是否支持 | Yes | Yes | Yes | Yes | Yes |