DEV Community

ahoNerd
ahoNerd

Posted on • Edited on • Originally published at ahonerd.com

2 1

Add Multiple Inline Style to an Element using javaScript

Untuk menambah beberapa inline style pada sebuah HTML element dengan menggunakan javaScript dapat menggunakan beberapa cara yang akan kita bahas berikut ini.

HTML code

Sebagai contoh pada catatan kali ini kita aka menggunakan HTML element sebegai berikut:

<div id="cobaInlineStyle">
  <span>Coba</span>
  <code>HTMLElement.style</code>
</div>
Enter fullscreen mode Exit fullscreen mode

HTMLElement.style

style adalah read-only property yang mengembalikan inline style dari suatu element dalam bentuk object CSSStyleDeclaration yang berisi daftar dari semua style property untuk element tersebut.

Berikut ini daftar CSS Property yang bisa digunakan.

javaScript code

const elm = document.getElementById('cobaInlineStyle');

elm.style.backgroundColor = 'tomato';
elm.style.color = 'white';
elm.style.lineHeight = '64px';
elm.style.padding = '0 1em';
elm.style.display = 'inline-block';
Enter fullscreen mode Exit fullscreen mode

Result

<div id="cobaInlineStyle" style="background-color: tomato; color: white; line-height: 64px; padding: 0px 1em; display: inline-block;">
  <span>Coba</span>
  <code>HTMLElement.style</code>
</div>
Enter fullscreen mode Exit fullscreen mode

Browser Support

caniuse.com/mdn-api_htmlelement_style

CSSStyleDeclaration.cssText

javaScript code

const elm = document.getElementById('cobaInlineStyle');

elm.style.cssText = 'background-color: tomato; color: white; line-height: 64px; padding: 0px 1em; display: inline-block;';
Enter fullscreen mode Exit fullscreen mode

Result

<div id="cobaInlineStyle" style="background-color: tomato; color: white; line-height: 64px; padding: 0px 1em; display: inline-block;">
  <span>Coba</span>
  <code>HTMLElement.style</code>
</div>
Enter fullscreen mode Exit fullscreen mode

Browser Support

caniuse.com/mdn-api_cssstyledeclaration_csstext

Conclusion

Dapat diperhatikan bahwa kedua metode diatas menghasilkan output yang sama persis, selain itu keduanya memiliki browser support yang baik, maka kedua metode di atas dapat digunakan sesuai degan kebutuhan.

Selain mengunakan metode di atas, masih ada cara lain untuk menambah atau mengatur tampilan dari suatu HTML element, yaitu dengan cara menambahkan class yang ada. Untuk lebih jelasnya bisa dipelajari pada catatan berikut: Add and Remove Multiple Classes to an Element using javaScript

Reference

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay