我正在处理一个相当长的CSS文件。我知道客户可能会要求更改配色方案,并想知道:是否可以将颜色分配给变量,这样我就可以更改一个变量,使新颜色应用于所有使用它的元素?
请注意,我不能使用PHP动态更改CSS文件。
我正在处理一个相当长的CSS文件。我知道客户可能会要求更改配色方案,并想知道:是否可以将颜色分配给变量,这样我就可以更改一个变量,使新颜色应用于所有使用它的元素?
请注意,我不能使用PHP动态更改CSS文件。
当前回答
没有简单的CSS解决方案。你可以这样做:
在CSS文件中找到background-color和color的所有实例,并为每个独特的颜色创建一个类名。 .top-header {color: #fff;} .content-text {color: #f00;} .bg-leftnav {background-color: #fff;} .bg-column {background-color: #f00;} 接下来,浏览网站上涉及到颜色的每个页面,并为颜色和背景色添加适当的类。 最后,删除CSS中除了新创建的颜色类之外的任何颜色引用。
其他回答
你可以试试CSS3变量:
body {
--fontColor: red;
color: var(--fontColor);
}
如果你的系统上有Ruby,你可以这样做:
http://unixgods.org/~tilo/Ruby/Using_Variables_in_CSS_Files_with_Ruby_on_Rails.html
这是为Rails制作的,但请参阅下面如何修改它以独立运行。
通过编写一个小型Ruby包装器脚本,您可以独立于Rails使用这个方法 它与site_settings一起工作。rb,并将css路径考虑在内 你可以在每次你想重新生成CSS的时候调用它(例如在网站启动期间)
您可以在几乎任何操作系统上运行Ruby,因此这应该是相当独立于平台的。
例如,包装器:generate_CSS。rb(在需要生成CSS时运行此脚本)
#/usr/bin/ruby # preferably Ruby 1.9.2 or higher
require './site_settings.rb' # assuming your site_settings file is on the same level
CSS_IN_PATH = File.join( PATH-TO-YOUR-PROJECT, 'css-input-files')
CSS_OUT_PATH = File.join( PATH-TO-YOUR-PROJECT, 'static' , 'stylesheets' )
Site.generate_CSS_files( CSS_IN_PATH , CSS_OUT_PATH )
site_settings中的generate_CSS_files方法。然后Rb需要修改如下:
module Site
# ... see above link for complete contents
# Module Method which generates an OUTPUT CSS file *.css for each INPUT CSS file *.css.in we find in our CSS directory
# replacing any mention of Color Constants , e.g. #SomeColor# , with the corresponding color code defined in Site::Color
#
# We will only generate CSS files if they are deleted or the input file is newer / modified
#
def self.generate_CSS_files(input_path = File.join( Rails.root.to_s , 'public' ,'stylesheets') ,
output_path = File.join( Rails.root.to_s , 'public' ,'stylesheets'))
# assuming all your CSS files live under "./public/stylesheets"
Dir.glob( File.join( input_path, '*.css.in') ).each do |filename_in|
filename_out = File.join( output_path , File.basename( filename_in.sub(/.in$/, '') ))
# if the output CSS file doesn't exist, or the the input CSS file is newer than the output CSS file:
if (! File.exists?(filename_out)) || (File.stat( filename_in ).mtime > File.stat( filename_out ).mtime)
# in this case, we'll need to create the output CSS file fresh:
puts " processing #{filename_in}\n --> generating #{filename_out}"
out_file = File.open( filename_out, 'w' )
File.open( filename_in , 'r' ).each do |line|
if line =~ /^\s*\/\*/ || line =~ /^\s+$/ # ignore empty lines, and lines starting with a comment
out_file.print(line)
next
end
while line =~ /#(\w+)#/ do # substitute all the constants in each line
line.sub!( /#\w+#/ , Site::Color.const_get( $1 ) ) # with the color the constant defines
end
out_file.print(line)
end
out_file.close
end # if ..
end
end # def self.generate_CSS_files
end # module Site
dicejs.com(正式形式为cssobjs)是SASS的客户端版本。您可以在CSS中设置变量(存储在json格式的CSS中),并重用您的颜色变量。
//create the CSS JSON object with variables and styles
var myCSSObjs = {
cssVariables : {
primaryColor:'#FF0000',
padSmall:'5px',
padLarge:'$expr($padSmall * 2)'
}
'body' : {padding:'$padLarge'},
'h1' : {margin:'0', padding:'0 0 $padSmall 0'},
'.pretty' : {padding:'$padSmall', margin:'$padSmall', color:'$primaryColor'}
};
//give your css objects a name and inject them
$.cssObjs('myStyles',myCSSObjs).injectStyles();
这里有一个完整的可下载演示的链接,这比他们的文档更有帮助:dicejs演示
编辑:这个答案不再是最新的。你现在应该使用CSS变量。
考虑使用SCSS。它与CSS语法完全兼容,因此有效的CSS文件也是有效的SCSS文件。这使得迁移很容易,只需要更改后缀。它有许多增强,最有用的是变量和嵌套选择器。
在将其发送到客户端之前,需要通过预处理器将其转换为CSS。
多年来,我一直是一名铁杆CSS开发人员,但自从强迫自己用SCSS做项目以来,我现在不会使用其他任何东西。
恐怕不是PHP,而是Zope和Plone使用类似于SASS的DTML来实现这一点。它在CMS中非常有用。
Upfront Systems在Plone中有一个很好的例子。