检测相关字体大小和包含块宽度

发布于 2024-12-10 00:41:30 字数 315 浏览 1 评论 0原文

当设计流体布局和可调整大小的文本网页时,您必须始终牢记当前样式元素的相关字体大小(对于 em 单位)和包含块宽度(百分比单位)。这是一个宽松的时间,必须始终警惕父元素的宽度,或者是否修改了当前元素或其祖先元素之一的字体大小。

使用动态样式表(如 Sass 或 Less)很容易实现 mixin 或函数,这些函数或函数负责处理 em 和百分比计算(简单除法)背后的数学,因此您可以以像素为单位编写大小并忘记翻译,但无论如何您都有提供相关的字体大小和包含的块宽度,因为据我所知,没有办法自动检测它们。

您知道是否有某种实用程序可以使其自动化吗?是否已经做出某种努力以某种方式实现这一目标?

When styling fluids layouts and resizable text web pages, you must have always in mind the relevant font-size (for em's units) and containing block width (percent units) of current styled elements. That's a loose of time to always have to be alert of which is the width of the parent element or if you have modified the font-size for the current element or one of its ancestors.

With Dynamic Stylesheets (like Sass or Less) is easy to implement mixins or functions that take care about the mathematical behind em's and percent calculations (simple divisions), so you can write the size in pixels and forget about the translation, but anyway you have to provide relevant font size and contaning block width as there is no way to auto-decect them, as far as I know.

Do you know if there is some class of utility to automatize that? Does some kind of effort have been done to achieve it in some way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温折酒 2024-12-17 00:41:30

您不能使用计算出的父级属性值来设置 CSS 中子级的属性。 LESS 或 SASS 不会改变这一点。但大小可以相对于其父级(或使用 rem 单位时的根)进行设置。

>No, I'm talking about styling any vertical space (line-height, padding-top, padding->bottom) in em units (so relative to relevant font size)

相关字体大小为父级设置的字体大小。当您将子元素的字体大小定义为 0.5em 时,其字体大小将比其父元素的字体大小小 50%。

您可以使用 Less / SASS 将 0.5em 声明为变量。这使您能够在一个地方更改和编辑它。

@basefont: 1em;
@smallfont: 0.5em;

section {
font-size: @basefont;
article {
font-size: @smallfont; // or @basefont / 2;
}
}

footer {
font-size: @smallfont;
}

以及以百分比为单位的任何水平空间(宽度、左分页、右填充)(因此相对于包含块的宽度)。

百分比单位已经相对于父级(包含块)。

You can't use the calculated property values of the parent to set the properties of child in CSS. LESS or SASS don't change that. But sizes can be set relative to their parent (or the root when using rem units).

>No, I'm talking about styling any vertical space (line-height, padding-top, padding->bottom) in em units (so relative to relevant font size)

The relevant font size is the set font size of the parent. When you define the font size of the child element as 0.5em, its font size will be 50% smaller than the font size of its parent.

You can use Less / SASS to declare for instance the 0.5em as a variable. Which enable you to change and edit it on a single place.

less

@basefont: 1em;
@smallfont: 0.5em;

section {
font-size: @basefont;
article {
font-size: @smallfont; // or @basefont / 2;
}
}

footer {
font-size: @smallfont;
}

and any horizontal space (width, pagging-left, padding-right) in percent units (so relative >to the containing block width).

The percent units are already relative to the parent (containing block).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文