将 css 表从 px 转换为 em 的脚本

发布于 2024-10-10 03:10:15 字数 422 浏览 8 评论 0原文

有人知道可以将样式表从 px 转换为 em 的脚本(php、python、perl、bash 等)吗?

比如,它会输入文件名和基本字体大小(默认 16)并将所有 px 实例转换为 em?

例如:

convertpx2ems --file stylesheet.css --base-font-size 16

会将其转换

button {
    font-size:14px;
    padding:8px 19px 9px;
}

为:

button {
    font-size: .875em;
    padding: .5em 1.188em .563em;
}

...也许,有没有办法用 sass 来做到这一点?

Anyone know of a script (php, python, perl, bash, whatever) that will convert a stylesheet from px to em?

Like, it would take input of the filename and base font-size (default 16) and convert all px instances to em?

eg:

convertpx2ems --file stylesheet.css --base-font-size 16

would convert this:

button {
    font-size:14px;
    padding:8px 19px 9px;
}

to something like this:

button {
    font-size: .875em;
    padding: .5em 1.188em .563em;
}

...maybe, is there a way to do this with sass?

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

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

发布评论

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

评论(8

逆流 2024-10-17 03:10:15

将您的 css 复制/粘贴到 JSFiddle 中进行转换。
代码示例:

var regFindPX = new RegExp('(\\d+)px','g');
var reg3decPoints = new RegExp('(\.\\d{3}).*', '');

var css = 'css code';
var result;
while ((result = regFindPX.exec(css)) !== null) {
    var px = parseInt(result[1]);
    var em = px / 16;
    em = em.toString().replace(reg3decPoints, '$1');
    css = css.replace(px + 'px', em + 'em'); 

}

Copy/Paste your css in JSFiddle to convert.
code example:

var regFindPX = new RegExp('(\\d+)px','g');
var reg3decPoints = new RegExp('(\.\\d{3}).*', '');

var css = 'css code';
var result;
while ((result = regFindPX.exec(css)) !== null) {
    var px = parseInt(result[1]);
    var em = px / 16;
    em = em.toString().replace(reg3decPoints, '$1');
    css = css.replace(px + 'px', em + 'em'); 

}
太阳男子 2024-10-17 03:10:15

你不能像这样明智地互换 px 和 em 大小。 px 大小是一个绝对值,无论它在哪里,都将保持相同的大小。 em 大小是相对于父元素的,因此 0.5em 是父元素大小的一半。如果这没有多大意义,请考虑下面的代码:

/* CSS */
span { font-size: 0.9em; }

<!-- HTML -->
<span>Testing,
    <span>Testing,
        <span>Testing</span>
    </span>
</span>

在本例中,您会发现“Testing”一词变得越来越小,因为每个 span 标记的字体大小都是上面的 90%。

对于 body 标签,1em 的字体大小大致相当于 16px,但这是唯一一次您可以明智地将一种尺寸转换为另一种尺寸的情况

You can't sensibly interchange px and em sizes like that. A px size is an absolute value that will remain the same size regardless of where it is. An em size is relative to the parent element, thus 0.5em is half the size of the parent element. If that doesn't make much sense, consider the following piece of code:

/* CSS */
span { font-size: 0.9em; }

<!-- HTML -->
<span>Testing,
    <span>Testing,
        <span>Testing</span>
    </span>
</span>

In this example you'll find that the word "Testing" gets smaller and smaller because the font size of each span tag is 90% of the one above.

For the body tag, a font-size of 1em is roughly equivalent to 16px, but that is the only time you can sensibly translate one size to another

去了角落 2024-10-17 03:10:15

你可以用 Perl 轻松完成:

perl -p -i -e 's/(\d+)px/($1\/16).em/ge' stylesheet.css

You can do it easily in Perl:

perl -p -i -e 's/(\d+)px/($1\/16).em/ge' stylesheet.css
弥繁 2024-10-17 03:10:15

我在 awk 中敲出了这个:

#!/bin/awk -f

BEGIN {
    bfs = 16;
}

{
    while (match($0, /([0-9]+\.)?[0-9]+px/)) {
        num = substr($0, RSTART, RLENGTH - 2);
        #print("numval: ", numval);
        $0 = substr($0, 1, RSTART-1) (num != 0 ? (1 / bfs) * num: 0 ) "em" substr($0, RSTART + RLENGTH);
  }
}

1

您可以使用它来用合适的转换替换文件中的任何 px 条目..请注意,bfs 必须设置为您希望使用的基本字体大小(在本例中为 16)。另请注意,您必须具备一些 shell 技能才能正确使用它。

I knocked this up in awk:

#!/bin/awk -f

BEGIN {
    bfs = 16;
}

{
    while (match($0, /([0-9]+\.)?[0-9]+px/)) {
        num = substr($0, RSTART, RLENGTH - 2);
        #print("numval: ", numval);
        $0 = substr($0, 1, RSTART-1) (num != 0 ? (1 / bfs) * num: 0 ) "em" substr($0, RSTART + RLENGTH);
  }
}

1

You can use it to replace any px entries in a file with suitable conversions .. note that bfs must be set to the Base Font Size (16 in this case) that you wish to use. Also note, you have to have some shell skills to use this properly.

蹲墙角沉默 2024-10-17 03:10:15

我意识到这是一个旧线程,但我认为这可能对某人有帮助:

将 px 转换为 em:
目标 ÷ 上下文 = 结果

示例:

求 1px & 的值假设容器的字体大小为 16px,则等式如下:

1 ÷ 16 = 0.0625~

(最好四舍五入到最接近的千分之一。)

1px = 0.063em

由此您还可以通过 target * em_value 找到其他测量值= 结果

Ex:

10 * .063 = .63

10px = .63em

我希望这是有道理的 &帮助某人。

I realize this is an old thread, but I thought this might help someone:

Convert px to em:
target ÷ context = result

Ex:

To find the value of 1px & say the font-size for the container is 16px the equation would be as follows:

1 ÷ 16 = 0.0625~

(It's best to round to the nearest thousandth.)

1px = 0.063em

From this you can also find other measurements by target * em_value = result

Ex:

10 * .063 = .63

10px = .63em

I hope this makes sense & helps someone.

Bonjour°[大白 2024-10-17 03:10:15

正如 James 指出的那样,任何直接转换器的问题是样式表不知道嵌套了哪些元素。由于 em 值是相对的,因此计算出的 font-size 在深度嵌套的元素中会有所不同。您需要一个工具来扫描渲染后的 HTML 输出,并递归地调整相关样式。

我编写了一个 JavaScript 组件,在这方面可能会有所帮助。您可以在此处获取它。

建议的工作流程是在浏览器中调用 emify(document.body),然后提取相关的 em 值并将其注入回 CSS 中。

并不理想,但这是准确地做到这一点的一种方法。

As James points out, the issue with any straight-up converter is that the stylesheet is unaware of which elements are nested. Because em values are relative, the computed font-size will be varied in heavily nested elements. You would need a tool that scanned the HTML output after it had been rendered, and recursively adjusted the relevant styles.

I've written a JavaScript component that could prove helpful in this regard. You can grab it here.

Suggested workflow would be to call emify(document.body) in the browser, and then extract the relevant em values and inject it back into your css.

Not ideal, but it's one way of doing it with accuracy.

ㄟ。诗瑗 2024-10-17 03:10:15

从前面的讨论来看,似乎不存在在线生成器,对吧?我确实找不到一个可以解析整个样式表的。

首先,如果 em 应用于图像或输入,除了输入内的字体之外,它们是否是相对的,如果是,是什么以及如何?图像无法嵌套,而且我很难看到它们相对于每个父 div 宽度。 (为了 CSS PIE 进行转换,以避免错误,这就是启发这一点的原因)。

除了输入和图像之外,我想知道是否有办法转换整个样式表。帮我看看我的逻辑是否可行。我想我已经创建了几个元素,我只需要将它们拼凑在一起即可。

单独使用 CSS 并不能解决这个问题,因为不可能知道哪些元素将被嵌套。所以它需要扫描 html 输出。最好的方法是什么?我能想到的唯一方法是应用所有内联样式,这是 生成器< /a> 已用于转换电子邮件等的模板。一旦样式内联,这将显示所有内容是如何嵌套的。由此,与样式表进行比较。

将每个带有“em”的嵌套元素与带有“em”的任何父元素进行比较,以相应地生成 px,并且我已经有一个正则表达式工具来解析 css,使这变得更容易。

一个问题是一个类可以嵌套在不同的元素下,单个类中的 em 在其上下文中变得不同,因此不可能转换为 px 并应用相同的元素。我认为,唯一的解决方案是在 css 表中重新创建嵌套结构或其中的一部分,以在上下文中字体大小不同时相应地应用 px。

例如:

<code>
    // Original Stylesheet
    .mainClass{font-size:1em;}

    // HTML
    <div id="outerNest" style="font-size:.5em;">
        <div class="mainClass"> Font here is .5em </div>
    </div>

    <div class="mainClass> Font here is 1em</div>

        // New stylesheet 
    .mainClass{font-size:16px;}

    // newly created styles to work with px conversion
    #outerNest .mainClass{font-size:8px;}
</code>

A. 你认为这可行吗?
B、有更好的方法吗?
C. 这是否值得?在某些随机情况下,我本可以千载难逢地使用它一次,但我怀疑很多人会发现它有任何价值。或者是否存在人们偶尔会遇到的情况?

From the previous discussion, it seems as though no online generator exists, right? I sure couldn't find one that would parse the entire stylesheet.

First, if em is applied to and image, or input, aside from the font inside an input, would they be relative, and if so, what and how? Images can't be nested, and I have a hard time seeing them being relative to every parent div width. (Conversion for the sake of CSS PIE, to avoid errors, is what is inspiring this).

Aside from just input and images, I was wondering if there ways a way to convert an entire stylesheet. Help me to see if my logic would work. I think I have several of the elements already created, I would just need to tack them together.

Working with the CSS alone can't do the trick, as it's impossible to know which elements will be nested. So it would require scanning the html output. What's the best way to do that? The only way I could think of is to apply all the styles inline, which a generator has been made to convert templates for emails and such. Once styles are inline, this would show how everything is nested. From that, compare to stylesheet.

Compare each nested element with "em" to any parent element with "em" to generate the px accordingly, and I already have a regex tool to parse the css, making this easier.

The one problem is that a class can be nested under different elements, the em in a single class becoming different in it's context, thus impossible to convert to px and be applied the same. The only solution, I believe, is to recreate the nested structure, or a portion of, in the css sheet to apply the px accordingly, anytime the font size differs in context.

For example:

<code>
    // Original Stylesheet
    .mainClass{font-size:1em;}

    // HTML
    <div id="outerNest" style="font-size:.5em;">
        <div class="mainClass"> Font here is .5em </div>
    </div>

    <div class="mainClass> Font here is 1em</div>

        // New stylesheet 
    .mainClass{font-size:16px;}

    // newly created styles to work with px conversion
    #outerNest .mainClass{font-size:8px;}
</code>

A. Do you think that could work?
B. Is there a better way?
C. Is it in any way worth it? I could have used it once in a blue moon in some random situations, but I doubt many people would find it of any value. Or is there a situation in which it would be handy in which people do run into here and there?

梦旅人picnic 2024-10-17 03:10:15

我已经为此任务创建了一个powershell脚本。

它是一个脚本,用于解析 CSS 文件,其 font-size 规则以 px 为单位编写,并将其转换为 font-sizerem 为单位。

欢迎您使用它:

https://github.com/taljacob2/convert-css-fontsize-px-to-rem

I have created a powershell script for this task.

It is a script that parses a CSS file which its font-size rules are written with units of px, and converts them to font-size with rem units.

You are welcome to use it:

https://github.com/taljacob2/convert-css-fontsize-px-to-rem

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