Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I grab all css styles of an element?

Tags:

html

css

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.

like image 673
János Avatar asked Mar 14 '11 09:03

János


People also ask

How do I get all the CSS properties of an element?

The CSS of an element can be obtained using the getComputedStyle element function in JavaScript. It returns a JavaScript object containing CSS properties and their values. This object is indexed and iterable over the property names. The getPropertyValue(property) is used to get the value of a property.

How do I get CSS style of an element?

First, you need to select the element with querySelector . Then, you use getComputedStyle to get the element's styles. If you log style , you should see an object that contains every CSS property and their respective values. You can also see this object in Chrome's and Firefox's dev tools.

How do I copy CSS from Devtools?

Chrome Dev tools Copy CSS Styles Open dev tools. Right-click an element in the Elements panel. Copy > Copy styles. Paste them where needed.

How do you copy an element style?

To copy a style from one element to another:Use the window. getComputedStyle() method to get an object of the element's styles. Assign the specific styles to the other element's style object.


1 Answers

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! 🎉

like image 137
kevnk Avatar answered Oct 13 '22 11:10

kevnk