JavaScript: style in DOM #kody_az

Dərs haqqında

Hər bir DOM elementinin style adlı xüsusiyyəti (property) var. Bu, həmin element üçün HTML-də, məsələn, style="..." atributunda göstərilə biləcək inline tərzləri (CSS) əks etdirir. JavaScript vasitəsilə bu obyektin xassələrini dəyişib oxuya bilərsiniz.

JavaScript-də CSS xassələrinin adları kebab-case deyil, camelCase formada olur.
background-color (CSS) → backgroundColor (JS)
font-size (CSS) → fontSize (JS)

Bir elementin bütün CSS tərzlərindən (xarici stillər, inline stillər, miras alınan xassələr və s.) təsirlənərək formalaşan yekun (computed) üslubunu əldə etmək üçün window.getComputedStyle(element) istifadə edin.

JavaScript vasitəsilə dinamik olaraq style elementləri yaratmaq və head (və ya hər hansı digər hissəyə) əlavə etmək mümkündür. Bu, xüsusilə istifadəçi tərəfindən yaradılan kontent və ya mövzuların (temaların) dinamik şəkildə dəyişdirilməsi üçün faydalıdır.

Sadəcə style elementini əlavə etməklə kifayətlənməyib, müasir brauzerlər CSSOM (CSS Object Model) vasitəsilə stilləri daha incə səviyyədə idarə etməyə imkan verir. HTML-dəki hər bir style və ya link elementi üçün müvafiq CSSStyleSheet obyekti yaranır ki, onu JavaScript vasitəsilə dəyişdirmək mümkündür.

CSS Custom Properties (tez-tez “CSS dəyişənləri” adlandırılır) JavaScript vasitəsilə dinamik olaraq dəyişdirilə bilər və dəyişikliklər, normal CSS kimi, miras alınır və bütün elementlərə tətbiq olunur. Bu, mövzuların (tema) dəyişdirilməsi və bir neçə elementi eyni vaxtda tərtib etmək üçün çox yararlıdır.

=======

In JavaScript, the term “style” most often refers to how you manipulate the presentation (CSS) of HTML elements using JavaScript code. It can also refer to coding style or best practices, but typically, developers focus on how to dynamically apply, update, or remove CSS styles via JavaScript.

Every DOM element has a style property that reflects its inline styles (the styles you would otherwise set directly in the HTML with the style="..." attribute). You can set and read CSS properties on this style object in JavaScript.

JavaScript uses camelCase property names instead of CSS’s kebab-case. For instance:
background-color in CSS → backgroundColor in JS
font-size in CSS → fontSize in JS

If you want to retrieve the final, computed style of an element (taking into account all CSS declarations: external stylesheets, inline styles, inherited properties, etc.), use window.getComputedStyle(element).

You can create style elements via JavaScript and inject them into the head (or anywhere in the document). This method is useful for dynamic or user-generated content where you need to create styles on the fly.

Beyond simple style insertion, modern browsers provide the CSSOM (CSS Object Model), which lets you manipulate stylesheets at a more granular level. Each style or link element gets a corresponding CSSStyleSheet object that you can access and modify.

CSS Custom Properties (often referred to as “CSS variables”) can be dynamically modified in JavaScript, and the changes cascade like normal CSS. This can be handy for theming or changing styles across multiple elements at once.

#kody_az #kodyaz #js #javascript #nodejs #es6 #viral #viralcode #nodejs #viralvideo #random #code #coder #azerbaycan #azerbaijan #baku #baki #programming #programmer #codes #hazırlıq #hazirliq #kurs #kurslar #course #courses #online #onlinecourses #test #quiz

YouTube-da bax · KODY.AZ