如何仅使用 CSS 使字体大小适应可变 div 大小
有类似的问题 具有CSS的div宽度?哪个具有误导性标题,因为它在示例中证明其div大小取决于视口大小,因此他们的解决方案就是简单地使字体大小也取决于视口大小。但是,我的DIV大小在px
中,并且可能随时更改。如何仅通过使用CSS来相应地调整字体尺寸?
不幸的是,%
中字体大小的值对应于母体字体大小,而不是父容器大小。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Example</title>
</head>
<style>
html, body {
margin: 0;
padding: 0;
background-color: red;
}
div#content {
margin: 0;
padding: 0;
background-color: blue;
width: 100px; /* this might change at any time */
font-size: 200%;
}
</style>
<body>
<div id="content">Text</div>
</body>
</html>
There is a similar question
Is it possible to adapt font size to div width with CSS? which has a misleading title because it turns out in their example their div size depends on the viewport size, so their solution is to simply make font size dependent on the viewport size, too. However, my div size is in px
and might change at any time. How do I adjust it's font size accordingly by using CSS only?
Values for font size in %
unfortunately correspond to the parent font size, not to the parent container size.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Example</title>
</head>
<style>
html, body {
margin: 0;
padding: 0;
background-color: red;
}
div#content {
margin: 0;
padding: 0;
background-color: blue;
width: 100px; /* this might change at any time */
font-size: 200%;
}
</style>
<body>
<div id="content">Text</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论