HTML中怎么插入图片作为背景?

html-css023

HTML中怎么插入图片作为背景?,第1张

visual studio简称为vs。下面,我们来看看HTML中怎么插入图片作为背景图片吧。

打开vs

打开visual studio软件,并新建一个文件夹为img,把图片粘贴到文件夹中,如下图所示:

<body style=" background:url(img/img01.jpg)">

设置背景图片的代码为 style=" background:url(img/img01.jpg)",如下图所示:

在浏览器中查看

右击【HTMLPage.htm】,选择【在浏览器中查看】,如下图所示:

预览效果

在浏览器中的预览效果如下图所示:

1、设置背景图片只需在css样式中设置backgroud-image属性:

<style type="text/css" >

body{

background-image:url(你的图片地址)

background-position:center       

background-repeat:repeat-y;

}

</style>

2、其中, background-image:url(你的图片地址)表示背景图片的存放路径;

background-position:表示的是背景图片的位置,主要有top(上)、bottom(下)、left(左)、right(右)这四个取值;

background-repeat:表示的图片的平铺方式。默认情况是平铺,一般设置为no-repeat,表示的是不平铺。

背景颜色属性(background-color):

这个属性为HTML元素设定背景颜色,相当于HTML中bgcolor属性。

body {background-color:#99FF00}:

上面的代码表示Body这个HTML元素的背景颜色是翠绿色的。

演示示例

背景图片属性(background-image):

这个属性为HTML元素设定背景图片,相当于HTML中background属性。

<body style="background-image:url(../images/css_tutorials/background.jpg)">

上面的代码为Body这个HTML元素设定了一个背景图片。

演示示例

背景重复属性(background-repeat):

这个属性和background-image属性连在一起使用,决定背景图片是否重复。如果只设置background-image属性,没设置background-repeat属性,在缺省状态下,图片既横向重复,又竖向重复。

repeat-x 背景图片横向重复;

repeat-y 背景图片竖向重复;

no-repeat 背景图片不重复;

body {background-image:url(../images/css_tutorials/background.jpg)background-repeat:repeat-y}

上面的代码表示图片竖向重复。

演示示例

背景附着属性(background-attachment):

这个属性和background-image属性连在一起使用,决定图片是跟随内容滚动,还是固定不动。这个属性有两个值,一个是scroll,一个是fixed。缺省值是scroll。

body {background-image:url(../images/css_tutorials/background.jpg)background-repeat:no-repeatbackground-attachment:fixed}

上面的代码表示图片固定不动,不随内容滚动而动。

演示示例

背景位置属性(background-position):

这个属性和background-image属性连在一起使用,决定了背景图片的最初位置。

body {background-image:url(../images/css_tutorials/background.jpg)background-repeat:no-repeatbackground-position:20px 60px}

上面的代码表示背景图片的初始位置距离网页最左面20px,距离网页最上面60px。

演示示例

背景属性(background):

这个属性是设置背景相关属性的一种快捷的综合写法, 包括background-color, background-image, background-repeat, backgroundattachment, background-position。

body {background:#99FF00 url(../images/css_tutorials/background.jpg) no-repeat fixed 40px 100px}

上面的代码表示,网页的背景颜色是翠绿色,背景图片是background.jpg图片,背景图片不重复显示,背景图片不随内容滚动而动,背景图片距离网页最左面40px,距离网页最上面100px。