Coder Social home page Coder Social logo

dayvonjersen / specificity-graph Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 184 KB

Statistics about your CSS selector specificity.

License: GNU General Public License v2.0

Ruby 54.17% PHP 45.83%
css css-architecture graph specificity css-specificity-graph css-selectors-complexity css-selectors

specificity-graph's Introduction

read this first

http://csswizardry.com/2014/10/the-specificity-graph/

then listen this to this

https://www.youtube.com/watch?v=1OKZOV-iLj4


I'd try to write a paragraph here, but Harry Roberts is really the best source of information on the topic of CSS architecture.

Apparently most people's CSS looks like this:

Y-axis: specificity X-axis: location in CSS file (or the order in which selectors are fed to the parser)

^ Erratic, specificity peaks and valleys, never-ending nightmare of overriding styles

When ideal CSS looks more like this:

^ Gradually increasing specificity, logical smooth sailing.

Specificity vs. position in ruleset is not the only thing that matters in creating a managable CSS architecture, but it's something you can quantitatively measure and control, unlike, say, modularity or extensibility.

Or creativity.

Author's Note

I never bothered to look up how specificity is actually calculated and it turns out it's not completely trivial (unless you just happen to add it to a CSS parser... maybe there's something for postcss already and I just wasted my time...shit.)

There are a few tools available to do this for you already that are well-tested and that you can use with your favorite build tools out of the box just add water plug and play vanilla bean ice cream.

I made this for personal use and curiousity but more for understanding what specificity really means. I gave into div-classitis a while ago and have been content with that for everything, but through understanding one might find a better way.

prereq

  • php
  • ruby
  • gnuplot

run it

php -f specificity.php some.css | ./eplot 2>/dev/null

    See graphs pictured above.

or if you want a PNG named "foo.png"

php -f specificity.php github.css | ./eplot --png

Note: github's actual css as of Fri 14 Aug 2015 03:56:50 PM EDT

*Note: graph may be wrong. *

or just save the data points

And feed them to gnuplot yourself with all your favorite parameters and the 3D views and the pepper brush.

php -f specificity.php graphsgraphsgraphs.com.css > datapoints.dat

gnuplot

gnuplot> plot 'datapoints.dat'

or if you're a hipster nerd

php -f specificity.php some.css --output-json

    [
        {
            "selector": "div.something-like-this:not(.idk):hover::before ~ input[type='tel'].blondie",
            
            "specificity": {
                "base36": "053",   // how its actually calculated in this script
                                   //   !! actual browser results may vary !!
                                        
                "base10": "183",   // number between 0 (for *) and 46656
                                   // !! this number does *not* matter unless 
                                   //    you want a visual *representation* !!
                                         
                "tuple": [0,0,5,3] // human-understandable, most accurate, use this.
                                   // !! first digit is for the style=""
                                   //    html attribute, you know the one
                                   //    which you're never supposed to use !!
                                         
                                   //̂ ͤ͋ͯ̃!ͣ̌! ̋͑ͥt̂͛h̽̐iͤ̀s͗͒̐͊ͦ do̓ͦe̊sͯ͂̔ ͣ͐̔̄not͐̉ͦͮ ͣͣ͗̂ͯp̈ͮ͆̓a͂̉͛ͦ̏͌̚rsͨ͂́ͯe̋ͣ̐ͪͧ̊̎ ̓ͪhͤ̇̓tͤm̔ͨl̋ͦ̄ͬ ̋w͐ͨ̇̈́iͬͭ́̓̈t͛͂̔ͬ͒̀h r̂ͪͦ̔̐e͒͆̑̑͌géͫ̅̚x̍͗̃ !͒̄̃̒!̾ͯ̇̿ͯͬ
            },
            "position": "1",     // position in ruleset, starting from 1
                                 // inb4 muh zero-indexing
                                 
            					 // whitespace agnostic, comma-separated values 
                                 // are given the same numerical position.
                                 
                                 // position will effect styling (bigger number)
                                 // if specificity is the same 
                                 
                                 // also, selectors which directly target elements
                                 // beat out inheritance
                                  
            "important": 0       // JSON-only. According to MDN !important does
                                 // not have to do with specificity, since 
                                 // that is calculated only by selectors and 
                                 // inline-styles
                                 
                                 // but !important does effect individual rules
                                 // so this is a count for how many are found
                                 // within a ruleset for this selector
                                 
                                 // you may have to do additional addition to
                                 // really figure out what the browser will 
                                 // (probably, hopefully?) do
        }//, ...
    ]

disclaimer

I haven't tested it thoroughly yet so use at your own risk or something

credits

http://csswizardry.com/2014/10/the-specificity-graph/

http://www.w3.org/TR/css3-selectors/#specificity


http://stackoverflow.com/a/12868778

http://liris.cnrs.fr/christian.wolf/software/eplot/index.html


http://realworldvalidator.com/css/pseudoelements

https://gist.github.com/afabbro/3759334#gistcomment-716299

https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Mozilla_Extensions

license

GPLv2 (see LICENSE) because of that Christian Wolf guy but you can do whatever you want kid just get out of my face :v

specificity-graph's People

Contributors

dayvonjersen avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.