CSS-背景属性

目录

背景属性

background-color (背景颜色 )

background-image (背景图片 )

background-repeat (背景图平铺方式 )

[no-repeat 不平铺](#no-repeat 不平铺)

[repeat-x 水平方向平铺](#repeat-x 水平方向平铺)

[repeat-y 垂直方向平铺](#repeat-y 垂直方向平铺)

[repeat 平铺](#repeat 平铺)

background-position (背景图位置)

background-size (背景缩放)

background-attachment (背景图固定)

background (复合属性)


背景属性

背景属性分类:

  • 背景颜色
  • 背景图片
  • 背景屏铺方式
  • 背景图位置
  • 背景缩放
  • 背景固定
  • 背景复合属性

background-color (背景颜色 )

属性名:backgourd-color

属性值:跟color的使用方法一样

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        
         a{
            background-color: red;
        }
    </style>
</head>
<body>

       <a href="KD.html">凯文杜兰特</a>
    
</body>
</html>

网页效果:

background-image (背景图片 )

网页中,使用背景图片来装饰图片效果

属性名:background-image

属性值:url ()

css 复制代码
​
   div{
            width:  1852px;
            height: 927px;
            background-image:url(./kd.jpg);
        }

​

网页效果:

background-repeat (背景图平铺方式 )

属性名:background-repeat

属性值:

  • no-repeat 不平铺
  • repeat(默认效果) 平铺
  • repeat-x 水平方向平铺
  • repeat-y 垂直方向平铺
no-repeat 不平铺
css 复制代码
div{
            width:  1000px;
            height: 600px;
            background-color: red;
            background-image:url(./a.png);
            background-repeat: no-repeat;
        }

不平铺我们会发现这张图片会呈现在左上角的位置

repeat-x 水平方向平铺
repeat-y 垂直方向平铺
repeat 平铺

background-position (背景图位置)

属性名:background-position

属性值:

  • 横坐标px,纵坐标px
  • 关键字
关键字 位置
left 左边
right 右边
center 中间
top 顶部
bottom

只写一个关键字,另一个方向默认居中

数字只写一个值表示水平方向,垂直方向为居中

css 复制代码
 div{
            width:  1000px;
            height: 600px;
            background-color: red;
            background-image:url(./a.png);
            background-repeat: no-repeat;
            background-position: 400px 300px;
        }

background-size (背景缩放)

属性:background-size

属性值:

  • 关键字
  • 百分比
  • 数字px
关键字 说明
cover 等比例缩放背景图片以完全覆盖背景区,可能背景图片部分看不见
contain 等比例缩放背景图片以完全装入背景区,可能背景部分空白

background-attachment (背景图固定)

属性:background-attachment

属性值:fixed

作用:背景不会随着内容滚动

background (复合属性)

属性:background

属性值:背景色 背景图 背景图平铺方式 背景图位置/背景图缩放 背景图固定

css 复制代码
 div{
            width: 400px;
            height: 500px;
            background: red url(./a.png) repeat 100px 100px/cover fixed;
        }
相关推荐
gnip1 小时前
企业级配置式表单组件封装
前端·javascript·vue.js
一只叫煤球的猫2 小时前
写代码很6,面试秒变菜鸟?不卖课,面试官视角走心探讨
前端·后端·面试
excel3 小时前
Three.js 材质(Material)详解 —— 区别、原理、场景与示例
前端
掘金安东尼3 小时前
抛弃自定义模态框:原生Dialog的实力
前端·javascript·github
hj5914_前端新手7 小时前
javascript基础- 函数中 this 指向、call、apply、bind
前端·javascript
薛定谔的算法7 小时前
低代码编辑器项目设计与实现:以JSON为核心的数据驱动架构
前端·react.js·前端框架
Hilaku7 小时前
都2025年了,我们还有必要为了兼容性,去写那么多polyfill吗?
前端·javascript·css
yangcode7 小时前
iOS 苹果内购 Storekit 2
前端
LuckySusu7 小时前
【js篇】JavaScript 原型修改 vs 重写:深入理解 constructor的指向问题
前端·javascript
LuckySusu7 小时前
【js篇】如何准确获取对象自身的属性?hasOwnProperty深度解析
前端·javascript