css - 如何以编程方式在所有现有 CSS 选择器中插入父元素

标签 css css-selectors phpstorm

我有一个包含 1000 多个选择器的大型 CSS 文件。我想将 ID MyId 作为父元素添加到所有选择器。

例如:

p{
...
}
.myclass li{
...
}

将变成:

#MyId p{
...
}
#MyId .myclass li{
...
}

有没有办法自动执行此操作,例如在 PhpStorm 或工具中?

最佳答案

您可以使用带有正则表达式选项的替换。

查找:^(.*)\{$

替换:#MyId $1{

这将匹配所有开始的 CSS 行例如[selector] { 并在其前面加上 #MyId

Regex Replacement

我建议考虑使用 SASS 来更好地控制编译 css 文件。

Note: this will not fix compound selectors such as html, body {, which will only prefix the compound value as #MyID html, body {

关于css - 如何以编程方式在所有现有 CSS 选择器中插入父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50106049/

相关文章:

css - 如何选择*但排除<body>?或者将 <body> 设置为默认样式?

javascript - 无法使用 PhpStorm 打开 *.hbs 文件

css - 将 calc() 与 $variables 一起使用时的意外术语

html - css:更简单的写法:div.markup h1,div.markup h2,div.markup h3{color:#f00;}?

PHPUnit 在 xdebug 处于事件状态并正在监听的情况下运行时卡住

javascript - JavaScript 中的搜索栏样式属性问题

javascript - 增加 png 图像的背景大小而不缩放图像

html - Bootstrap 的 Jumbotron 示例如何在导航栏元素中创建空间

html - 如何使两个内联元素适应主体宽度?

css-selectors - 如何在 Less 中使用 begin with 选择器