【发布时间】:2021-04-28 17:37:22
【问题描述】:
我对以下 html/css 代码有疑问:
body{
font-size: 3.8em;
color: white;
background-color: black;
color: white;
font-family: Verdana, Geneva, sans-serif;
padding: 0px;
margin: 0px;
}
div{
margin: 0px;
margin-bottom: 1em;
text-align: center;
box-sizing: border-box;
width: 100%;
}
#title,#footer{
background-color: #1e9e40;
font-size: 0.5em;
padding: 0.6em;
}
#footer{
position:absolute;
bottom:0px;
margin-bottom: 0px;
}
#main{
padding: 1.3em;
background-color: #540054;
margin-top: 2.5em;
font-size: 0.65em;
text-align: justify;
}
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>css problems</title>
</head>
<body>
<div id="title">Title font should be smaller!</div>
<div id="main">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<div id="footer">This is the correct font size!</div>
</body>
</html>
当您在 Chrome 开发人员模式下查看渲染网页并激活移动视图时,#title 使用的字体大小比 #footer 大。我无法解释为什么。你可以吗?截图如下:
【问题讨论】:
-
我在 Chrome (Windows 10) 上看不到问题。您使用的是什么浏览器/系统?
-
如果您没有
<meta name="viewport" ...>设置,那么您的移动视图几乎就是运气。 -
@AHaworth 我也在 Windows 10 上使用 Chrome(64 位版本 88.0.4324.104)。但请确保您处于移动模式。首先你需要进入开发者模式(Ctrl+Shift+C),然后你必须切换到移动模式(Ctrl+Shift+M)。例如选择 Pixel 2 作为设备。但问题出现在许多设备上。
-
@Alohci 你是对的。但我仍然想了解这个问题。两个 div 应该有相同的字体大小。
-
我不建议您对初学者使用 ems 或 pxs。很难维护(ems)。这是example link,我在其中比较了 ems、rems 和 pxs。我建议您使用 rems 或 pxs ,除非您真正了解 em 在多个嵌套元素中的行为方式。是的,
<meta name="viewport" ..>会阻止你解决这个问题。