检测相关字体大小和包含块宽度
当设计流体布局和可调整大小的文本网页时,您必须始终牢记当前样式元素的相关字体大小(对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用计算出的父级属性值来设置 CSS 中子级的属性。 LESS 或 SASS 不会改变这一点。但大小可以相对于其父级(或使用 rem 单位时的根)进行设置。
相关字体大小为父级设置的字体大小。当您将子元素的字体大小定义为 0.5em 时,其字体大小将比其父元素的字体大小小 50%。
您可以使用 Less / SASS 将 0.5em 声明为变量。这使您能够在一个地方更改和编辑它。
少
百分比单位已经相对于父级(包含块)。
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).
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
The percent units are already relative to the parent (containing block).