concept style sheet in category javascript

appears as: style sheet, The style sheet, style sheets
Secrets of the JavaScript Ninja, Second Edition

This is an excerpt from Manning's book Secrets of the JavaScript Ninja, Second Edition.

Listing 12.6. Examining the style property
<style> #A div { font-size: 1.8em; border: 0 solid gold; } </style> <div style="color:#000;" title="Ninja power!"> #B 忍者パワー </div> <script> document.addEventListener("DOMContentLoaded", () => { const div = document.querySelector("div"); assert(div.style.color === 'rgb(0, 0, 0)' || #C div.style.color === '#000', 'color was recorded'); assert(div.style.fontSize === '1.8em', #D 'fontSize was recorded'); assert(div.style.borderWidth === '0', #E 'borderWidth was recorded'); div.style.borderWidth = "4px"; #F assert(div.style.borderWidth === '4px', #G 'borderWidth was replaced'); }); </script> #A - Declares an in-page style sheet that applies font size and border information #B - This test element should receive multiple styles from various places, including its own style attribute and the style sheet. #C - Tests that the inlined color style was recorded #D - Tests that the inherited font size style was recorded #E - Tests that the inherited border width style was recorded #F - Replaces the border width style #G - Tests the border width style change was recorded !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!
Figure 12.3. Computed styles include all styles specified with the element as well as those inherited from style sheets.

At any point in time, the computed style of an element is a combination of all the built-in styles provided by the browser, all the styles applied to it via style sheets, the element’s style attribute, and any manipulations of the style property by script. Figure 12.2 shows how browser developer tools differentiate between styles.

Loading order of style sheets and scripts

Often we’ll expect CSS rules to already be available by the time our code executes. One of the best ways to ensure that CSS rules provided by style sheets are defined when our JavaScript code executes is to include the external style sheets prior to including the external script files.