html - 带有 CSS 的图像不显示
问题描述
这是我的 HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Will Treston - CPD Showcase</title>
<meta name="author" content="name">
<meta name="description" content="description here">
<meta name="keywords" content="keywords,here">
<link rel="stylesheet" href="index.css" type="text/css">
</head>
<body>
<div id="content" class="content">
<div id="profile_pic" class="profile_pic">
</div>
<div id="overview">
</div>
<div id="buttons">
</div>
</div>
</body>
</html>
这是我目前使用的 CSS:
body {
min-height: 100%;
width: 100%;
/* taken from https://css-tricks.com/perfect-full-page-background-image/ */
background: url("./images/index_background.jpg") no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
/* end */
}
.content {
min-height: 100%;
padding: 10%, 5%, 10%, 5%;
text-align: center;
display: inline-block;
}
.profile_pic {
height: 20%;
width: 20%;
background-image: url("./images/profile_pic.jpg");
}
目前,body 类图像按原样显示,但 profile_pic 类中的图像不是。图像命名正确,链接 100% 正确。
任何帮助和建议都会有所帮助。
解决方案
我认为您的问题在于您的 CSS 声明,.content
并且.profile_pic
- 因为您没有设置 a height
on ,所以没有任何内容.content
可供您参考.height
.profile_pic
height: 20%
这是一些修改后的CSS:
body {
min-height: 100vh;
height: 100vh; //using view height (vh) units makes it so it's always X% of the visible height
}
.content {
height: 100%;
width: 100%; //this works because it's direct parent, 'body', has a height. Therefore, this height is recognized as (100% of 100vh, or 100vh).
padding: 10% 5% 10% 5%;
text-align: center;
display: inline-block;
}
.profile_pic {
height: 20%;
display: inline-block;
width: 20%;
background-image: url("./images/profile_pic.jpg");
}
您不需要使用vh
单位,但这里的问题是您.profile_pic
的高度是 0 的 20%(因为.content
,它是直接父级,没有定义高度)。
另外,作为旁注,您的padding
呼叫.content
不正确-您不需要逗号。
推荐阅读
- python - 如何硬重新安装 PyCharm 和 Python?
- node.js - Next.js 中为什么没有通过 getServerSideProps 将 cookie 发送到服务器?
- python - 在数据类表示中传递参数
- javascript - 为什么 TypeError:由于传递 GeolocationPosition 时属性中的非法值而失败
- reactjs - 反应: (??) 速记不适用于 JSX 条件检查?
- javascript - SQL 中的“包含”和“不包含”方法或使用 Java 脚本 UDF
- c - fscanf 在输入的最后一行失败(仅负值?)
- xamarin - 如何在 Xamarin 中更新购物车
- python - 在 python 中创建序列时查找 range() 中的最后一个元素
- python - 我需要解一个二次方程