[英]My image won't show up in CSS but will in HTML
所以,我有一个我正在为学校制作的网站。 我正在尝试在 CSS 中添加图像,以便可以向它们添加样式,但它不会显示。 链接绝对没问题,因为当我在用于图像的 div 中键入任何字母时,它会显示在文本的参数中,这很令人困惑:所以我会告诉你:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HELLO</title>
</head>
<body>
<div class="fullImg">
<!-- if I say something like 'HI' in this div, part of the image shows up, but not without it! -->
</div>
</body>
<style type="text/css">
.fullImg {
background-image: url('img.jpg');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
height: 100%;
}
</style>
</html>
如果没有任何内容,您的 div 的高度为 0,因此看不到背景图像。 您需要做一些事情来确保 div 具有非零高度。 有多种方法可以做到这一点,包括设置height: 100vh
或min-height: 400px
。 设置height: 100%
无效。
您应该将样式放置在页面的头部元素中
您必须在 .fullImg 之前放置以下.fullImg
样式规则
html, body {
height: 100%;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.