CSS: Which is faster for the browser? color:#fff; or color:#ffffff;
#fff
is fewer characters, which is generally better. Network latency and bandwidth matter more than processing time in most cases. For this reason, most CSS compressors will intelligently optimize to the #fff
version. The network matters more than parsing speed in this case.
If you're concerned about parsing time, I doubt the difference between the 2 declarations accounts for even 0.005% of total parsing time. There are much bigger bottlenecks that dwarf any speed difference compared to parsing color declarations.
Is there a performance difference between the different css color formats
From what I read in the article I linked below using HEX code is better but not by much we are talking if you have 100,000 colors in your code then it will create 1ms difference between them.
but you can visit this link to get a more meaning full understanding of why is doesn't make that much of a difference
and to see if it really makes a difference run an audit on your website and see the performance difference for each and see which one is better if any.
Link to answer
How to convert #ffffff to #fff or #fff to #ffffff while asserting the background color rgb(255,255,255) returned by Selenium getCssValue(background)
You can use replaceAll
with a regular expression that looks for the case where all three parts use the same digit:
static String getHex(int r, int g, int b) {
return String.format("#%02x%02x%02x", r, g, b).replaceAll("^#([a-fA-F])\\1([a-fA-F])\\2([a-fA-F])\\3$", "#$1$2$3");
}
That looks for a string starting with #
followed by three pairs of matching hex digits, and replaces them with just the short form. (I suppose I could have just used [a-f]
instead of [a-fA-F]
in your specific example, since you know you'll be getting lower case only, but...)
Complete example (on Ideone):
public class Example {
public static void main(String[] args) {
System.out.println(getHex(255, 255, 255)); // #fff
System.out.println(getHex(255, 240, 255)); // #fff0ff
}
static String getHex(int r, int g, int b) {
return String.format("#%02x%02x%02x", r, g, b).replaceAll("^#([a-fA-F])\\1([a-fA-F])\\2([a-fA-F])\\3$", "#$1$2$3");
}
}
which one is faster: hex color codes or color names?
My guess is the codes will be faster since the names are probably assigned to a look-up table and reference hex codes anyway.
That being said, my guess is the speed difference will be completely negligible.
Here's a performance test I just created. They're neck and neck, though hex codes seems to be just barely faster on 4/5 runs so far:
http://jsperf.com/css-color-names-vs-hex-codes
What is the most efficient way to parse a CSS color in JavaScript?
function parseColor(input) {
var m;
Obviously, the numeric values will be easier to parse than names. So we do those first.
m = input.match(/^#([0-9a-f]{3})$/i)[1];
if( m) {
// in three-character format, each value is multiplied by 0x11 to give an
// even scale from 0x00 to 0xff
return [
parseInt(m.charAt(0),16)*0x11,
parseInt(m.charAt(1),16)*0x11,
parseInt(m.charAt(2),16)*0x11
];
}
That's one. Now for the full six-digit format:
m = input.match(/^#([0-9a-f]{6})$/i)[1];
if( m) {
return [
parseInt(m.substr(0,2),16),
parseInt(m.substr(2,2),16),
parseInt(m.substr(4,2),16)
];
}
And now for rgb()
format:
m = input.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
if( m) {
return [m[1],m[2],m[3]];
}
Optionally, you can also add support for rgba
format, and even hsl
/hsla
if you add an HSL2RGB conversion function.
Finally, the named colours.
return ({
"red":[255,0,0],
"yellow":[255,255,0],
// ... and so on. Yes, you have to define ALL the colour codes.
})[input];
And close the function:
}
Actually, I don't know why I bothered writing all that. I just noticed you specified "assuming a major browser", I'm assuming that also means "up-to-date"? If so...
function parseColor(input) {
var div = document.createElement('div'), m;
div.style.color = input;
m = getComputedStyle(div).color.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
if( m) return [m[1],m[2],m[3]];
else throw new Error("Colour "+input+" could not be parsed.");
}
An up-to-date browser will convert any given colour to rgb()
format in its computed style. Just get it back, and read it out.
Google Chrome background-gradient
Have you tried:
background: -webkit-linear-gradient(#917c4d, #ffffff);
WebKit updated to match the spec syntax, so you should have this to get maximum browser support:
background: -webkit-gradient(linear, center top, center bottom, from(#917c4d), to(#ffffff));
background: -webkit-linear-gradient(#917c4d, #ffffff);
background: -moz-linear-gradient(#917c4d, #ffffff);
background: -o-linear-gradient(#917c4d, #ffffff);
background: -ms-linear-gradient(#917c4d, #ffffff);
background: linear-gradient(#917c4d, #ffffff);
Grey Font Color Printing
Solution:
@media print {
h1 {
color: rgba(0, 0, 0, 0);
text-shadow: 0 0 0 #ccc;
}
@media print and (-webkit-min-device-pixel-ratio:0) {
h1 {
color: #ccc;
-webkit-print-color-adjust: exact;
}
}
}
Related Topics
Prevent Box Shadow from Showing on a Specific Side
Set Max Height of Adsense Responsive Ad Unit
When Does a Box Establish an Inline Formatting Context
Sass (Not SCSS) Syntax for CSS3 Keyframe Animation
Less: Better to Use Inheritance or Multiple Classes
Twitter Bootstrap Sideways Caret
Twitter Bootstrap 3 Form-Horizontal and Multiple Input Columns on Single Line
Creating a Circular Menu with CSS
How to Override Bootstrap's Panel Heading Background Color
How to Choose The Last 2 Items in a List with CSS Nth-Child
Why Does Bem Often Use Two Underscores Instead of One for Modifiers
A Way to Correct Background Scaling in Ipad's Safari
What Is The Least Horrible Way to Center an Element with CSS