HTML如何设置图片为背景图片

HTML如何设置图片为背景图片

  • 背景图像
    • 背景图像 - 水平或垂直平铺
    • 背景图像- 设置定位与不平铺
    • 背景属性

背景图像

background-image 属性描述了元素的背景图像.

默认情况下,背景图像进行平铺重复显示,以覆盖整个元素实体.

一,直接定义在body里面
body {background-image:url(‘xxx.gif’);}

二,标签选择器定义在head的style里面
body{
background-image:url(‘xxx.gif’);
}

背景图像 - 水平或垂直平铺

默认情况下 background-image 属性会在页面的水平或者垂直方向平铺。

一些图像如果在水平方向与垂直方向平铺,这样看起来很不协调

如果图像只在水平方向平铺 (repeat-x), 页面背景会更好些:

body
{
background-image:url(‘gradient2.png’);
background-repeat:repeat-x;
}

背景图像- 设置定位与不平铺

让背景图像不影响文本的排版

如果你不想让图像平铺,你可以使用 background-repeat 属性:

body
{
background-image:url(‘img_tree.png’);
background-repeat:no-repeat;
}

背景图像与文本显示在同一个位置,为了让页面排版更加合理,不影响文本的阅读,我们可以改变图像的位置。

可以利用 background-position 属性改变图像在背景中的位置:

body
{
background-image:url(‘img_tree.png’);
background-repeat:no-repeat;
background-position:right top;
}

背景属性

background 简写属性,作用是将背景属性设置在一个声明中。
background-attachment 背景图像是否固定或者随着页面的其余部分滚动。
background-color 设置元素的背景颜色。
background-image 把图像设置为背景。
background-position 设置背景图像的起始位置。
background-repeat 设置背景图像是否及如何重复。

你可能感兴趣的:(html,css,css3)