Compile a problematic CSS file with Twitter Recess - css

I am using recess (https://github.com/twitter/recess) to compress the CSS file
div {
color: red
border: 1px solid red
}
p {
color: blue
}
# recess test.css --compress (No output)
As you can see, there is missing colon in the CSS file so recess failed to output anything, but in a real browser it is perfactly ok if only part of the CSS contains error
e.g.
http://jsfiddle.net/VDQLU/ (see the bule color in p, not affecting by error above)
Q. Are there any way to compress the file anyway with recess? Or using other tool?

The reason recess fails, is because that is invalid css. (You probably know that). And the reason it works anyway on the browser you use, is because browsers assume some web developers just wont use correct syntax and also they want to be compatible with legacy websites, not because the semicolons are optional on css, or because css parsers are supposed to have a quirks mode!.
I would use a sed line to clean the css file, and from now on, use correct css code. Here is an example sed line: (backup your css file first!)
sed -i '/{/,/}/ { /[}{]/ !{ /\/\*.*[^(\*\/\t )][\t ]*$/,/\*\// !{ s/\([^(\*\/);\t ]\)[\t ]*$/\1;/ }}}' yourcssfile.css
This sed line will:
Add ;, only to the lines that end without semicolons; that is, if you do have some lines that end with semicolons, you wont end with two semicolons one after another (like ;;)
Check also for commented lines, be on one line like: /* some comment */, or multiple lines.
That does NOT mean the change is automagically done flawlessly; you would probably have to do some manual fixing (and perhaps manual hacking the sed line), but if your css file looks like your example, it should work almost perfect.

Any sort of CSS compression will remove line breaks from your CSS.
Though a browser may understand your code now, without line breaks to help it process the incorrect code, it would just have a single string.
Any compressor that did compress that code wouldn't be doing you any favours as it almost certainly wouldn't be understood by any browser once the line breaks were removed.

Please use the div style like this.
div {color: red;border:solid red;}
its a good solution for your result.

Related

In effect, suppress wrapping pseudo-elements

My source is xml (not HTML) and I can’t modify it.
This would be easy if CSS had a parent selector.
This XML
<p>Have you heard <quote>I never saw a purple cow</quote>? It is a silly poem.</p>
<p>
It goes like this. <quote><l>I never saw a purple cow.</l>
<l>I never hope to see one.</l>
<l>But I can tell you anyhow,</l>
<l>I’d rather see than be one.</l></quote> Isn’t that silly?
</p>
and this CSS
quote::before {content:"“"}
quote::after {content:"”"}
/* For lines inside quotes */
quote > l {display:block; margin-left:2em}
produce this output
Is there some trickery to produce the same result but without the two quotation marks I’ve circled?
(It would also be OK if the quotation marks were not eliminated but moved, the opening one to the beginning of the first line and the closing one to the end of the last line of the poem.)
https://jsfiddle.net/mccaskey/ujvny0o1/21/

CSS not being applied

I've set a class on the body tag, called container and then in my CSS, I've tried using both body.container and simply .container. and yet whatever happens them image is not shown as the body background.
The browsers show clearly in their inspector that it isn't being applied. Weirdly, in Firefox, it shows a red dot next to the beginning of the rule which suggest there's something wrong and you can delete this dot, which makes the rule work and apply correctly.
I'm actually writing pre-processed SASS which is pre-compiled by Rails and weirdly again, running locally where it's compiled on the fly rather than pre-compiled, it works...
Here's a short snippet from the original SCSS file:
body.container {
background: url('background.jpg');
background-size: auto 110%;
background-repeat: no-repeat;
background-attachment: fixed;
font-family: "Tahoma";
.personalization_box {
position: absolute;
}
...
}
And here's a snippet of the output CSS:
#safari_rememberDiv{width:0px;height:0px;display:none}body.container{background:‌​url("background.jpg");background-size:auto 110%;background-repeat:no-repeat;background-attachment:fixed;font-family:"Tahoma‌​"}body.container .personalization_box{position:absolute;top:5%;width:80%;left:10%;height:90%;bord‌​er-radius:15px;border:2px solid #5B5B5B;background-color:#FFF;overflow:hidden}
I get the following error on CSS validation:
#safari_rememberDiv Lexical error at line 1, column 1010. Encountered: "\ufeff" (65279), after : "" }body.container{background:url("background.jpg");background-size:auto 110%;background-repeat:no-repeat;background-attachment:fixed;font-family:"Tahoma"}
[It's all on one line because it's generated by Rails that way, I can split it into multiple lines for clarity if that helps].
I've resolved the issue and found the cause of the issue.
The two different .scss files pre-processed by SASS within Rails were in different character encodings, which led the pre-compiler to insert a UTF-8 BOM, the red dot, described by \ufeff. By changing the file encoding of this file to ISO-8859-1 (matching the other file) and hence removing some special quote marks later in my CSS (which were inserted using element::before and content:), I was able to resolve the issue.

Visual Studio Standard Style as CSS

I'm searching for the correct style to configure CopySourceAsHtml to change my Selenitic style to default Visual Studio style when I copy and paste it to e.g. an e-mail. Does anybody know what CSS style(s) I need to use to set it up correctly?
When I copy & paste source code an e-mail I get a dark background. If I set it to white it will show all text in light colours which are hard to read. So I need to change the complete style.
How does your plugin expect the CSS to look like? If you open the theme I referred to in the comments above in a text editor you'll see a lot of lines like this:
<Item Name="outlining.collapsehintadornment" Foreground="0x00E8DDD7" Background="0x00FAF7F6" BoldFont="No"/>
Now since I don't know how exactly your plugin is expecting the CSS I will go ahead and give an example of what this might look like in CSS:
.outlining-collapsehintadornment
{
color: #D7DDE8;
background-color: #F6F7FA;
}
Or:
<Item Name="String" Foreground="0x001515A3" Background="0x02000000" BoldFont="No"/>
Becomes
.String
{
color: #A31515;
background-color: #000000;
}
How did I get this?
Visual Studio settings file save color codes as "code hex" values. And they use BGR instead of RGB. This means that you can convert this VS color hexes very simple to HTML color hexes. And since HTML uses RGB don't forget to invert the code. So for example:
0x00E8DDD7. Replace the 0x00 (sometimes this may be 0x02) with a #. And then swap the first 2 and the last 2 characters (BGR to RGB). So you get #E8DDD7 = #D7DDE8. And of course "Foreground" is text-color and "Background" is background-color...If your plugin supports it you might even consider to write font-size: bold if you see BoldFont="Yes" in your XML.
Summarized: Open the VSSettings file I referred to in my comment above in a texteditor. Then convert everything to CSS. Write a simple program to do this, or do it by hand, whatever you prefer. Just remember that this is just an example of what it might look like. I don't know what "CopySourceAsHtml" is expecting your CSS to look like.

YUI Compressor and CSS content?

I have a stylesheet with the following code:
.author-name:before {
content:"by: ";
}
When I run this through YUI Compressor, I get:
.author-name:before{content:"by:"}
This is a problem because it wipes out intended whitespace. I thought using the YUI Compressor special comments, like this:
.author-name:before {
/*!YUI-Compressor */content: "by: ";
}
... might help, but it seems not to. Additionally, the comments themselves got wiped out. My impression based on the was that comments in JavaScript that start with /*! get preserved, but that's not actually happening.
Short of post-processing my build process to unminify, is there a way to do this? We're currently using version 2.3.5 of YUI compressor, and running with the flags --charset utf8 -v -o
So my question, in short, is, is there a way to get YUI Compressor to respect spaces in content: values, or a way to add CSS comments before and after a block I don't want minified?
As a workaround, try using unicode entity instead of space character itself:
.author-name:before {content: "by:\00A0"; }
\00A0 in particular is non-breaking space.
I tried your css using version 2.3.4 and 2.4.6 and both preserve the space inside the string. So it's either a very specific bug (can't find anything in the bug tracker) or something else is going wrong. Are you sure it's a ascii space character and regular apostrophes (not the Windows smart ones or something)?
When I run a test on the same css replacing ascii quotes with unicode character 201C, a left double quotation mark, the space is removed. The reason is quite simple: the parser doesn't recognize it as a string and therefor it strips white space.
You might want to try the latest version from here http://yuilibrary.com/download/yuicompressor/ anyway. In case your current version is installed using some package manager: just extract the .jar file from the archives 'build' folder.
You might add a margin to the content:
.author-name:before {
content: "by:";
margin: 0 .35em 0 0;
}

How can I define colors as variables in CSS?

I’m working on a CSS file that is quite long. I know that the client could ask for changes to the color scheme, and was wondering: is it possible to assign colors to variables, so that I can just change a variable to have the new color applied to all elements that use it?
Please note that I can’t use PHP to dynamically change the CSS file.
CSS supports this natively with CSS Variables.
Example CSS file
:root {
--main-color:#06c;
}
#foo {
color: var(--main-color);
}
For a working example, please see this JSFiddle (the example shows one of the CSS selectors in the fiddle has the color hard coded to blue, the other CSS selector uses CSS variables, both original and current syntax, to set the color to blue).
Manipulating a CSS variable in JavaScript/client side
document.body.style.setProperty('--main-color',"#6c0")
Support is in all the modern browsers
Firefox 31+, Chrome 49+, Safari 9.1+, Microsoft Edge 15+ and Opera 36+ ship with native support for CSS variables.
People keep upvoting my answer, but it's a terrible solution compared to the joy of sass or less, particularly given the number of easy to use gui's for both these days. If you have any sense ignore everything I suggest below.
You could put a comment in the css before each colour in order to serve as a sort of variable, which you can change the value of using find/replace, so...
At the top of the css file
/********************* Colour reference chart****************
*************************** comment ********* colour ********
box background colour bbg #567890
box border colour bb #abcdef
box text colour bt #123456
*/
Later in the CSS file
.contentBox {background: /*bbg*/#567890; border: 2px solid /*bb*/#abcdef; color:/*bt*/#123456}
Then to, for example, change the colour scheme for the box text you do a find/replace on
/*bt*/#123456
Yeeeaaahhh.... you can now use var() function in CSS.....
The good news is you can change it using JavaScript access, which will change globally as well...
But how to declare them...
It's quite simple:
For example, you wanna assign a #ff0000 to a var(), just simply assign it in :root, also pay attention to --:
:root {
--red: #ff0000;
}
html, body {
background-color: var(--red);
}
The good things are the browser support is not bad, also don't need to be compiled to be used in the browser like LESS or SASS...
Also, here is a simple JavaScript script, which changes the red value to blue:
const rootEl = document.querySelector(':root');
root.style.setProperty('--red', 'blue');
CSS itself doesn't use variables. However, you can use another language like SASS to define your styling using variables, and automatically produce CSS files, which you can then put up on the web. Note that you would have to re-run the generator every time you made a change to your CSS, but that isn't so hard.
You can try CSS3 variables:
body {
--fontColor: red;
color: var(--fontColor);
}
There's no easy CSS only solution. You could do this:
Find all instances of background-color and color in your CSS file and create a class name for each unique color.
.top-header { color: #fff; }
.content-text { color: #f00; }
.bg-leftnav { background-color: #fff; }
.bg-column { background-color: #f00; }
Next go through every single page on your site where color was involved and add the appropriate classes for both color and background color.
Last, remove any references of colors in your CSS other than your newly created color classes.
The 'Less' Ruby Gem for CSS looks awesome.
http://lesscss.org/
Yes, in near future (i write this in june 2012) you can define native css variables, without using less/sass etc ! The Webkit engine just implemented first css variable rules, so cutting edge versions of Chrome and Safari are already to work with them. See the Official Webkit (Chrome/Safari) development log with a onsite css browser demo.
Hopefully we can expect widespread browser support of native css variables in the next few months.
Do not use css3 variables due to support.
I would do the following if you want a pure css solution.
Use color classes with semenatic names.
.bg-primary { background: #880000; }
.bg-secondary { background: #008800; }
.bg-accent { background: #F5F5F5; }
Separate the structure from the skin (OOCSS)
/* Instead of */
h1 {
font-size: 2rem;
line-height: 1.5rem;
color: #8000;
}
/* use this */
h1 {
font-size: 2rem;
line-height: 1.5rem;
}
.bg-primary {
background: #880000;
}
/* This will allow you to reuse colors in your design */
Put these inside a separate css file to change as needed.
Sure can, sort of, thanks to the wonderful world of multiple classes, can do this:
.red {color:red}
.blackBack {background-color: black}
but I often end up combining them anyway like this:
.highlight {color:red, background-color: black}
I know the semantic police will be all over you, but it works.
I'm not clear on why you can't use PHP. You could then simply add and use variables as you wish, save the file as a PHP file and link to that .php file as the style sheet instead of the .css file.
It doesn't have to be PHP, but you get what I mean.
When we want programming stuff, why not use a programming language until CSS (maybe) supports things like variables?
Also, check out Nicole Sullivan's Object-oriented CSS.
You can group selectors:
#selector1, #selector2, #selector3 { color: black; }
You could pass the CSS through javascript and replace all instances of COLOUR1 with a certain color (basically regex it) and provide a backup stylesheet incase the end user has JS turned off
dicejs.com (formally cssobjs) is a client-side version of SASS. You can set variables in your CSS (stored in json formatted CSS) and re-use your color variables.
//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();
And here is a link to a complete downloadable demo which is a little more helpful then their documentation : dicejs demo
EDIT: This answer is no longer current. You should use CSS variables now.
Consider using SCSS. It's full compatible with CSS syntax, so a valid CSS file is also a valid SCSS file. This makes migration easy, just change the suffix. It has numerous enhancements, the most useful being variables and nested selectors.
You need to run it through a pre-processor to convert it to CSS before shipping it to the client.
I've been a hardcore CSS developer for many years now, but since forcing myself to do a project in SCSS, I now won't use anything else.
If you have Ruby on your system you can do this:
http://unixgods.org/~tilo/Ruby/Using_Variables_in_CSS_Files_with_Ruby_on_Rails.html
This was made for Rails, but see below for how to modify it to run it stand alone.
You could use this method independently from Rails, by writing a small Ruby wrapper script
which works in conjunction with site_settings.rb and takes your CSS-paths into account, and
which you can call every time you want to re-generate your CSS (e.g. during site startup)
You can run Ruby on pretty much any operating system, so this should be fairly platform independent.
e.g. wrapper: generate_CSS.rb (run this script whenever you need to generate your 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 )
the generate_CSS_files method in site_settings.rb then needs to be modified like this:
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
Not PHP I'm afraid, but Zope and Plone use something similar to SASS called DTML to achieve this. It's incredibly useful in CMS's.
Upfront Systems has a good example of its use in Plone.
If you write the css file as an xsl template, you could read color values from a simple xml file. Then create the css with an xslt processor.
colors.xml:
<?xml version="1.0"?>
<colors>
<background>#ccc</background>
</colors>
styles.xsl:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="iso-8859-1"/>
<xsl:template match="/">body {
background-color: <xsl:value-of select="/colors/background" />;
}
</xsl:template>
</xsl:stylesheet>
Command to render css: xsltproc -o styles.css styles.xsl colors.xml
styles.css:
body {
background-color: #ccc;
}
It’s not possible with CSS alone.
You can do it with JavaScript and LESS using less.js, which will render LESS variables into CSS live, but it’s for development only and adds too much overhead for real-life use.
The closest you can come with CSS is to use an attribute substring selector like this:
[id*="colvar-"] {
color: #f0c69b;
}
and set the ids of all your elements that you want to be adjusted to names starting with colvar-, such as colvar-header. Then when you change the color, all the ID styles are updated. That’s as close as you can get with CSS alone.

Resources