86

I use Google Chrome developer tools and I am constantly inspecting one element against another back and forth to find out what may be causing a particular styling issue.

It would be nice to compare the differences in style between element 1 and element 2.

Can this be done with chrome currently or via some workaround? Is there a tool out there that can do what I am looking for?

Current example of style difference is that I have an inline H4 next to a A where the A is appearing higher in vertical alignment. I am not seeking a solution in this question as I will sort it out.

Valamas
  • 24,169
  • 25
  • 107
  • 177

1 Answers1

149

Update: As a result of this discussion, the "CSS Diff" Chrome extension was created.

enter image description here


Great question and cool idea for extension!

Proof of concept

As a proof of concept, we can do a small trick here and avoid creating extension. Chrome keeps elements you select via 'inspect element' button in variables. Last selected element in $0, one before that in $1 etc. Basing on this, I've created a small snippet that compares last two inspected elements:

(function(a,b){    
    var aComputed = getComputedStyle(a);
    var bComputed = getComputedStyle(b);

    console.log('------------------------------------------');
    console.log('You are comparing: ');
    console.log('A:', a);
    console.log('B:', b);
    console.log('------------------------------------------');

    for(var aname in aComputed) {
        var avalue = aComputed[aname];
        var bvalue = bComputed[aname];

        if( aname === 'length' || aname === 'cssText' || typeof avalue === "function" ) {
            continue;
        }
        
        if( avalue !== bvalue ) {
            console.warn('Attribute ' + aname + ' differs: ');
            console.log('A:', avalue);
            console.log('B:', bvalue);
        }
    }

    console.log('------------------------------------------');
})($0,$1);

How to use it?

Inspect two elements you want to compare, one after another, and paste the code above to console.

Result

sample output from provided snippet

Peter
  • 37,042
  • 39
  • 142
  • 198
Konrad Dzwinel
  • 36,825
  • 12
  • 98
  • 105
  • Fantastic. Have posted a follow up question http://stackoverflow.com/questions/12611440/accessing-chromes-last-inspected-element-being-0-and-1-via-js – Valamas Sep 26 '12 at 22:31
  • Since I ended up creating chrome extension for this, my original answer was updated. – Konrad Dzwinel Sep 27 '12 at 09:16
  • Now, if it could be extended to compare styles between two different pages, I'd pay money for it. – Katie Kilian Jun 18 '13 at 14:43
  • Very cool, except I need the same to compare pages between tabs - i.e. dev site to QA site comparision – Geoffrey Hudik Feb 05 '14 at 01:25
  • 1
    @GeoffreyHudik CSS Diff extension now works between the tabs - check it out. – Konrad Dzwinel Feb 05 '14 at 09:06
  • @KonradDzwinel I tried it yesterday and it hasn't been updated since then. Just realized that it's not working for me for the same page even - then again I may be doing it wrong... just quickly read through documentation. – Geoffrey Hudik Feb 05 '14 at 22:49
  • 1
    If you don't mind using Firefox, I added an feature that lets you right click, set "A" node, right click, set "B" node, and compare attributes, css for the element *and its children*: https://addons.mozilla.org/en-US/firefox/addon/devtools-tweaks/ – NoBugs Oct 04 '14 at 05:19
  • @KonradDzwinel where and how i cant find it ? – shareef Nov 08 '17 at 13:47
  • @KonradDzwinel 404 ... are you sure it's still available ? – Dror Dec 11 '19 at 08:09
  • 11
    link is currently dead – wal Dec 16 '19 at 03:51
  • @Dror This is a [css diff chrome extension](https://chrome.google.com/webstore/detail/css-diff/bnkooidmjbobfbgncondmfoajbpafjio?hl=en) no clue whether it is the same one. – Fluous Feb 07 '20 at 10:10
  • 1
    CSS diff extension seems to not be woking - I didn't seen CSS diff tab in devtools :-(. I've used it in the past and was very satisfied. I cannot find it any more. – Zdeněk Mlčoch Aug 26 '20 at 10:43
  • 1
    Manually installed version 0.33.0 from github https://github.com/kdzwinel/CSS-Diff was working. It's on your own risk. – Zdeněk Mlčoch Aug 26 '20 at 10:52
  • 1
    here's a version that prints using console.table instead https://gist.github.com/jakedowns/384d6aa84db9f43c31616a5c88cf3649 – jake downs Feb 05 '21 at 02:27
  • I get a tab in chrome dev tools but its just blank.. – Peter Sep 05 '22 at 11:32
  • @KonradDzwinel Your link goes to the wrong Chrome extension!! Can you please update your answer to point to your [GitHub repo](https://github.com/kdzwinel/CSS-Diff), and perhaps include [instructions to install unpacked extensions](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked) – Ian Feb 21 '23 at 22:26