简化CSS代码
如何简化此代码简化CSS代码,css,Css,如何简化此代码 #user_panel .subscribe, #user_panel .faves, #user_panel .tags, #user_panel .title, #user_panel .calendar a, #user_panel .item .content { color:#fc0; } 我不想一直写“用户”面板。有什么办法吗?少用CSS怎么样?让你的生活轻松多了 #user_panel { .subscribe, .faves, .tags, .
#user_panel .subscribe,
#user_panel .faves,
#user_panel .tags,
#user_panel .title,
#user_panel .calendar a,
#user_panel .item .content
{
color:#fc0;
}
我不想一直写“用户”面板。有什么办法吗?少用CSS怎么样?让你的生活轻松多了
#user_panel {
.subscribe, .faves, .tags, .title, .calendar a, .item .content {
color:#fc0;
}
}
编辑:根据注释,LESS只有在使用Ruby时才有用。这是完全错误的。LESS是用Ruby编写的(不要与仅与兼容的less2css混淆),它附带了一个命令行工具,用于将
*.LESS
文件转换为*.css
文件。我将使用最常见的答案,因为你没有给我一个不这样做的理由:-)
另一种选择是在每个元素上添加一个额外的类,然后在需要时引用多个类,例如:
.highlight
{
color:#fc0;
}
<div id="user_panel">
<span class="subscribe highlight"></span>
<span class="tags highlight"></span>
</div>
。突出显示
{
颜色:#fc0;
}
您可以为这些元素中的每一个赋予第二个类。用户面板控件,并将CSS更改为:
#user_panel .user-panel-control
{
color:#fc0;
}
HTML将如下所示:
<div id="user_panel">
<span class='subscribe user-panel-control'>This is a user panel control</span>
</div>
这是一个用户面板控件
这大概是您将要得到的最短信息。您可以重命名user_面板,但应尽量保持标识符的描述性,以便于维护代码。显而易见的问题是,你的选择器甚至需要“用户”面板部分吗
您可能会:
#user_panel *{ color: #fc0 }
或者按照Jon下面所说的去做。使用一个较短的名字,比如“up”+1作为链接,以前从未听说过,但多年来一直想写类似的东西。还有一个名字叫SASS(启发了LESS的创建者)。Python/Ruby/YAML开发人员喜欢它,但我认为如果你来自CSS,学习起来就不那么容易了——这是一种进化,而不是一场革命。@graphicdevine:我不会说它只有在你使用Ruby时才有用。NET的插件已经少了。它的核心只是一个命令行工具(用Ruby编写),可以用于任何目的(甚至是静态网站)。可能只有在项目具有某些类时,才需要应用这种风格。
#user_panel *{ color: #fc0 }