Files
30-seconds-of-code/snippets/addStyles.md
Angelos Chalaris 8a6b73bd0c Update covers
2023-02-16 22:24:28 +02:00

25 lines
558 B
Markdown

---
title: Add styles to HTML element
tags: browser
author: chalarangelo
cover: digital-nomad-14
firstSeen: 2021-01-07T00:37:43+02:00
lastUpdated: 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.
```js
const addStyles = (el, styles) => Object.assign(el.style, styles);
```
```js
addStyles(document.getElementById('my-element'), {
background: 'red',
color: '#ffff00',
fontSize: '3rem'
});
```