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

Spyglass Dispatch: Comcast's SpinCo Out Hunting • NVIDIA Makes Mint • The Fate of Chrome • Amazon Shows New 'Show' • End of Around the Horn • Writing on the Web

Thursday, November 21, 2024

Comcast's SpinCo Out Hunting • NVIDIA Makes Mint • The Fate of Chrome • Amazon Shows New 'Show' • End of Around the Horn • Writing on the Web The Spyglass Dispatch is a free newsletter sent

Issue 340 - Elon Musk hints at a new model for large families

Thursday, November 21, 2024

View this email in your browser If you are just now finding out about Tesletter, you can subscribe here! If you already know Tesletter and want to support us, check out our Patreon page Issue 340 -

Data Science Weekly - Issue 574

Thursday, November 21, 2024

Curated news, articles and jobs related to Data Science, AI, & Machine Learning ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

Programmer Weekly - Issue 232

Thursday, November 21, 2024

View this email in your browser Programmer Weekly Welcome to issue 232 of Programmer Weekly. Let's get straight to the links this week. Quote of the Week "Writing software is a very intense,

Better - An AI Powered Code Reviewer

Thursday, November 21, 2024

Top Tech Content sent at Noon! How the world collects web data Read this email in your browser How are you, @newsletterest1? 🪐 What's happening in tech today, November 21, 2024? The HackerNoon

Python Weekly - Issue 677

Thursday, November 21, 2024

View this email in your browser Python Weekly Welcome to issue 677 of Python Weekly. Let's get straight to the links this week. From Our Sponsor Get Your Weekly Dose of Programming A weekly

Web Tools #592 - JS Libraries, Git/CLI Tools, Media/SVG

Thursday, November 21, 2024

WEB VERSION Issue #592 • November 21, 2024 Advertisement Deploy AMD Instinct™ MI300X on Vultr AMD Instinct MI300X accelerators are now available on the Vultr cloud platform. With thousands of AMD

Stop Using the Wrong State Management in Jetpack Compose

Thursday, November 21, 2024

View in browser 🔖 Articles Benchmark Insights: Direct State Propagation vs. Lambda-based State in Jetpack Compose Here, we'll dive into some benchmark analysis on the state propagation approach in

wpmail.me issue#694

Thursday, November 21, 2024

wpMail.me wpmail.me issue#694 - The weekly WordPress newsletter. No spam, no nonsense. - November 21, 2024 Is this email not displaying correctly? View it in your browser. News & Articles State of

Turn off Google AI with two letters

Thursday, November 21, 2024

$250 off M4 MacBook; Linux Foundation marks 20 years; Bluesky tips -- ZDNET ZDNET Tech Today - US November 21, 2024 laptop This absurdly simple trick turns off AI in your Google Search results There