Convert CSS styles to inline styles via JavaScript


Vacisio

I want to add all CSS styles of a specific element to its inline style property. E.g:

I have got:

 <div id="d"></div>

and:

#d { background: #444444; width: 50px; height: 20px; display: inline-block; }

Now I want a JavaScript function to convert the div to:

<div id="d" style="background: #444444; width: 50px; height: 20px; display: inline-block;"></div>

Please help me. And, by the way, I don't want any CSS styles to override any existing inline styles.

Mark Bonelli

You can do the following:

function applyStyle(el) {
    s = getComputedStyle(el);

    for (let key in s) {
        let prop = key.replace(/\-([a-z])/g, v => v[1].toUpperCase());
        el.style[prop] = s[key];
    }
}

let x = document.getElementById('my-id');
applyStyle(x);

xWhere is the element you want to apply the style to .

Basically, this function gets the element's computed style, then copies each property (like padding, background, color, etc.) to the element's inline style.

I don't know why you need to do this, but I think it's a very dirty method and I personally don't recommend it.

Related


Convert CSS styles to inline styles via JavaScript

Vacisio I want to add all CSS styles of a specific element to its inline style property. E.g: I have got: <div id="d"></div> and: #d { background: #444444; width: 50px; height: 20px; display: inline-block; } Now I want a JavaScript function to convert the d

Is there any way to convert block styles/CSS to inline CSS?

Alvaro Joao I'm writing an HTML/CSS email template to trigger for Gmail and other mail. However, Gmail doesn't load <style>the block, so I had to use the 'style' attribute inline to get it to work. To illustrate the problem: <style> .center { text-

Disable inline CSS styles

Ozcan When I inspect the html with IE Developer tools, I found that the button has an inline style: I don't need any attributes for widththis input element . How to disable or override null width? Nikhil Aggarwal If you want to override inline styles, you need

Convert React inline styles to CSS rules

my voice Does a tool exist to convert React inline style descriptions to CSS rules? Input example : { minHeight: 200, position: 'relative', flexGrow: 1, flexShrink: 1, display: 'flex', flexDirection: 'column', } Sample output : { m

Summarize values of inline styles with css styles

Edward I set an element's margins in a React component's .scss file: .element { margin: 20px; } I want to sum this value with the value set to the margin in the inline style: <div className={scss.element} style={{ margin: 10px }}>Whatever</div> The end r

Is there a package to convert inline styles to CSS classes

Abdul If I pass, is there a package <p style="width: 500px; height: 500px"> Hello World </p> will return <p class="foo"> Hello World </p> .foo { width: 500px; height: 500px; } Shubh Sheth Use JS to do this. Get all elements with style tag var els = docu

Disable inline CSS styles

Ozcan When I inspect the html with IE Developer tools, I found that the button has an inline style: I don't need any attributes for widththis input element . How to disable or override null width? Nikhil Aggarwal If you want to override inline styles, you need

Inline CSS styles in ReactJs

Sed Kaiser I'm having trouble setting an inline CSS style in a React App because I want to change the background color of the card based on the data in the array, but I'm not doing so. I'm using a variable with a switch statement and passing it in inline style

Disable inline CSS styles

Ozcan When I inspect the html with IE Developer tools, I found that the button has an inline style: I don't need any attributes for widththis input element . How to disable or override null width? Nikhil Aggarwal If you want to override inline styles, you need

Is there any way to convert block styles/CSS to inline CSS?

Alvaro Joao I'm writing an HTML/CSS email template to trigger for Gmail and other mail. However, Gmail doesn't load <style>the block, so I had to use the 'style' attribute inline to get it to work. To illustrate the problem: <style> .center { text-

Convert React inline styles to CSS rules

my voice Does a tool exist to convert React inline style descriptions to CSS rules? Input example : { minHeight: 200, position: 'relative', flexGrow: 1, flexShrink: 1, display: 'flex', flexDirection: 'column', } Sample output : { m

Override inline CSS styles

Casey I'm using Twitter's type-ahead script to auto-suggest my search form. However, the script generates its own CSS styles, which I can't seem to override. You can see the problem here : http://bahcalculator.org/dev/nano.php . I'm trying to create a copy of

Is there a package to convert inline styles to CSS classes

Abdul If I pass, is there a package <p style="width: 500px; height: 500px"> Hello World </p> will return <p class="foo"> Hello World </p> .foo { width: 500px; height: 500px; } Shubh Sheth Use JS to do this. Get all elements with style tag var els = docu

Convert CSS styles to inline styles via JavaScript

Vacisio I want to add all CSS styles of a specific element to its inline style property. E.g: I have got: <div id="d"></div> and: #d { background: #444444; width: 50px; height: 20px; display: inline-block; } Now I want a JavaScript function to convert the d

Convert CSS styles to inline styles via JavaScript

Vacisio I want to add all CSS styles of a specific element to its inline style property. E.g: I have got: <div id="d"></div> and: #d { background: #444444; width: 50px; height: 20px; display: inline-block; } Now I want a JavaScript function to convert the d

Is there a package to convert inline styles to CSS classes

Abdul If I pass, is there a package <p style="width: 500px; height: 500px"> Hello World </p> will return <p class="foo"> Hello World </p> .foo { width: 500px; height: 500px; } Shubh Sheth Use JS to do this. Get all elements with style tag var els = docu

Is there any way to convert block styles/CSS to inline CSS?

Alvaro Joao I'm writing an HTML/CSS email template to trigger for Gmail and other mail. However, Gmail doesn't load <style>the block, so I had to use the 'style' attribute inline to get it to work. To illustrate the problem: <style> .center { text-

Is there any way to convert block styles/CSS to inline CSS?

Alvaro Joao I'm writing an HTML/CSS email template to trigger for Gmail and other mail. However, Gmail doesn't load <style>the block, so I had to use the 'style' attribute inline to get it to work. To illustrate the problem: <style> .center { text-

Disable inline CSS styles

Ozcan When I inspect the html with IE Developer tools, I found that the button has an inline style: I don't need any attributes for widththis input element . How to disable or override null width? Nikhil Aggarwal If you want to override inline styles, you need

Disable inline CSS styles

Ozcan When I inspect the html with IE Developer tools, I found that the button has an inline style: I don't need any attributes for widththis input element . How to disable or override null width? Nikhil Aggarwal If you want to override inline styles, you need

Convert React inline styles to CSS rules

my voice Does a tool exist to convert React inline style descriptions to CSS rules? Input example : { minHeight: 200, position: 'relative', flexGrow: 1, flexShrink: 1, display: 'flex', flexDirection: 'column', } Sample output : { m

Inline CSS styles in ReactJs

Sed Kaiser I'm having trouble setting inline CSS styles in React App because I want to change the background color of the card based on the data in the array, but I'm not doing so. I'm using a variable with a switch statement and passing it in inline style, bu

Is there a package to convert inline styles to CSS classes

Abdul If I pass, is there a package <p style="width: 500px; height: 500px"> Hello World </p> will return <p class="foo"> Hello World </p> .foo { width: 500px; height: 500px; } Shubh Sheth Use JS to do this. Get all elements with style tag var els = docu

Override inline CSS styles

Casey I'm using Twitter's type-ahead script to auto-suggest my search form. However, the script generates its own CSS styles, which I can't seem to override. You can see the problem here : http://bahcalculator.org/dev/nano.php . I'm trying to create a copy of