当前位置: 首页 > news >正文

[CSS]CSS 的背景

请添加图片描述

文章目录

    • 1 背景颜色
    • 2 背景图片
    • 3 背景平铺
    • 4 背景图片位置
      • 4.1 方位名词
      • 4.2 精确单位
      • 4.3 混合单位
    • 5 背景图像固定(背景附着)
    • 6 背景复合写法
    • 7 背景色半透明
    • 8 背景总结


CSS 背景属性,可以给页面元素添加背景样式。可以设置背景颜色、背景图片、背景平铺、背景图片位置、背景图像固定等。

1 背景颜色

background-color 属性定义了元素的背景颜色。

语法:
默认值为:transparent(透明)
颜色值可以为:颜色单词 | 十六进制 | rgb值 | rgba值

background-color: 颜色值;
<body>
  <div class="demo1"></div>
  <div class="demo2"></div>
  <div class="demo3"></div>
  <div class="demo4"></div>
  <div class="demo5"></div>
</body>
  <style>
    div {
      display: inline-block;
      width: 100px;
      height: 100px;
      border: 1px black solid
    }
    .demo1 {
      /* 默认值 */
      background-color: transparent;
    }
    .demo2 {
      /* 颜色单词 */
      background-color: red;
    }
    .demo3 {
      /* 十六进行 */
      background-color: #6666f3;
    }
    .demo4 {
      /* rgb值 */
      background-color: rgb(221, 85, 85);
    }
    .demo5 {
      /* rgba值  最后一个值为透明度 */
      background-color: rgba(245, 4, 4, 0.3);
    }
  </style>

在这里插入图片描述

2 背景图片

background-image 属性可以设置元素的背景图像。

语法:

background-image : none | url (url)

取值:

  • none:无背景图片
  • url(url):背景图片的路径

背景图片后面的地址,千万不要忘记加 url(), 同时里面的路径不要加引号。

<body>
  <div class="demo1"></div>
  <div class="demo2"></div>
  <div class="demo3"></div>
</body>
  <style>
    div {
      display: inline-block;
      width: 300px;
      height: 300px;
      border: 1px solid black;
    }

    .demo1 {
      background-image: none;
    }

    .demo2 {
      background-image: url(./pic.jpeg);
    }

    .demo3 {
      /* 在设置背景图片的同时可以设置背景颜色 */
      background-color: aqua;
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
    }
  </style>

在这里插入图片描述

3 背景平铺

在 HTML 页面上对背景图像进行平铺,可以使用 background-repeat 属性。

语法:

background-repeat: repeat | no-repeat | repeat-x | repeat-y
  • repeat:背景图片在纵向和横向上平铺(默认值)
  • no-repeat:背景图片在纵向和横向上都不平铺
  • repeat-x:背景图片在横向上平铺
  • repeat-y:背景图片在纵向上平铺
<body>
  <div class="demo1"></div>
  <div class="demo2"></div>
  <div class="demo3"></div>
  <div class="demo4"></div>
  <div class="demo5"></div>
</body>
  <style>
    div {
      display: inline-block;
      width: 400px;
      height: 200px;
      border: 1px solid black;
    }
    .demo1 {
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
    }
    .demo2 {
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
      background-repeat: repeat;
    }
    .demo3 {
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
      background-repeat: no-repeat;
    }
    .demo4 {
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
      background-repeat: repeat-x;
    }
    .demo5 {
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
      background-repeat: repeat-y;
    }

  </style>

在这里插入图片描述

4 背景图片位置

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

语法:

background-position: x y;
  • x:横方向
  • y:纵方向

可以使用 方位名词 或者 精确单位

  • x:left | center | right
  • y:top | center | bottom

4.1 方位名词

  <div class="demo1"></div>
    div {
      display: inline-block;
      width: 400px;
      height: 200px;
      border: 1px solid black;
    }
    .demo1 {
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
      background-repeat: no-repeat;
      background-position: center center;
    }

在这里插入图片描述

如果指定的两个值都是方位名词,则两个值前后顺序无关,比如 left top 和 top left 效果一致,因为对于横向是left | center | right,对于纵向是top | center | bottom,可以根据单词区分出横向和纵向。

  <div class="demo2"></div>
  <div class="demo3"></div>
    .demo2 {
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
      background-repeat: no-repeat;
      background-position: left top;
    }
    .demo3 {
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
      background-repeat: no-repeat;
      background-position: top left;
    }

在这里插入图片描述

如果只指定了一个方位名词,另一个值省略,则第二个值默认居中对齐
指定横向:

  <div class="demo4"></div>
    .demo4 {
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
      background-repeat: no-repeat;
      background-position: left;
    }

在这里插入图片描述

指定纵向:

  <div class="demo4"></div>
    .demo5 {
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
      background-repeat: no-repeat;
      background-position: bottom;
    }

在这里插入图片描述

4.2 精确单位

  • 如果参数值是精确坐标,那么第一个肯定是 x 坐标,第二个一定是 y 坐标
  • 如果只指定一个数值,那该数值一定是 x 坐标,另一个默认垂直居中
  <div class="demo1"></div>
  <div class="demo2"></div>
  <div class="demo3"></div>
  <div class="demo4"></div>
  <style>
    div {
      display: inline-block;
      width: 400px;
      height: 200px;
      border: 1px solid black;
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
      background-repeat: no-repeat;
    }
    .demo1 {
      background-position: 50px 20px;
    }
    .demo2 {
      background-position: 20px 50px;
    }
    .demo3 {
      background-position: 20px;
    }
    .demo4 {
      background-position: 50px;
    }
  </style>

在这里插入图片描述

