gpt4 book ai didi

javascript - 如何使用 javascript 相对于其父尺寸缩放具有给定类的每个元素的字体?

转载 作者:行者123 更新时间:2023-11-27 23:20:11 25 4
gpt4 key购买 nike

我正在尝试制作我的第一个响应式网站,但遇到了一些问题。

特别是,我需要一些文本居中,但是当我在 FireFox Dev Edition 中使用响应式设计模式尝试不同的窗口大小时,居中的文本不会真正适合其容器。所以,我认为 JavaScript 可能是最好的行动方案。

虽然这适用于使用 document.getElementById('id'); 选择的元素,我似乎无法通过使用 for 让它与类一起工作循环。

这是我用于具有 ID 的元素的代码:

document.getElementById('header-text').style.fontSize = ((19/47.73) * document.getElementById('header-title').getBoundingClientRect().height).toString().concat('px');

这是我尝试使用通用类对元素使用的内容。

var allIconLinks = document.getElementsByClassName('extras-iconlink-text');
var i;
for ( i = 0; i > allIconLinks.length; i++ ) {
allIconLinks[i].style.fontSize = (19.2/42.43) * allIconLinks[i].parentElement.getBoundingClientRect().height.toString().concat('px');
}

值得注意的是,在 for 循环中根本没有执行任何内容,我尝试使用 console.log功能,没有任何记录。

如果它有帮助,这里是元素的 HTML 和 CSS(我没有在这个片段中显示,但我有 <meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0' /> ):

<body>
<div id = 'header'>
<div id = 'header-title'>
<span id = 'header-icons'>
<img id = 'icon-layer1' src = '../../assets/logos/layer1.svg'>
<img id = 'icon-layer2' src = '../../assets/logos/layer2.svg'>
</span>
<span id = 'header-text'>Karakaras Portfolio</span>
</div>
<div id = 'header-extras'>
<span id = 'extras-info' class = 'extras-iconlink'>
<img id = 'icon-info' src = '../../assets/icons/info.svg'>
<span class = 'extras-iconlink-text'>About</span>
</span>
<span id = 'extras-projects' class = 'extras-iconlink'>
<img id = 'icon-info' src = '../../assets/icons/projects.svg'>
<span class = 'extras-iconlink-text'>Dev projects</span>
</span>
</div>
</div>
</body>
#header {
top: 0%;
left: 0%;
width: 100%;
height: 7.5%;
background-color: #7161ef;
position: fixed;
}
#header #header-title {
top: 5%;
left: 0.15%;
width: 20%;
height: 90%;
background-color: transparent;
position: absolute;
}
#header #header-title #header-text {
display: flex;
top: 5%;
left: 25%;
width: 80%;
height: 90%;
justify-content: center;
flex-direction: column;
background-color: inherit;
white-space: nowrap;
position: absolute;
font-family: 'comfortaabold';
font-size: 120%;
}
#header #header-extras {
top: 5%;
left: 22.65%;
width: 77.25%;
height: 90%;
background-color: transparent;
position: absolute;
}
#header #header-extras .extras-iconlink {
display: inline-block;
top: 10%;
width: 4%;
height: 80%;
background-color: inherit;
position: absolute;
}
#header #header-extras .extras-iconlink .extras-iconlink-text {
display: flex;
top: 0%;
left: 100%;
width: 250%;
height: 100%;
justify-content: center;
flex-direction: column;
background-color: inherit;
white-space: nowrap;
position: absolute;
font-family: 'comfortaabold';
font-size: 120%;
}

我确定我犯的错误非常愚蠢和明显,我并没有真正进行 Web 开发,我主要使用 Lua 进行游戏开发。

非常感谢所有帮助!

最佳答案

您可能不需要使用 JavaScript 来居中。这主要是 CSS 很好地处理的表示问题。我建议您转到这两个链接,看看在每种情况下您可以使用什么,具体取决于您需要居中的内容。读起来不长。

text-align

Centering in CSS: A complete guide

关于javascript - 如何使用 javascript 相对于其父尺寸缩放具有给定类的每个元素的字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58052179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com