Archive for the ‘Webtechnology’ Category

6 Comments

Unleash the power of SVG sprites

Wednesday, April 4th, 2012

Here at Webonomic we always liked SVG. We experimented with SVG and SVG background quite a few times, also creating some fancy animated backgrounds and other animations at the times Apple hadn’t even released Safari or thought about CSS transforms, transitions, and animations.

Unfortunately SVG has never been really embraced by the majority of browser vendors, only Opera was enthusiastic about it, probably because both Adobe (Flash) and Microsoft (Silverlight/ Vector Markup Language (VML) had their own standards and felt threatened by it. (Adobe doesn’t make browsers, but SVG support in their Creative Suits has never been impressive)  Maybe that’s gonna change now with HTML5 and even Microsoft Explorer 9 support.

Using SVG as images was one thing, using it as a background image was even better.

Setting SVG images by using :target and ID’s

And now we stumbled upon this clever trick showing layered SVG images by setting the hashtag to an ID on a g element and using the CSS :target rule. Simple and elegant.


g { display: none }
g:target { display: inline }

Compare:

yoga

yoga position 1

yoga position 2

Works in Firefox and Opera at the moment. What about Chromium?

Combine with background images

Combine that with a background image and you can use sprites without setting the horrible position values:

Compare this elegant code

div{ background: transparent url(pathto/yoga.svg) no-repeat;}

div.position_1{ background-image: url(pathto/yoga.svg#postion_1); }

div.position_2{ background-image: url(pathto/yoga.svg#position_2);}

with this old code

div{ background: transparent url(pathto/png) no-repeat ;}

div.postion_1{ background-position: 10px 20px}

div.position_2{ background-position: 30px 20px}

Live Demo

Let’s try setting the background image in the following two paragraphs:

The problem of the last example is not only that you have to calculate all the horizontal and vertical offsets, but also the maintenance of the sprite itself.  And scaling issues are much better handled by SVG then of PNG or GIF. I think even a SVG-minifier can be made to concat and minfy several SVG images into one SPRITE – it’s XML in the end – without going back to Inkscape or GIMP, (or Adobe CS  if you prefer to use proprietary software.)
Works in Firefox and … Well, only Firefox at the moment. Great that you can set the SVG-layer from the main document. But why can’t we set the fill or strike color?

We need CSS access

The last thing we wish for is that you can set the CSS from within the main document. I never understood why you couldn’t do it with SVG objects, but it would be even better if you could do something like this (both for SVG images and SVG background images):

.user svg[src=pathto/svg] #position-1{
fill :blue;}

.student svg[src=pathto/svg] #position_2{
fill :red;}

Of course above example isn’t working or correct, – how should we select both images and background images with actual CSS grammar – but making SVG (background) images accessible for  CSS (and JS?) would be awesome. (beware security and cross origin issues)

Targeting SVG Images with CSS


// targeting SVG images
img[src=pathto/svg]

// new CSS syntax for targeting both SVG and SVG background images
svg[url(pathto/svg)]

// I guess setting the URL is better then setting an extra ID or class, after all aren't URL's unique by definition.

Then you can unleash the power of SVG sprites completely, and skin every site and web-app `on the go`!

No Comments

How to manipulate styles with javascript

Thursday, March 15th, 2012

In the DOM the document has a styleSheets property which is a StyleSheetList object, which is a collection of CSSStyleSheet objects which has a cssRules property which is a CSSRuleList object which is a collection of CSSStyleRule objects.

alert(document.styleSheets[0].cssRules[0].cssText)

And the  CSSStyleRule has a cssText property which gives a CSS rule declaration like

body{background-color:white;}
The StyleSheetList and the CSSRuleList are array like objects, but like domNodeList you can’t call foreach methods on it but they are enumable with a loop. Each style tag or link tag to an external  stylesheet is created as a seperate StyleSheetList object. That does make sense, but sometimes one big array of rules can come in hand to. This is the code to achieve that. We use some tricks like calling the splice method to  list to create arrays.
var rules = Array(),superCSSSheet='';

Array.prototype.slice.call(document.styleSheets).forEach(function(sheet){

 rules = rules.concat(Array.prototype.slice.call(sheet.cssRules))

 })

rules.forEach(function(rule){
superCSSSheet += rule.cssText+"\n"
})
alert(superCSSSheet)

Which clearly shows WordPress isn’t very efficient with CSS code: to be more specific most themes and plugin aren’t 🙂

Another funny thing came to attention is that CSS color values are read out differently then set.

1 Comment

Why aren’t CSS colors the same? `in-browser` and `cross-browser`

Thursday, March 15th, 2012

When you set backgroundcolor of the body to hsla(20,30%,50%,.5), what do you expect as output when you read it out with javascript?

The same result in every browser?
Wrong!

The same  in a particular browser ?
Wrong!

The same in a particular browser  indifferent to the color model (rgba or hsla)?
Wrong again! Only Firefox does it right.

Chromium 17: body { background-color: rgba(166, 115, 89, 0.496094); } 

Opera 11.61: body { background-color: rgba(166, 115, 89, 0.5059) } 

Firefox 10.02: body { background-color: rgba(165, 114, 89, 0.5); }

Example

Setting and reading out the color value a 100 times doesn’t change the color in the end, so it seems a result of the javascript floating-point arithmic quirk without any serious implications.

No Comments

Cross-browser CSS transition problems

Tuesday, February 28th, 2012

The great CSS compatibility tables of PPK show good cross browser support for transitions on the width or height property.

Actually the reality is different, there are problems and they are related to height/width changing from percentage/number to another value, from number to percentage and vice versa and most problematic to auto,

You can’t make transitions to height auto, which is IMHO plain stupid. Maybe there are reasons like that the value `auto` is unknown and has to be calculated first. Still, you can read out the property height as a value and  it’s the thing you want as a webdeveloper and it worked in some earlier versions of Chrome if I recall correctly.

Furthermore in the following example the animation renders differently in different browsers.

The latest Firefox (10.0.2) can animate from numbers to percentage. Opera and Chrome can’t, Firefox 9 couldn’t.

No browser can animate to a value auto, which is IMHO we want all.

No Comments

CSS vendor prefix issues

Thursday, February 16th, 2012

Vendor prefixes are the verbose code you see in new `HTML5` sites, in which browser vendors test their new CSS3 rules, when specs are in proposal time or under development.

Something like:


-o-transition: opacity 1s ease-in 1s; //opera
-moz-transition: opacity 1s ease-in 1s; /firefox
-ms-transition: opacity 1s ease-in 1s; // internet explorer
-webkit-transition: opacity 1s ease-in 1s; // safari , chrome
-khtml-transition: opacity 1s ease-in 1s; // konquerer , linux
transition: opacity 1s ease-in 1s; // all when it's official spec

The problem is that most developers seem to do just this:

-webkit-transition: opacity 1s ease-in 1s; // safari , chrome

So they only support webkit browsers and that is plain dumb. At least you should always add the rule without any prefix. That means eventually it will work in every browser that supports it.
Typing all the rules is a pain in the ass, so most developers only do webkit, probably also because Apple invented quite a lot for transitions etc. At that time it was not supported by any other browser, so why on earth should you add code that has no meaning. Well because you care about the future and care about different minded people.

Two helpful solutions

First there are macro’s for IDE’s to type one rule and auto generate the other ones. Here is a CSS vendor prefix macro for Netbeans. Not much of a hassle.

Secondly you can call userJS to the rescue, userJS is like userCSS a rather underestimated part of the internet, but yet a very valuable one. It let you overrule CSS rules or run own JS in websites.

For Opera add this as a userJS:

opera.addEventListener('BeforeCSS', function(userJSEvent){
userJSEvent.cssText = userJSEvent.cssText
.replace(/-(moz|ms|webkit|o)-(border|text-overflow)/g,'$2')
.replace(/-(moz|ms|webkit)-(gradient|transform|transition)/g,'-o-$2');
}, false);

Source here

It will automatically rewrite all -webkit- rules to -o-  rules in Opera, so all the webkit  demo’s will start to work. (if Opera supports them)

Caveats

  • Code (css specs) can change during development.
  • Implementations differ between vendors.

Of course these are workarounds, I still think developers should add all vendor prefixes to their code. It can be automated in an IDE like with mentioned macro for Netbeans or in deployment.

No Comments

Smartphones can be stupid too, and that makes Apple Evil BTW

Wednesday, February 15th, 2012

Why can’t the iPhone / iPad download files?

The standard mobile Safari cannot download all types of files. It can only download files of the type : Word, Excel, Powerpoint and PDF files.

Why on earth does a company design a browser that cannot download files. That is a disgrace, and an insult to the way internet works. It’s getting more smelly, when you realise it can download Office files from one specific vendor Microsoft but not from any other. That isn’t exactly treating people the same, that isn’t exactly improving competition. That’s an insult to the free world. That is limiting competition. That’s anti-competitive behaviour. And it’s simply unfair. We should use open standards in communication, we want to communicate in a way everyone can understand, or at least everyone can learn to understand for free, well that is by investing time, but not money.

Apple has created a phone that can only shop in their own shops, you can’t buy downloads somewhere else. ( iPhone/iPad allows customers to access the iTunes Store to download audio and video files, AR 2010)

(more…)