Fit div size to background image

后端 未结 3 959
你的背包
你的背包 2020-12-03 23:41

I\'m trying to set the size (both width and height) of a div to match it\'s background image size, but I can\'t get it working. The background image size has to be in percen

3条回答
  •  抹茶落季
    2020-12-04 00:29

    *{
        padding: 0;
        margin: 0;
    }
    
    
    div{
        width: 100%;
    }
    div figure{
        padding-top: 36.56%;  /* 702px/1920px = 0.3656 */
        display: block;
        background: url("https://st.fl.ru/images/landing/bg2.jpg") no-repeat center top;
        background-size: cover;  
    }

提交回复
热议问题