CSS笔记 - 背景

背景

  • 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-position:(水平方向偏移量) (垂直方向偏移量)

  • background-origin:背景图片的偏移量计算的原点

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

    • border-box 默认值,背景会出现在边框的下边
    • padding-box 背景不会出现在边框,只出现在内容区和内边距
    • content-box 背景只会出现在内容区
  • background-size:设置背景图片的大小

    • 方式一

      background-size: (width) (height) 直接设置宽度和高度,如果只写一个,第二个值默认为auto

    • 方式二

      background-size:cover 图片比例不变,将元素铺满

      background-size:contain 图片比例不变,将图片在元素中完整显示

  • background-attachment:背景图片是否跟随元素移动(滚动条下拉时背景图片是否移动)

    • scroll 默认值,背景图片会跟随元素移动
    • fixed 背景会固定在页面中,不会随元素移动
    .box1{
        width: 500px;
        height: 500px;
        
        background-color: #bfa;
        background-image: url("./img/2.jpg");
        background-repeat: no-repeat;
        
        background-position: center center;
        background-size: contain;
    }
    
    .box3{
        width: 500px;
        height: 500px;
        /*简写设置背景*/
        background: url('./img/2.jpg') #bfa center center/contain border-box no-repeat
    }
    /*
    背景相关的简写属性,所有背景相关的样式都可以通过该样式来设置,且没有顺序要求,也不一定全都要写
    注意:
    	background-size必须写在background-position的后边,并且使用"/"隔开
    	background-origin必须写在background-clip的前面
    */
    
posted @   Solitary-Rhyme  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示