关于HTML标签style属性的使用方法详解(附具体实例)


本文摘自PHP中文网,作者寻∝梦,侵删。

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

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

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

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

HTML style 属性实例

在 HTML 文档中使用 style 属性:

1

2

<h1 style="color:blue; text-align:center">This is a header</h1>

<p style="color:red">This is a paragraph.</p>

HTML标签style属性的语法:

1

<element style="value">

HTML标签style属性的属性值:

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

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

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

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

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

1

2

3

4

5

6

7

8

9

10

11

<!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指定的颜色。下面是示例代码和示意图:

1

2

3

4

5

6

7

8

9

10

11

<!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。下面是示例代码和示意图:

1

2

3

4

5

6

7

8

9

10

11

<!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属性的两种具体用法实例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<!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属性的使用方法详解(附具体实例)的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

如何更加优雅的从html中获取dom元素

html blockquote标签怎么用

html是什么语言

html搜索框怎么设置?html搜索框input标签的使用方法实例

html实现拖动的完整代码

html var标签怎么用

html隐藏控件方法

html切图是什么

html中<input>标签的class属性的值及其使用方法总结

html怎么设置表格高度

更多相关阅读请进入《style》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...