从屌丝到架构师的飞越(CSS篇)-CSS背景

一、介绍

CSS背景指的是可以设置网页背景单一颜色、网页背景为图片、网页背景图片居中于网页、网页背景图片网页固定位置、网页背景图片中网页中重复平铺等css背景样式,这里指通过CSS对对象设置背景属性,如通过CSS设置背景各种样式。

二、知识点介绍

1、CSS背景语法

2、CSS背景颜色

3、CSS背景图片

4、CSS重复背景图片

5、CSS背景图片的起始位置

6、CSS背景图片是否滚动

7、CSS背景图片样式整合

8、CSS背景扩展

三、上课对应视频的说明文档

1、CSS背景语法

background: background-color || background-image || background-repeat || background-attachment || background-position

2、CSS背景颜色

background-color:颜色 对象背景颜色

案例一:

<html>

<head>

<style type="text/css">

body {background-color: yellow}

h1 {background-color: #00ff00}

h2 {background-color: transparent}

p {background-color: rgb(250,0,255)}

p.no2 {background-color: gray; padding: 20px;}

</style>

</head>

<body>

<h1>这是标题 1</h1>

<h2>这是标题 2</h2>

<p>这是段落</p>

<p class="no2">这个段落设置了内边距。</p>

</body>

</html>

案例二:

<html>

<head>

<style type="text/css">

span.highlight

{

background-color:yellow

}

</style>

</head>

<body>

<p>

<span class="highlight">这是文本。</span> 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 这是文本。 <span class="highlight">这是文本。</span>

</p>

</body>

</html>

3、CSS背景图片

background-image:url(图片地址)  设置背景图像

<html>

<head>

<style type="text/css">

body {background-image:url(/i/eg_bg_04.gif);}

</style>

</head>

<body></body>

</html>

4、CSS重复背景图片

background-repeat :值  如何重复背景图片

<html>

<head>

<style type="text/css">

body

{

background-image:

url(/i/eg_bg_03.gif);

background-repeat: repeat-y

}

</style>

</head>

<body>

</body>

</html>

5、CSS背景图片的起始位置

background-position :值  背景图像的起始位置

水平 :left: 左  center: 中  right: 右

垂直 :top: 上 center: 中 bottom: 下

垂直与水平的组合 :x-% y-%  x-pos y-pos

<html>

<head>

<style type="text/css">

body

{

background-image:url('/i/eg_bg_03.gif');

background-repeat:no-repeat;

background-attachment:fixed;

background-position:center;

}

</style>

</head>

<body>

<body>

<p><b>提示:</b>您需要把 background-attachment 属性设置为 "fixed",才能保证该属性在 Firefox 和 Opera 中正常工作。</p>

</body>

</body>

</html>

top left, left top 等价于 0% 0%.

top, top center, center top 等价于 50% 0%.

right top, top right 等价于 100% 0%.

left, left center, center left 等价于 0% 50%.

center, center center 等价于 50% 50%.

right, right center, center right 等价于 100% 50%.

bottom left, left bottom 等价于 0% 100%.

bottom, bottom center, center bottom 等价于 50% 100%.

bottom right, right bottom 等价于 100% 100%.

<html>

<head>

<style type="text/css">

body

{

background-image: url('/i/eg_bg_03.gif');

background-repeat: no-repeat;

background-attachment:fixed;

background-position: 30% 20%;

}

</style>

</head>

<body>

<p><b>注释:</b>为了在 Mozilla 中实现此效果,background-attachment 属性必须设置为 "fixed"。</p>

</body>

</html>

6、CSS背景图片是否滚动

background-attachment :值  设置背景图片是否固定或者随着页面的其余部分滚动

<html>

<head>

<style type="text/css">

body

{

background-image:url(/i/eg_bg_02.gif);

background-repeat:no-repeat;

background-attachment:fixed

}

</style>

</head>

<body>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

<p>图像不会随页面的其余部分滚动。</p>

</body>

</html>

7、CSS背景图片样式整合

background: #00FF00 url(bgimage.gif) no-repeat fixed top;

<html>

<head>

<style type="text/css">

body

{

background: #ff0000 url(/i/eg_bg_03.gif) no-repeat fixed center;

}

</style>

</head>

<body>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

<p>这是一些文本。</p>

</body>

</html>

8、CSS背景扩展

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、CSS入门 1、css选择器 选择器的作用是“用于确定(选定)要进行样式设定的标签(元素)”。 有若干种形式的...
    宠辱不惊丶岁月静好阅读 1,628评论 0 6
  • 1.CSS基本概念 1.1 CSS的定义 CSS(Cascading Style Sheets)层叠样式表,主要用...
    寥寥十一阅读 1,875评论 0 6
  • CSS 指层叠样式表(Cascading Style Sheets),是一种用来为结构化文档(如 HTML 文档或...
    神齐阅读 2,134评论 0 14
  • 添加背景图 用 background-image 属性,在元素的背景中添加图形。background-image属...
    德育处主任阅读 5,972评论 2 7
  • 学习CSS的最佳网站没有之一 http://www.w3school.com.cn/tags/index.asp ...
    Amyyy_阅读 1,100评论 0 1