因此,我尝试通过使选择器类具有background-image
属性,然后使用<div>
标签对其进行调用来显示图像。选择器类是第一次在<head>
标签之间。一切似乎都正常:
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body, html {
margin: 0;
}
.bg {
/* The image used */
background-image: url("images.jpeg");
/* Full height */
height: 100%;
/* Center and scale the image nicely */
background-position: center;
background-size: cover;
}
</style>
</head>
<body>
<div class="bg"></div>
</body>
</html>
但是当我将选择器类放在单独的CSS文件中时,它似乎不起作用。这是HTML文件的代码:
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body, html {
margin: 0;
}
</style>
</head>
<body>
<div class="bg"></div>
</body>
</html>
这里是CSS:
.bg {
/* The image used */
background-image: url("images.jpeg");
/* Full height */
height: 100%;
/* Center and scale the image nicely */
background-position: center;
background-size: cover;
}
有人能告诉我我做错了什么吗?我先谢谢你!
您是否在HTML页面上导入了单独的CSS文件?
在HTML的link
内添加head
标记以将样式连接到HTML。