55

If I like an element of a site, and I want to implement it into my site, what is the easiest way to do it? Sometimes there is a lot of CSS files, that is hard to follow all of them.

byxor
  • 5,930
  • 4
  • 27
  • 44
János
  • 32,867
  • 38
  • 193
  • 353

9 Answers9

67

UPDATE: As @tank answers below, Chrome version 77 added "Copy Styles" when you right-click on an element in the devtools inspector.


Using Javascript worked best for me. Here's how I did it:

  1. Open Chrome DevTools console.
  2. Paste this dumpCSSText function from this stack overflow answer into the console, and hit Enter:

    function dumpCSSText(element){
      var s = '';
      var o = getComputedStyle(element);
      for(var i = 0; i < o.length; i++){
        s+=o[i] + ':' + o.getPropertyValue(o[i])+';';
      }
      return s;
    }
    
  3. When using Chrome, you can inspect an element and access it in the console with the $0 variable. Chrome also has a copy command, so use this command to copy ALL the css of the inspected element:

    copy(dumpCSSText($0));
    
  4. Paste your CSS wherever you like!

kevnk
  • 18,733
  • 3
  • 28
  • 30
  • 4
    This is a great tip, I love it! I would add that, if you find yourself using this a lot, you can use Dev Tools' little utilized Snippets feature to permanently save it in your browser. You need to run it for every new instance of Dev Tools before using, but it's better than copy and paste. Or, you could make a bookmarklet. Also, I would add a newline "\n" after the property value but that's personal preference. – T Nguyen Jul 02 '18 at 14:49
23

open Firefox, install Firebug right click on the element you want, choose Inspect element and then open the Computed area

you will have ALL STYLES applied to that element

This is valid in Chrome, Safari, Opera and IE with their own development tools

Opera (DragonFly is already installed with Opera)

enter image description here

Firefox (Needs FireBug plugIn)

enter image description here

Internet Explorer (Needs IE Developer Toolbar plugin)

enter image description here

Chrome & Safari (Web Inspector is already part of Chrome and Safari)

enter image description here

balexandre
  • 73,608
  • 45
  • 233
  • 342
19

Chrome 77 now has Copy styles in the Context menu on the Inspect Element tab.

Right click on the Element > Inspect > Right click on the element in the opened Elements tab > Copy > Copy styles

Image example

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
tank
  • 193
  • 1
  • 7
  • I get a lot of stuff starting with `--` like `--fusion-audio-max-width-default: 100%;` and `--header_border_color: #e5e5e5;` Is this broken in Chrome? – HMR Jan 09 '20 at 12:07
  • @HMR no, those are CSS variables. – ZachB May 04 '20 at 19:52
6

Personally i would pick up the css of the entire website since this can affect the element chosen.

1- Go to console and run this function

function copyCSS(){
  let cssStyles = ''

  // Started at index 1 for index 0 is browser's user agent stylesheet.
  for (let i = 1; i < document.styleSheets.length; i++) {
    let style = null

    try {
      if (document.styleSheets[i]) {
        const classes =
          document.styleSheets[i].cssRules || document.styleSheets[i].rules

        if (classes) style = classes
      }
      for (const item in style) {
        if (style[item].cssText != undefined) cssStyles += style[item].cssText
      }
    } catch (e) {
      continue
    }

    
  }

  return cssStyles
}

2- Run in console

copy(copyCSS())

You have in your clipboard all the CSS.

Zenit
  • 429
  • 1
  • 8
  • 20
2

In one word:

Firebug.

Use Firebug to inspect the element, then you can see the cascade. Even better, you can copy and paste right out of FB to a CSS file.

If you want to use other browsers, you can also use their pre-installed developers tools (F12 in IE (requires the IE developers toolbar) right click - inspect element in chrome) or you can use Firebug Lite. :)

Kyle
  • 65,599
  • 28
  • 144
  • 152
1

In chrome/Chromium you can look at computed style. In FF u will need Firebug to see computed style, in Opera use firefly

Kumar
  • 5,038
  • 7
  • 39
  • 51
0

Use something like FireBug or Chrome's developer tools to inspect the DOM and see what styles are applied to the element in question.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

IE8: click F12 --> click "Select element by click" (the white arrow, left most in the icons menu) --> Go back to the web page, click the element you like --> Go back to the Developer Tools page of IE and you'll see the whole style listed to the right.

Others already answered for other browsers. :)

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

this JS function that will get the CSS of a parent div and all its children:

function getCSS(selector) {
  // Get the parent div
  const parentDiv = document.querySelector(selector);
  
  // Get the CSS of the parent div
  const parentCSS = window.getComputedStyle(parentDiv);
  
  // Get the children of the parent div
  const children = parentDiv.querySelectorAll('*');
  
  // Get the CSS of each child and add it to an array
  const childrenCSS = Array.from(children).map(child => {
    return window.getComputedStyle(child);
  });
  
  // Return an object containing the CSS of the parent div and all its children
  return {
    parent: parentCSS,
    children: childrenCSS
  };
}

then to use:

getCSS(".home-platform_component"));
danywigglebutt
  • 238
  • 1
  • 17