Files
30-seconds-of-code/snippets/js/s/add-styles-to-html-element.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

559 B

title, type, language, tags, author, cover, dateModified
title type language tags author cover dateModified
Add styles to HTML element snippet javascript
browser
chalarangelo digital-nomad-14 2021-01-07T00:37:43+02:00

Adds the provided styles to the given HTML element.

  • Use Object.assign() and HTMLElement.style to merge the provided styles object into the style of the given element.
const addStyles = (el, styles) => Object.assign(el.style, styles);
addStyles(document.getElementById('my-element'), {
  background: 'red',
  color: '#ffff00',
  fontSize: '3rem'
});