在html中怎样使用css样式?

html-css06

在html中怎样使用css样式?,第1张

在html网页中引入引入css主要有以下四种方式:\x0d\x0a(1)行内式\x0d\x0a

网页中css的导入方式\x0d\x0a\x0d\x0a(2)嵌入式\x0d\x0a\x0d\x0aP{ color:red }\x0d\x0a\x0d\x0a 嵌入式一般写在head中,对于单个页面来说,这种方式很方便。\x0d\x0a\x0d\x0a(3)导入式\x0d\x0a \x0d\x0a \x0d\x0a @import "jisuan.css" \x0d\x0a\x0d\x0a\x0d\x0a(4)链接式\x0d\x0a\x0d\x0a \x0d\x0a导入式和链接式差不多,都是从外部引入css文件。但是链接式对于客户端用户浏览网站时,效果会好些。

CSS是样式层叠表,有三种引入方式。下面,我们来看看HTML样式CSS的三种写法吧。

01

行内样式

CSS可以直接放到行内样式中引入即可,比如代码如下图:

<p style="color: bluebackground: red">

hello world!

</p>

02

嵌入式

还可以采用潜入方式引入CSS,就是把CSS写到<style>标签中,这种方式比较实用,如下图所示:

<style type="text/css">

h1 {color: red}

</style>

<h1>hello world</h1>

03

外部样式表

还有一种叫做外部样式,也就是把CSS写在另外一张页面上,然后再引用到指定页面就可以了,这种也很常见。

@import url(main.css)

1、直接使用行内样式,即在html标签上使用style

<div style="width:100pxheight:100pxbackground:red">div内容</div>

2、先定义css样式,再给html的class属性赋值

<style>

.content{

    width:100px

    height:100px

    background:red

}

</style>

<div class="content">

    div测试内容

</div>

总结:两种方式效果是一致的,只是第二种方式可进行重用,只要html元素class属性相同即可