4.3 混合单位

  • 如果指定的两个值是精确单位和方位名词混合使用,则第一个值是 x 坐标,第二个值是 y 坐标
  <div class="demo1"></div>
  <div class="demo2"></div>
  <style>
    div {
      display: inline-block;
      width: 400px;
      height: 200px;
      border: 1px solid black;
      background-image: url(https://game.gtimg.cn/images/yxzj/web201706/images/comm/logo.png);
      background-repeat: no-repeat;
    }
    .demo1 {
      background-position: 20px top;
    }
    .demo2 {
      background-position: right 30px;
    }
  </style>

在这里插入图片描述

5 背景图像固定(背景附着)

background-attachment 属性设置背景图像是否固定或者随着页面的其余部分滚动。

语法:

background-attachment : scroll | fixed
  • scroll 背景图片随内容滚动(默认)
  • fixed 背景固定
<body>
  <p>hello</p>
  <p>hello</p>
  <p>hello</p>
  ...
</body>
  <style>
    body {
      background-image: url(../bg2.jpg);
      background-repeat: no-repeat;
    }
  </style>

在这里插入图片描述

  <style>
    body {
      background-image: url(../bg2.jpg);
      background-repeat: no-repeat;
      background-attachment: scroll;
    }
  </style>

在这里插入图片描述

  <style>
    body {
      background-image: url(../bg2.jpg);
      background-repeat: no-repeat;
      background-attachment: fixed;
    }
  </style>

在这里插入图片描述

6 背景复合写法

当使用简写属性时,没有特定的书写顺序,一般习惯约定顺序为:

background: 背景颜色 背景图片地址 背景平铺 背景图像滚动 背景图片位置;
<body>
  <p>hello</p>
  <p>hello</p>
  <p>hello</p>
  ...
</body>
  <style>
    body {
      background: yellowgreen url(../bg2.jpg) no-repeat scroll center top;
    }
  </style>

在这里插入图片描述

7 背景色半透明

CSS3 为我们提供了背景颜色半透明的效果。

语法:

background: rgba(0, 0, 0, 0.3);
  • 最后一个参数是 alpha 透明度,取值范围在 0~1之间
  • 我们习惯把 0.3 的 0 省略掉,写为 background: rgba(0, 0, 0, .3);
  • 注意:背景半透明是指盒子背景半透明,盒子里面的内容不受影响
  • CSS3 新增属性,是 IE9+ 版本浏览器才支持的
<body>
  <div class="demo1">
    <h1>hello</h1>
  </div>
  <div class="demo2">
    <h1>hello</h1>
  </div>
  <div class="demo3">
    <h1>hello</h1>
  </div>
</body>
  <style>
    body {
      background-image: url(../bg2.jpg);
    }
    div {
      width: 200px;
      height: 200px;
      background-color: blue;
    }
    .demo2 {
      background-color: rgba(0, 0, 255, 0.3);
    }
    .demo3 {
      background-color: rgba(0, 0, 255, .3);
    }
  </style>

在这里插入图片描述

8 背景总结

在这里插入图片描述
背景图片:实际开发常见于 logo 或者一些装饰性的小图片或者是超大的背景图片, 优点是非常便于控制位置. (精灵图也是一种运用场景)

相关文章:

  • 大数据ClickHouse进阶(六):Distributed引擎深入了解
  • PyTorch、TensorFlow和Jax构建神经网络模型的标准化流程
  • 【最详细demo】雪花算法详细解释
  • 基于JavaSwing开发扫雷小游戏(不同版本) 课程设计 大作业
  • 【云原生 | Kubernetes 系列】---Ceph集群安装部署
  • 分组卷积/转置卷积/空洞卷积/反卷积/可变形卷积/深度可分离卷积/DW卷积/Ghost卷积/
  • java计算机专业招聘网站计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
  • 如何用Python自动爬取全国30+城市地铁图数据?
  • 手把手教大家编译 flowable 源码
  • 2022主流内网穿透技术分享(含NAS)
  • 常见遍历方法 for循环、forEach、map、filter、find、findIndex、some、every
  • STM32国产替代方案
  • 学js的第十七天
  • 搭建自己的云存储空间|FastDFS分布式文件系统考虑一下?
  • <哈希及模拟实现>——《C++高阶》
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • AngularJS指令开发(1)——参数详解
  • es6
  • Git初体验
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • Javascript Math对象和Date对象常用方法详解
  • js对象的深浅拷贝
  • js数组之filter
  • mysql innodb 索引使用指南
  • PaddlePaddle-GitHub的正确打开姿势
  • RxJS: 简单入门
  • TypeScript迭代器
  • Vue.js 移动端适配之 vw 解决方案
  • 基于游标的分页接口实现
  • 入门到放弃node系列之Hello Word篇
  • nb
  • #传输# #传输数据判断#
  • #控制台大学课堂点名问题_课堂随机点名
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (附源码)springboot太原学院贫困生申请管理系统 毕业设计 101517
  • (论文阅读22/100)Learning a Deep Compact Image Representation for Visual Tracking
  • (排序详解之 堆排序)
  • (区间dp) (经典例题) 石子合并
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • .describe() python_Python-Win32com-Excel
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!
  • .NET Remoting学习笔记(三)信道
  • .net 设置默认首页
  • .NET 依赖注入和配置系统
  • .NET 应用架构指导 V2 学习笔记(一) 软件架构的关键原则
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • .NET/C# 使用反射注册事件
  • .NET微信公众号开发-2.0创建自定义菜单
  • .Net小白的大学四年,内含面经
  • @WebServiceClient注解,wsdlLocation 可配置
  • [ C++ ] STL_stack(栈)queue(队列)使用及其重要接口模拟实现
  • [20170705]lsnrctl status LISTENER_SCAN1
  • [android]-如何在向服务器发送request时附加已保存的cookie数据
  • [C\C++]读入优化【技巧】