本篇文章为大家讲解的是关于HTML标签的style属性的定义和作用,还有关于HTML中的style属性的使用方法实例,还有关于HTML标签中的style属性的各种样式解析。让我们一起来看本篇文章吧

首先我们介绍下HTML标签style属性的定义吧:

style 属性是规定元素的行内样式(inline style)

style 属性将覆盖任何全局的样式设定,例如在 <style> 标签或在外部样式表中规定的样式。

HTML style 属性实例

在 HTML 文档中使用 style 属性:

<h1 style="color:blue; text-align:center">This is a header</h1>
<p style="color:red">This is a paragraph.</p>
登录后复制

HTML标签style属性的语法:

<element style="value">
登录后复制

HTML标签style属性的属性值:

style_definition : 一个或多个由分号分隔的 CSS 属性和值。

HTML中的style属性的各种样式解析

1.style属性可以设置字体样式(font-family:)、字体大小(font-size:)、文本的颜色(color:)等内容。它可以在

每个不同的标签中设置,也可以放置到CSS样式表中进行设置。因为我的文章都是跟随着http://www.codecademy.com来

写的,目前还没有介绍到CSS,所以在这里暂时先不涉及CSS,仅仅是在标签中进行设置,下面是示例代码和示意图:

<!DOCTYPE html>
<html>
<head>
<title>color the text</title>
</head>
<!-- now i will color the text-->
<body>
<h3 style="color:blue;font-size=20px">Hello World!</h3>
<p style="color:red;font-size=10px">你好!!!!</p>
</body>
</html>
登录后复制

注意:

a)我用的浏览器是chrome,在其中编辑并显示时总是发现设置的10px字体没有应用,通过查找chrome设置,发现

是在chrome中设置的最小字体是12px,所以10px字体大小没有用,把chrome浏览器的最小字体改小就好了。

b)style属性中的赋值方式是:属性名称:值,而不是习惯的等号=,如果用等号,则style效果不会被执行。

2.style样式中也可以设置背景颜色,style中包含属性background-color,设置它的颜色值就可以使整个标签范围内的背景颜色变

为background-color指定的颜色。下面是示例代码和示意图:

<!DOCTYPE html>
<html>
<head>
<title>color the text</title>
</head>
<!-- now i will color the text-->
<body>
<h3 style="color:blue;font-size=20px ;background-color:yellow">Hello World!</h3>
<p style="color:red;font-size=10px ;background-color:green">你好!!!!</p>
</body>
</html>
登录后复制

3.文本对齐

有些标签属性中有align属性,可以设置文本对齐方式,在style中也可以设置类似的属性。这个属性值就是text-align。它的

取值包括left、center和right。下面是示例代码和示意图:

<!DOCTYPE html>
<html>
<head>
<title>color the text</title>
</head>
<!-- now i will color the text-->
<body>
<h3 style="color:blue;font-size=20px ;background-color:yellow;text-align:center">Hello World!</h3>
<p style="color:red;font-size=10px ;background-color:green;text-align:right">你好!!!!</p>
</body>
</html>
登录后复制

关于html的style属性的两种具体用法实例:

<!DOCTYPE html>
<html>
     <head>
        <meta charset="utf-8">
        <title>练习使用HTML</title>
        <style>
            /*标签选择器*/
            h4 {
                color: blue;
                text-align: right;
            }
</style>
    </head>
     <body>
        <!--规定元素的行内样式-->
        <p style="color:red;text-align:center">测试html的style属性</p>
        <h4>h41</h4>
        <h4>h42</h4>
    </body>
 </html>
登录后复制

【小编的相关推荐】

html5 details标签的作用是什么?<details>标签的使用方法介绍(附使用实例)

html table表格是什么?<table>标签中各种属性的使用方法

以上就是关于HTML标签style属性的使用方法详解(附具体实例)的详细内容,更多请关注Work网其它相关文章!

09-18 20:57