gpt4 book ai didi

html - Rems 不适用于移动设备

转载 作者:行者123 更新时间:2023-11-28 01:09:57 27 4
gpt4 key购买 nike

我在移动设备上遇到响应式设计问题,请查看代码和图片。这两者之间的区别是 html 根元素的字体大小,我将一个设置为百分比,另一个设置为 px,但是设置百分比的设置不正确,而设置 px 的设置正常。图片中的数字表示红色 div 的宽度。根据我的CSS代码的第一部分,div的宽度在第一张图片中应该是320px,但它是450并且不会改变,除非我将font-size设置为大于52.1%的百分比或其他东西,我不记得确切的数字,为什么?为什么在按百分比设置根元素的字体大小时,rems 在移动设备上不能以正确的方式缩放?请帮忙。

HTML

<!DOCTYPE>
<html>
<head>
<title>test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="main.css">
<script src = "main.js"></script>
<head>
<body>
<div id="div">Hello</div>
<div id="info"></div>
</body>
</html>

第一个 CSS

*{margin:0;padding:0;}
html{font-size:62.5%;}
#div{
width:50rem;
height:50rem;
margin:0 auto;
position:relative;
top:10rem;
font-size:5rem;
text-align:center;
line-height:50rem;
background:#ff0000;
}
#info{position:relative;top:10rem;font-size:5rem;}

@media screen and (max-width:500px){
html{font-size:40%;}
body{background:blue;}
}

第二个 CSS

*{margin:0;padding:0;}
html{font-size:62.5%;}
#div{
width:50rem;
height:50rem;
margin:0 auto;
position:relative;
top:10rem;
font-size:5rem;
text-align:center;
line-height:50rem;
background:#ff0000;
}
#info{position:relative;top:10rem;font-size:5rem;}

@media screen and (max-width:500px){
html{font-size:6px;}
body{background:blue;}
}

我的第一部分代码在我的 iphone 上的结果

enter image description here

我的代码第二部分在我的 iphone 上的结果

enter image description here

最佳答案

html{font-size:16px;}
body{font-size:62.5%;}

您需要问自己 62.5% 的是什么?据我所知,默认浏览器字体大小是 html 16px,“rem 单位是相对于根元素或 html 元素的。”

https://snook.ca/archives/html_and_css/font-size-with-rem

在我的 responsive.scss 中,我使用了最近发现的一些建议,抱歉我没有链接,但代码中的注释是解释性的:

@media only screen and (max-width:320px)
{
/* addresses a Mobile Webkit browsers - Safari & Chrome - issue with text downsizing in portrait mode */
html.touch.webkit,
html.touch.webkit body
{
font-size:22px;

.button
{
font-size:12.8px;
}
}

希望对您有所帮助。

关于html - Rems 不适用于移动设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38156959/

27 4 0
文章推荐: javascript - 用给定节点包裹单个节点
文章推荐: javascript - 选择框 html5 中不同选项的单独输入选项
文章推荐: css - 如何格式化
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com