thingsinjars

  • 16 Sep 2023

    My Books

    Not Geek, Ideas

  • 28 Jan 2011

    CSS Scoped

    Update: Bam! Plugin'd

    One of the things in HTML5 that caught my eye a while back was the new 'scoped' attribute for style tags. The idea of it is that you can include a style element mid-document, mark it as scoped and its declarations will only apply to the style elements parent element and its child elements. The styles won't affect anything outside this. The biggest problem with bringing in this attribute is that it's not backwards compatible. If you include a style block mid-page, its declarations will be applied to ever element whose selector matches, inside or outside scope. It is anti-progressively enhancable. This means that designers and developers can't start using it until there's enough support. What we need is another of those JS tricks to make it usable.

    Possibly the idea of having a style block dropped into the middle of a semantic document strikes you as a distasteful but thinking practically, this could make CMS workflows a bit easier and keep global stylesheets clean of lots of one-off specific styles. For example, this post describing striped CSS backgrounds could have benefited from having the demo directly within the body of the post but, without going into the backend to add an extra id, this could have messed up the layouts for all other posts.

    My first attempt at solving this problem with JS involved generating a unique class, adding it to the parent element and then parsing the style blocks using JSCSSP so that I could rewrite them with the new class to add specificity. This approach only worked for the most basic declarations, unfortunately. The parser worked perfectly but there's a lot of detail in CSS specificity that mean this would be a much larger problem than I thought.

    My second attempt involved:

    1. Allowing the style blocks to affect everything on the page (at which point, the elements in-scope look right, those out-of-scope look wrong)
    2. Using JS to read the current computed styles of each element in-scope and copy them to a temporary array
    3. Emptying out the scoped style element (in-scope look wrong, out-of-scope looks right)
    4. Copying the styles back from the temporary array onto each element

    The first couple of versions only work in webkit, the latest (below) works in mozilla, too.

    This worked great unless you had more than one scoped block – step 1 allowed scoped styles to affect each other.

    The current attempt involves temporarily emptying all other scoped styles before taking the computed styles from a block. I'm now just thinking that this method might not work if you have multiple scoped blocks within the same context. Oh well, there's something to fix in the future.

    This is where I'm at just now.

    Yes, it's a mess, yes the JS is scrappy and yes, it doesn't currently work in IE but I'll get round to that next. It took long enough to get it working in Firefox as there's no simple way to convert a ComputedCSSStyleDeclaration to a string in Mozilla unlike Webkit's implementation of cssText or IE's currentStyle. I might even make it into one of those new-fangled jQuery plugins everyone's using these days.

    Development, CSS, Javascript, Geek

  • 26 Jan 2011

    jQTouch Calendar Extension

    jQTouch Calendar Extension

    I needed to build an offline calendar in jQTouch for a project and found this particularly nice-looking jQTouch iCal project by Bruno Alexandre. Unfortunately, it required a server connection.

    A day later and I've pulled the thing apart, refactored and rebuilt into a shiny jQTouch extension (still using the original project's CSS). It's built for Mobile Safari but still looks good in other webkits.

    View a demo

    Grab the code from the GitHub repository

    iOS, Javascript, Geek, Toys

  • 25 Jan 2011

    Uncooked Composition 3

    Download the file

    I apparently felt in a waltzy mood when I recorded this one

    Not Geek

  • 20 Jan 2011

    Toolkit

    Toolkit

    I'm not entirely sure what the toolkit's for but it's got everything she needs.

    Again, the paper texture came from here. This was basically an excuse to look at pictures of Diana Rigg...

    Design

  • newer posts
  • older posts

Categories

Toys, Guides, Opinion, Geek, Non-geek, Development, Design, CSS, JS, Open-source Ideas, Cartoons, Photos

Shop

Colourful clothes for colourful kids

I'm currently reading

Projects

  • Awsm Street – Kid's clothing
  • Stickture
  • Explanating
  • Open Source Snacks
  • My life in sans-serif
  • My life in monospace
Simon Madine (thingsinjars)

@thingsinjars.com

Hi, I’m Simon Madine and I make music, write books and code.

I’m the Engineering Lead for komment.

© 2025 Simon Madine