首页 > 解决方案 > 无法将背景图像应用于 div

问题描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Portfolio</title>
    <link rel="stylesheet" type="text/css" href="Project9.css">
    <link rel="icon" type="image/png" href="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-camera-512.png">
</head>
<body>

<div class="back">


</div>

</body>
</html>




*{

   margin: 0;
   padding: 0;
}

.back{

    
    background-image: url(images/bg.jpg);
    background-size: cover;
    background-attachment: fixed;
    background-repeat: no-repeat;
    background-position: center;
}

背景图像不会显示在 (.back) div 中,但它确实适用于 body 标签。我已经检查了文件的名称和所有内容,但它显示为空白。

标签: htmlcss

解决方案


你必须给 div awidth和 a height

.back{
    background-image: url("https://picsum.photos/200/300");
    background-size: cover;
    background-attachment: fixed;
    background-repeat: no-repeat;
    background-position: center;
    height: 100px;
    width: 100px;
}
<div class="back"></div>


推荐阅读