html - 为什么这个文本在它的位置?
问题描述
我有一个 HTML 页面,由于某种原因,文本超出了应有的位置。 这是一张图片。请注意,此图像被剪得更小图像中 的文本应该位于灰色区域。
我不是最擅长 CSS 和 HTML,所以这可能很明显。
我试图修复它,但我没有做任何工作,它只会在我试图修复它时变得更糟。
这是代码。
#screen {
position: fixed;
background: none;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
}
#levels {
position: fixed;
background: black;
left: calc(50% - 224px);
top: 0px;
width: 448px;
height: 126px;
}
#rockCount {
position: relative;
background: gray;
left: calc(50% - 128px);
top: 84px;
width: 256px;
height: 32px;
}
.level {
position: relative;
background: gray;
left: 0px;
top: 10px;
width: 64px;
height: 64px;
margin-left: 64px;
float: left;
}
#rock {
position: fixed;
background: black;
left: calc(50% - 128px);
top: calc(50% - 128px);
width: 256px;
height: 256px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Stone breaker</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="screen">
<div id="levels">
<div class="level" id="level0"></div>
<div class="level" id="level1"></div>
<div class="level" id="level2"></div>
<div id="rockCount">
<div id="countText">Rocks destroyed: 0/10</div>
</div>
</div>
<div id="rock"></div>
</div>
</body>
<script src="main.js"></script>
</html>
解决方案
首先,只有在没有更好的标签可用时才应该使用“div”元素。您可以在此处找到不同标签的概述: https ://www.w3schools.com/tags/
关于您的问题,我会说这是因为以下css:
#rockCount {
position: relative;
background: gray;
left: calc(50% - 128px);
top: 84px;
width: 256px;
height: 32px;
}
你说元素应该相对于它的当前位置定位。然后你说它应该是从这个位置向下 84px 的位置。
您可以在此处阅读有关定位的更多信息: https ://www.w3schools.com/css/css_positioning.asp
推荐阅读
- python - 为什么 Openpyxl 无法加载我现有的 .xlsx 文档?
- azure - Azure AD 在启动时运行应用程序
- azure - SelectPdf - NET Core 3.1 - 无法在 Azure 应用服务上将 HTML 转换为 PDF
- javascript - 如何使 Bootstrap 拆分按钮下拉菜单看起来像折叠导航栏中的标准下拉菜单
- c++ - vscode 无法识别扩展名 .cpp.in
- android - 我可以在 Android 手机上运行 IOS 应用程序吗?
- flutter - 如何使用合并语义将 Cupertino 添加到 appbar
- c# - 继承 IdentityUser 和 IdentityDbContext 时脚手架不起作用 - Wep Api
- javascript - 使用 Discord.js 向频道发送消息时遇到问题
- holoviews - 为什么我的 HoloViews DynamicMap 不会响应参数值的更改而更新?