Dev Tips: DevTools: Copy CSS as JavaScript 💡

Dev Tips

 

DevTools: Copy CSS as JavaScript

 

Introduction

You can now copy CSS styles as JavaScript, so the styles in your clipboard are compatible with CSS-in-JavaScript libraries.


How to copy CSS as JavaScript

To try this out:

  1. Inspect an element via DevTools.
  2. Right click on the styles within the Styles Pane.
  3. Select Copy all declarations as JS (or copy a single declaration).

Your clipboard now has a string which looks like this (excluding the curly braces):

{
    letterSpacing: '2px',
    textTransform: 'uppercase',
    textDecoration: 'none',
    textAlign: 'center',
    margin: '1em',
    padding: '25px 40px'
}

Notice, DevTools converts the properties to items such as letterSpacing instead of a hyphenated CSS property like letter-spacing.


Bonus: How to assign styles with vanilla JavaScript

As a bonus tip, if you want to start out with a simple vanilla CSS-in-JavaScript approach (assuming you have a solid use-case of course!), you could use Object.assign().

The Object.assign() method can copy an object onto a target object, such as the .style property of a DOM element. Here's the code:

const anchor = document.querySelector('a');

Object.assign(anchor.style, {
    no-color: 'white',
    letterSpacing: '2px'
});


Availability

This feature is currently in Chrome Canary, tested in version 98, and should make its way to regular Chrome soon.


Further Resource

DevTools has lots of interesting 'copy as' features.

  • You can copy all the styles for an element on the page, as plain CSS.
  • You can copy a network request as fetch() and even cURL!
Email Marketing Powered by Mailchimp

Copyright © 2021 Umar, All rights reserved.
You are receiving this email because you signed up for Dev Tips.

unsubscribe from this list    update subscription preferences 

Older messages

Dev Tips: DevTools: Record and playback your user journeys 🎥

Friday, November 5, 2021

Dev Tips DevTools: Record and playback your user journeys 📽 Introduction You can use the new Recorder Panel to record user flows (aka user journeys or user interactions) and play them back. The

Dev Tips: DevTools: Visualise your JavaScript bundles 📊

Friday, October 29, 2021

Dev Tips DevTools: Visualise your JavaScript bundles 📊 Introduction The Lighthouse Treemap shows a visualisation of your JavaScript bundles. It's compatible with sourcemaps and is great for

DevTips: DevTools: Easily change CSS units and values 🔢

Monday, October 18, 2021

Dev Tips DevTools: Easily change CSS units and values 🔢 Hey all, hope you have a great week! Introduction Changing CSS values/units ( 10px / 1rem / 20vh ) can be cumbersome. Canary DevTools has some

Dev Tips: DevTools: Better accessibility inspection with the Source Order Viewer

Friday, July 30, 2021

Dev Tips DevTools: Better accessibility inspection with the Source Order Viewer 🔎 View this online: umaar.com/dev-tips/245-source-order-viewer / tweet Introduction The Source Order Viewer shows you the

DevTips: DevTools: Easily control typography with the CSS Font Editor 🖊

Friday, July 23, 2021

Dev Tips DevTools: Easily control typography with the CSS Font Editor 🖊 Introduction The Font Editor within the Styles Pane helps you to quickly change typography on a web page - using a visual tool

You Might Also Like

Berliner Goes Tabloid 🤬

Thursday, April 18, 2024

The movie that perfectly explains what that NPR editor did. Here's a version for your browser. Hunting for the end of the long tail • April 17, 2024 Berliner Goes Tabloid Considering the tale of

Tuesday Triage #196

Wednesday, April 17, 2024

Your weekly crème de la crème of the Internet is here! The 196th edition featuring Naismith's rule, cow magnet, and Little Sparta. ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

Atlas humanoid robot goes electric 

Wednesday, April 17, 2024

Meta goes back to the drawing board and Tesla does some advertising View this email online in your browser By Christine Hall Wednesday, April 17, 2024 Good afternoon, and welcome to your Hump Day

📞 1990s Technology We Still Use Today — How to Fix Google Maps When It's Not Working

Wednesday, April 17, 2024

Also: Why You Should Buy Last Year's Flagship TV, and More! How-To Geek Logo April 17, 2024 📩 Get expert reviews, the hottest deals, how-to's, breaking news, and more delivered directly to your

Today only: Join Incubator to get these bonuses

Wednesday, April 17, 2024

I'll work with you personally in a small group ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

JSK Daily for Apr 17, 2024

Wednesday, April 17, 2024

JSK Daily for Apr 17, 2024 View this email in your browser A community curated daily e-mail of JavaScript news React useState Vs. Context API: When to Use Them React has improved its state management

Daily Coding Problem: Problem #1415 [Easy]

Wednesday, April 17, 2024

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Stripe. Write a function to flatten a nested dictionary. Namespace the keys with a

⚙️ Workplace AI

Wednesday, April 17, 2024

Plus: Amazon Music just copy pasted Spotify ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

Charted | The World's Biggest Nuclear Energy Producers ⚡

Wednesday, April 17, 2024

China has grown its nuclear capacity over the last decade, now ranking second on the list of top nuclear energy producers. View Online | Subscribe Presented by: FEATURED STORY The World's Biggest

You’re invited: 2024 Security report insights webinar

Wednesday, April 17, 2024

Join us April 25th. ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