耍一耍 background

1,132 阅读4分钟

前几天看到 chokcoco 大佬的 CSS奇思妙想 -- 使用 background 创造各种美妙的背景 ,仿佛发现了新大陆。回想起来, background 虽然总是在用,但是却又似懂非懂,话在嘴边但又说不出来的那种感觉。所以今天以学习为目的,整理一下 background 基础知识点以及炫酷使用。

基础使用

这里先列举一下 background 的基础用法

  • background-color 设置背景颜色

  • background-image 设置背景图片

    • 可以同时设置背景图片和背景颜色,这样背景颜色将会成为图片的背景色
    • 如果背景的图片小于元素,则背景图片会自动在元素中平铺将元素铺满
    • 如果背景的图片大于元素,将会一个部分背景无法完全显示
    • 如果背景图片和元素一样大,则会直接正常显示
  • background-repeat 用来设置背景的重复方式

    可选值:

    • repeat 默认值,背景会沿着 x 轴 y 轴双方向重复
    • repeat-x 沿着x轴方向重复
    • repeat-y 沿着y轴方向重复
    • no-repeat 背景图片不重复
  • background-position 用来设置背景图片的位置

    设置方式:

    • 通过 top left right bottom center 几个表示方位的词来设置背景图片的位置,使用方位词时须要同时指定两个值,如果只写一个则第二个默认就是 center
    • 通过偏移量来指定背景图片的位置,水平方向的偏移量、垂直方向变量
  • background-clip 设置背景的范围

    可选值:

    • border-box 默认值,背景会出现在边框的下边
    • padding-box 背景不会出现在边框,只出现在内容区和内边距
    • content-box 背景只会出现在内容区
  • background-origin 背景图片的偏移量计算的原点

    • padding-box 默认值,背景图片的偏移量从内边距处开始计算
    • content-box 背景图片的偏移量从内容区处计算
    • border-box 背景图片的变量从边框处开始计算
  • background-size 设置背景图片的大小

    • 第一个值表示宽度,第二个值表示高度,如果只写一个,则第二个值默认是 auto
    • cover 图片的比例不变,将元素铺满
    • contain 图片比例不变,将图片在元素中完整显示
  • background-attachment 背景图片是否跟随元素移动

    可选值:

    • scroll 默认值 背景图片会跟随元素移动
    • fixed 背景会固定在页面中,不会随元素移动
  • backgound 背景相关的简写属性,所有背景相关的样式都可以通过该样式来设置,并且该样式没有顺序要求,也没有哪个属性是必须写的

    注意:

    • background-size 必须写在 background-position 的后边,并且使用 / 隔开
    • background-origin 要在 background-clip 的前边

开始富有想象力

如果想单纯通过 CSS 代码来将我们的背景变得绚丽,我们自然需要一些其他基础知识:

  • 通过渐变可以设置一些复杂的背景颜色,可以实现从一个颜色向其他颜色过渡的效果,使用 CSS 渐变

    linear-gradient() 线性渐变,颜色沿着一条直线发生变化

    repeating-linear-gradient() 可以平铺的线性渐变

    • 线性渐变的开头,我们可以指定一个渐变的方向

      • to left
      • to right
      • to bottom
      • to top
      • deg 表示度数
      • turn 表示圈
    • 渐变可以同时指定多个颜色,多个颜色默认情况下平均分布,也可以手动指定渐变的分布情况

    radial-gradient() 径向渐变(放射性的效果)

    repeating-radial-gradient() 可以平铺的径向渐变

    • 默认情况下径向渐变的形状根据元素的形状来计算的

      • 正方形 --> 圆形
      • 长方形 --> 椭圆形
    • 语法:

      radial-gradient(类型 大小 at 位置, 颜色 位置 ,颜色 位置 ,颜色 位置)

      • 类型 circle 圆形 ellipse 椭圆

      • 大小: closest-side 近边 closest-corner 近角 farthest-side 远边 farthest-corner 远角

      • 位置: top right left center bottom conic-gradient() 圆锥形渐变

background-image: linear-gradient(60deg, red 20px, blue 30px, yellow 40px);

image

background-image: repeating-linear-gradient(0.6turn, red, blue 10px, yellow 50px);

image

background-image: radial-gradient(circle farthest-side at 40px 50px, red, blue 60px, yellow);

image

background-image: repeating-radial-gradient(circle farthest-side at 40px 50px, red, blue 60px, yellow);

image

background:conic-gradient(red 20%, blue 50%, yellow 80%);

image

  • mix-blend-mode 属性描述了元素的内容应该与元素的直系父元素的内容和元素的背景如何混合。 可选值:
    • normal
    • multiply
    • screen
    • overlay
    • darken
    • lighten
    • color-dodge
    • color-burn
    • hard-light
    • soft-light
    • difference
    • exclusion
    • hue
    • saturation
    • color
    • luminosity
.box1 {
    width: 200px;
    height: 200px;
    position: absolute;
    background-image: repeating-linear-gradient(-45deg, red, blue 10px, yellow 50px);
}
.box2 {
    width: 200px;
    height: 200px;
    position: absolute;
    background-image: repeating-linear-gradient(45deg, red, blue 10px, yellow 50px);
    mix-blend-mode: screen;
}

image

耍一下

加个动画鬼畜一下

image

源码地址:github.com/lixiang2020…