Languages
[Edit]
EN

JavaScript - style element in React way with inline style property

3 points
Created by:
Shri
8550

In this short article, we would like to show how in JavaScript style elements in React way - style elements with object.

Style element with object in React way in JavaScript
Style element with object in React way in JavaScript

Practical example:  

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script>

    function styleElement(element, styles) {
      	for (var key in styles) {
        	element.style[key] = styles[key];
        }
    }
    
  </script>
</head>
<body>
  <div id="element">Element body...</div>
  <script>

    var element = document.querySelector('#element');

    styleElement(element, {
      	padding: '5px 10px 20px 30px',
    	border: '5px dashed red',
      	background: 'yellow',
      	color: 'blue'
    });

  </script>
</body>
</html>
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join