Skip to content

Mustafa Ateş UZUN Blog

mustafauzun.co

  • About
  • Blog
  • Home
  • »
  • JavaScript
  • »
  • Javascript, css in browser console
  • »
    • September 18, 2022September 18, 2022
    • by mustafauzun
  • CSS, JavaScript

Javascript, css in browser console

Did you know that with JavaScript, you can use CSS in the console for the browsers such as Chrome and Firefox.

We use the %c prefix to do this.

For example;

console.log('%chello', 'background: red;')

Output;

For multiple CSS;

console.log('%chello %cworld', 'background: red;', 'color: red;')

Output;

Also, you can do this quickly with the object to css string function developed by me.

(Source: My Github)

const style = ( styles = {} ) => {

    let text = '';

    for(let key in styles) {

        text += key.replace(/([a-z])([A-Z])/g, '$1-$2')
                    .replace(/([A-Z])([A-Z])/g, '$1-$2')
                    .toLowerCase() + ':' + styles[key] + ';';

    }

    return text;

}

Usage;

style({ backgroundColor: 'red', color: 'white' })

Output;

background-color:red;color:white;

Using with console;

console.log('%cHello World', style({ backgroundColor: 'red', color: 'white' }));
Tags: console, log, style
Share

My Udemy Course – What ?F.js: JavaScript’in Bilinmeyenleri

Javascript, Vibrate to the mobile phone from a browser

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Javascript, Task Queue
  • Javascript, Thread Pool
  • Javascript, Lexical Environment
  • Javascript, Execution Context
  • Javascript, Event Delegation

Recent Comments

No comments to show.

Archives

  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • January 2022

Categories

  • C#
  • CMD
  • CSS
  • HTML
  • JavaScript
  • MongoDB
  • Node.js
  • Podcast
  • Powershell
  • Uncategorized
Theme: Puskar by Template Sell.