<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mike Shine</title>
    <description>The latest articles on DEV Community by Mike Shine (@shinetechnicallywrites).</description>
    <link>https://dev.to/shinetechnicallywrites</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F606080%2F219c3dd3-8e23-4b1b-925a-19f64ac53aa0.jpeg</url>
      <title>DEV Community: Mike Shine</title>
      <link>https://dev.to/shinetechnicallywrites</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shinetechnicallywrites"/>
    <language>en</language>
    <item>
      <title>Vanilla JavaScript Tutorial – Part Two: CSS Styling with JavaScript</title>
      <dc:creator>Mike Shine</dc:creator>
      <pubDate>Tue, 11 May 2021 17:56:22 +0000</pubDate>
      <link>https://dev.to/shinetechnicallywrites/vanilla-javascript-tutorial-part-two-css-styling-with-javascript-5b7h</link>
      <guid>https://dev.to/shinetechnicallywrites/vanilla-javascript-tutorial-part-two-css-styling-with-javascript-5b7h</guid>
      <description>&lt;p&gt;Welcome to part two of the vanilla JavaScript adventure!&lt;/p&gt;

&lt;p&gt;If you missed part one and would like to give it a try, &lt;a href="https://shinetechnicallywrites.netlify.app/posts/vanilla-js-tutorial-part-one/"&gt;click here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/mike-shine/pen/abpJNxN"&gt;Click here&lt;/a&gt; for the starter code to part two, if you didn't complete part one or don't have the code anymore.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why CSS?
&lt;/h2&gt;

&lt;p&gt;HTML and CSS are the peanut butter and jelly of the web development world; they always go hand in hand.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LEFf3rDW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdros5cne3hggi5ly0aq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LEFf3rDW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdros5cne3hggi5ly0aq.jpg" alt="Peanut butter and jelly sandwich" title="I'm worried about that plate falling."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the basis of part one was using JavaScript in lieu of HTML, learning how to use JavaScript in lieu of CSS seems like a logical next step.&lt;/p&gt;

&lt;p&gt;If you know basic HTML and basic JavaScript, chances are you have come in contact with CSS as well. CSS (short for Cascading Style Sheets) essentially controls the stylistic/visual component of the elements on the webpage: colors, fonts, text decoration, animations (sometimes), and more. If you need a more thorough explanation of CSS &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/What_is_CSS"&gt;check out this MDN article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's continue where we left off with our part one code, and take a look at how to use CSS within JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using CSS in your JavaScript file
&lt;/h2&gt;

&lt;p&gt;As explained in previous articles, having separate files for HTML and CSS as well as many different files for different JavaScript components of your project is standard practice, and for good reason. Adhering to the principle of &lt;a href="https://en.wikipedia.org/wiki/Separation_of_concerns"&gt;separation of concerns&lt;/a&gt; allows for clearer organization of code, as well as reducing the chances of breaking changes occurring in the whole project because of a single faulty edit in one module. As in part one, we will still be using our &lt;code&gt;index.js&lt;/code&gt; file for all of our CSS, to immerse fully into the JavaScript experience. However, please remember that 99% of projects you work on will not follow this practice, we are only doing it for the sake of learning how!&lt;/p&gt;

&lt;p&gt;The general principle of styling in JavaScript is actually quite simple. The majority of this part of the tutorial will just be learning the most essential style features that you will probably need.&lt;/p&gt;

&lt;p&gt;The basic pattern of styling in JavaScript is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;element to be styled&amp;gt;.style.&amp;lt;style property&amp;gt; = "desired outcome";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That wasn't very well-explained. Let me give you a more concrete example to demonstrate; the next line of code will result in my &lt;code&gt;header&lt;/code&gt; turning green:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;header.style.color = "green";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kkPilQaN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t914tb0uhxly1esk4oqa.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kkPilQaN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t914tb0uhxly1esk4oqa.jpg" alt="image of the example code, with header green colored"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That looks better. In that example, &lt;code&gt;header&lt;/code&gt; is the element being acted upon, &lt;code&gt;style&lt;/code&gt; is usually the keyword for accessing style properties, &lt;code&gt;color&lt;/code&gt; is the style property that is being accessed, and &lt;code&gt;green&lt;/code&gt; is the new desired color.&lt;/p&gt;

&lt;p&gt;With our basic pattern of styling elements in mind, let's segue into our first CSS essential: color.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J2Tsos-b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywig2k5lq6fq022prun7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J2Tsos-b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywig2k5lq6fq022prun7.jpg" alt="Very colorful arches"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Essential #1: Color
&lt;/h2&gt;

&lt;p&gt;Color is one of the most important style elements in visual design. As such, there is substantial functionality built into CSS to support color; however, covering every aspect of using color in your styling could be an entire tutorial series by itself, so we will only be exploring a few basics.&lt;/p&gt;

&lt;p&gt;Let's start by taking the example from the previous section and applying it here: change the color of your &lt;code&gt;header&lt;/code&gt;. Use my code above, but instead of &lt;code&gt;green&lt;/code&gt; you can select any basic color you like. Think color wheel/kindergarten colors, not the wackiest color you remember from the crayon box you used to have. For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;header.style.color = "orange";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Your display should now look something like this, depending on what color you chose:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O5sfm_q1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8nhthdqp3n2358en58tq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O5sfm_q1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8nhthdqp3n2358en58tq.jpg" alt="example code with orange header"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nicely done. The &lt;code&gt;color&lt;/code&gt; property can be applied to text, backgrounds, borders, and pretty much any element on your webpage that you can think of.&lt;/p&gt;

&lt;p&gt;Before we move on, go ahead and apply that same color you chose to your &lt;code&gt;text1&lt;/code&gt; and &lt;code&gt;text2&lt;/code&gt; elements.&lt;/p&gt;

&lt;p&gt;Cool, now let's think about background color. White background is cool and all, but let's decide on something a little more exciting.&lt;/p&gt;

&lt;p&gt;Use this code to modify your background color (you can substitute my color for a color of your choosing), then read on for an explanation:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;document.body.style.backgroundColor = "red";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Good! Although this might seem quite different from changing the color of our header, it still follows our same basic pattern:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;element to be styled&amp;gt;.style.&amp;lt;style property&amp;gt; = "desired outcome";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For our background color styling, &lt;code&gt;document.body&lt;/code&gt; is the element to be styled, the &lt;code&gt;style&lt;/code&gt; keyword is still used, &lt;code&gt;backgroundColor&lt;/code&gt; is the style attribute we are modifying, and &lt;code&gt;red&lt;/code&gt; is my desired outcome.&lt;/p&gt;

&lt;p&gt;One important distinction between CSS and JavaScript styling becomes relevant when modifying styling attributes that are more than one word (such as background color). In CSS, the syntax for such attributes is kebab-case (i.e. &lt;code&gt;background-color&lt;/code&gt;). For JavaScript, the syntax is camelCase (i.e. &lt;code&gt;backgroundColor&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Now then, with my choice of orange text and red background, my example looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1IpUUKYS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7oymvqs24y6lhpu68yle.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1IpUUKYS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7oymvqs24y6lhpu68yle.jpg" alt="red background and orange text, difficult to read text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bleh. That is not an attractive color combination, but more importantly it is quite difficult to read, even for users with standard vision. For users with vision impairments, this would be extremely difficult if not impossible to read. Always keep &lt;a href="https://www.w3.org/WAI/standards-guidelines/wcag/"&gt;web accessibility standards&lt;/a&gt; in mind when selecting color combinations! There are several tools (&lt;a href="http://colorsafe.co/"&gt;like this one&lt;/a&gt;) that help web developers/designers select color combinations that are appropriate for users of all vision levels.&lt;/p&gt;

&lt;p&gt;With that in mind, I'm going to change my background color to something that will contrast the bright orange a little better.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;document.body.style.backgroundColor = "black";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T5cOBJ8q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/972nas0e3oho16nrvqgm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T5cOBJ8q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/972nas0e3oho16nrvqgm.jpg" alt="orange text on black background"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Much better! We can use more colors than just the primary ones, however. To be precise, we have &lt;a href="https://techterms.com/definition/rgb"&gt;16,777,216 color options&lt;/a&gt; (although some may argue that because of transparency options, &lt;a href="https://stackoverflow.com/questions/22285636/total-number-of-possible-color-combinations/53963230"&gt;we actually have a great deal more than that&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;There are three ways to specify color in web design:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://encycolorpedia.com/html"&gt;Keyword&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/RGB_color_model"&gt;RGB value&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Web_colors#Hex_triplet"&gt;Hex code&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the interest of brevity, click on any of the three items above to learn more. The "Keyword" link leads to a list of all keywords that can be used in your color styling. The "RGB value" and "Hex code" links lead to respective explanations of the concepts, if you are unfamiliar.&lt;/p&gt;

&lt;p&gt;Here are a couple examples of how each approach would look syntactically.&lt;/p&gt;

&lt;p&gt;Keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cornflowerblue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chartreuse&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lightsalmon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dimgray&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;RGB:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rgb(100, 149, 237)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rgb(127, 255, 0)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rgb(255, 160, 122)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rgb(105, 105, 105)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hex code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#6495ed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#7fff00&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#ffa07a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#696969&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These three examples are visually identical, and should all display like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oho65qv0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0zpwfez27ttybl1jo01.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oho65qv0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0zpwfez27ttybl1jo01.jpg" alt="same header and text with different colors from before" title="Mature readers won't find the hex code for dimgray funny at all."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This wraps up our color section, but you will continue to get practice with it along with other style elements.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Essential #2: Backgrounds
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;background-color&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In the previous section, we learned that we can modify the background color of the page with &lt;code&gt;document.body.style.backgroundColor = "color_goes_here";&lt;/code&gt;. There is another important tidbit to know about the background color property, which is that it can be applied to individual elements as well. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Vanilla JS practice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Go hang a salami, I&lt;/span&gt;&lt;span class="se"&gt;\'&lt;/span&gt;&lt;span class="s1"&gt;m a lasagna hog.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Read the previous sentence backwards.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XL6SnVYW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21isy51vhj6uvnkkeylx.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XL6SnVYW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21isy51vhj6uvnkkeylx.jpg" alt="three lines of text, each with a different background color"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;background-image&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In addition to a solid color, we can use images as backgrounds as well. Say, for example, we wanted to use this yummy-looking lasagna as a background image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g-zZSZtz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/TUQLNdi.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g-zZSZtz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/TUQLNdi.jpg" alt="lasagna" title="why oh why am I writing this section right before dinner"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use this image as our background one of two ways.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Image file saved to local filesystem
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;url("lasagna.jpg")&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Image file is online
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;url("https://imgur.com/TUQLNdi.jpg")&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9jiL4cwb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qc9wysjgyua3777j9t50.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9jiL4cwb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qc9wysjgyua3777j9t50.jpg" alt="lasagna"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;background-repeat&lt;/code&gt; and &lt;code&gt;background-size&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Oftentimes our background image does not perfectly match the size of the display. Therefore, we often have to stretch or repeat the image to make the background look complete.&lt;/p&gt;

&lt;p&gt;The two main style properties that affect this are &lt;code&gt;background-repeat&lt;/code&gt; and &lt;code&gt;background-size&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;background-repeat&lt;/code&gt; has four options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;no-repeat&lt;/code&gt; (image renders one time, default location is top left corner)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;url('https://biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundRepeat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;no-repeat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--khfh64Qy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/moffjjqxuulfxx1wkev9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--khfh64Qy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/moffjjqxuulfxx1wkev9.jpg" alt="no repeat" title="no-repeat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;repeat-x&lt;/code&gt; (image repeats horizontally across the screen)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;url('https://biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundRepeat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;repeat-x&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oIDgSaDq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uqtczjh0glteej88g03q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oIDgSaDq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uqtczjh0glteej88g03q.jpg" alt="repeat-x" title="repeat-x"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;repeat-y&lt;/code&gt; (image repeats vertically across the screen)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;url('https://biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundRepeat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;repeat-y&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F9TgJL-i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkoji0flf29olremfw4a.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F9TgJL-i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkoji0flf29olremfw4a.jpg" alt="repeat-y" title="repeat-y"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;repeat&lt;/code&gt; (default behavior; image repeats horizontally and vertically to fill the entire screen)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;url('https://biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundRepeat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;repeat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XFCrj2TZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k897h3qqmzmt30ajy5d2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XFCrj2TZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k897h3qqmzmt30ajy5d2.jpg" alt="repeat" title="repeat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;background-size&lt;/code&gt; has two main options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;auto&lt;/code&gt; (default behavior; image renders at its original size)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;url('https://biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundRepeat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;no-repeat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;auto&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w5mI_3SD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnm9wpcb41h0fv3qcslp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w5mI_3SD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnm9wpcb41h0fv3qcslp.jpg" alt="auto" title="auto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;cover&lt;/code&gt; (a single image stretches to cover the entire screen)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;url('https://biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundRepeat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;no-repeat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cover&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H0VW6PS2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3t4w4udmc3w70ysmy1d.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H0VW6PS2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3t4w4udmc3w70ysmy1d.jpg" alt="cover"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Essential #3: Text emphasis
&lt;/h2&gt;

&lt;p&gt;The main types of text/typographical emphasis include &lt;span&gt;bold&lt;/span&gt;, &lt;span&gt;italic&lt;/span&gt;, and &lt;span&gt;underline&lt;/span&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bold text
&lt;/h3&gt;

&lt;p&gt;To make text &lt;span&gt;bold&lt;/span&gt;, set the &lt;code&gt;font-weight&lt;/code&gt; property to the value "bold".&lt;/p&gt;

&lt;p&gt;&lt;code&gt;text1.style.fontWeight = "bold";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3sTZ0hhu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/auinq1evjd215fgkcgpr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3sTZ0hhu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/auinq1evjd215fgkcgpr.jpg" alt="bold text" title="bold text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Italic text
&lt;/h3&gt;

&lt;p&gt;To make text &lt;span&gt;italic&lt;/span&gt;, set the &lt;code&gt;font-style&lt;/code&gt; property to the value "italic".&lt;/p&gt;

&lt;p&gt;&lt;code&gt;text2.style.fontStyle = "italic";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QpkVKCci--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1bbu8zmz4t6z0bt95w3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QpkVKCci--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1bbu8zmz4t6z0bt95w3.jpg" alt="italic text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Underlined text
&lt;/h3&gt;

&lt;p&gt;To make text &lt;span&gt;underlined&lt;/span&gt;, set the &lt;code&gt;text-decoration&lt;/code&gt; property to the value "underline".&lt;/p&gt;

&lt;p&gt;&lt;code&gt;header.style.textDecoration = "underline";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zOFYCEMW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6huwcmy5kdsco72tvoz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zOFYCEMW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6huwcmy5kdsco72tvoz.jpg" alt="underlined text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In addition to &lt;span&gt;underlined&lt;/span&gt; text, the &lt;code&gt;text-decoration&lt;/code&gt; property can also &lt;span&gt;overline&lt;/span&gt; and &lt;span&gt;line-through/strikethough&lt;/span&gt; text.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Vanilla JS practice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textDecoration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;underline&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Go hang a salami, I&lt;/span&gt;&lt;span class="se"&gt;\'&lt;/span&gt;&lt;span class="s1"&gt;m a lasagna hog.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fontWeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bold&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textDecoration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;overline&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Read the previous sentence backwards.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fontStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;italic&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textDecoration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;line-through&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dRBYOfU5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vssdfffyrmoeealybnnq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dRBYOfU5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vssdfffyrmoeealybnnq.jpg" alt="all text emphasis styles"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Essential #4: Borders
&lt;/h2&gt;

&lt;p&gt;Borders have several properties, which can be set and modified individually. However, in my experience most developers use the shorthand &lt;code&gt;border&lt;/code&gt; property, which combines the &lt;code&gt;border-width&lt;/code&gt;, &lt;code&gt;border-style&lt;/code&gt;, and &lt;code&gt;border-color&lt;/code&gt; properties. Instead of writing three lines of code...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;borderWidth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;borderStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dotted&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;borderColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;salmon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...one could write one line of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;border&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5px dotted salmon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MQByRUF9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eji9c8muvw7xyff9bdke.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MQByRUF9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eji9c8muvw7xyff9bdke.jpg" alt="header with border"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When using the &lt;code&gt;border&lt;/code&gt; property, the only parameter that is required is &lt;code&gt;border-style&lt;/code&gt;. &lt;code&gt;border-width&lt;/code&gt; and &lt;code&gt;border-color&lt;/code&gt; are optional, and default to 3px and black respectively.&lt;/p&gt;

&lt;p&gt;As with color, borders are big topic with lots of different options for customization and specification. Rather than explain and show all the options in this tutorial, &lt;a href="https://www.w3schools.com/css/css_border.asp"&gt;click here if you wish to learn more about CSS border properties and options&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it yourself – Task
&lt;/h2&gt;

&lt;p&gt;Congrats on getting this far! To try to synthesize the information presented in this tutorial, try this task on your own. Of course, if you need to refer back to earlier parts of this article for information, you are encouraged to do so!&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Your task&lt;/span&gt;: &lt;span&gt;Make a square on the screen, below your text.&lt;/span&gt; This is very open-ended, and there are nearly limitless ways to do this.&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Extra Credit&lt;/span&gt;: If you complete this task, try to complete it one or two MORE times using different approaches.&lt;/p&gt;

&lt;p&gt;Before you get started, you should do some independent Google research on CSS &lt;code&gt;height&lt;/code&gt; and &lt;code&gt;width&lt;/code&gt; properties. I didn't cover those properties in this tutorial, but you may need them now.&lt;/p&gt;

&lt;p&gt;If you get stuck, scroll down for a few hints, and down to the next section for a couple sample answers. Good luck!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/12XDYvMJNcmLgQ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/12XDYvMJNcmLgQ/giphy.gif" alt="Patrick Star rooting for you"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Hint #1&lt;/span&gt;: The first step to completing this task is to create a new element, likely a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;. From there, you can use a combination of the style properties you learned in this tutorial to make a square.&lt;/p&gt;

&lt;p&gt;.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Hint #2&lt;/span&gt;: Have you looked up the &lt;code&gt;height&lt;/code&gt; and &lt;code&gt;width&lt;/code&gt; properties yet? You may need to set those properties on your new element to give it a defined area.&lt;/p&gt;

&lt;p&gt;.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Hint #3&lt;/span&gt;: If you can't get a square to appear on the screen, re-check the basic stuff:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Have you used &lt;code&gt;document.body.appendChild(YOUR_SQUARE_HERE)&lt;/code&gt;?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Have you given your square some sort of style feature that makes it visible? Double check your syntax for every line.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it yourself – Example solutions
&lt;/h2&gt;

&lt;p&gt;I hope you were able to make a square! If you were not, or if you want to see some more ways to do it, check out some example solutions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using the &lt;code&gt;border&lt;/code&gt; property:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newSquare&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;border&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;solid&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;200px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;200px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---Yfyufke--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wg2wuz0ymlzf6owbw8n7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---Yfyufke--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wg2wuz0ymlzf6owbw8n7.jpg" alt="square made with border property" title="square made with border property"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;ol&gt;
&lt;li&gt;Using the &lt;code&gt;background-color&lt;/code&gt; property:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newSquare&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;200px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;200px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LCU9SDZW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfuwqcrundnothvys2bt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LCU9SDZW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfuwqcrundnothvys2bt.jpg" alt="square made with background color property" title="square made with background color property"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using the &lt;code&gt;background-image&lt;/code&gt; property:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newSquare&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;url('purple-square.png')&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;200px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;200px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newSquare&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3yrEq3aT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3107i9bghdu20hz263fp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3yrEq3aT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3107i9bghdu20hz263fp.jpg" alt="square made with background image property" title="square made with background image property"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That was but a &lt;span&gt;brief&lt;/span&gt; introduction to all that CSS has to offer, and how to implement it in JavaScript. I hope you learned a lot that you can apply in your own projects! Be sure to check out the third part of this tutorial, which will (I think) cover modals in JavaScript.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Attributions&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Peanut butter and jelly image by Freddy G, linked from Unsplash&lt;/li&gt;
&lt;li&gt;Colorful festival image by Robert Katzki, linked from Unsplash&lt;/li&gt;
&lt;li&gt;Lasagna image by sunorwind, linked from Unsplash&lt;/li&gt;
&lt;li&gt;Horseback rider image by Mikayla Storms, linked from Unsplash&lt;/li&gt;
&lt;li&gt;Patrick Star gif is the property of ViacomCBS, Inc. Linked from GIPHY&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>In Defense of Light Mode</title>
      <dc:creator>Mike Shine</dc:creator>
      <pubDate>Sat, 17 Apr 2021 22:27:47 +0000</pubDate>
      <link>https://dev.to/shinetechnicallywrites/in-defense-of-light-mode-5gnc</link>
      <guid>https://dev.to/shinetechnicallywrites/in-defense-of-light-mode-5gnc</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2lbcRiKl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/bzOZlZV.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2lbcRiKl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/bzOZlZV.jpg" alt="man holding red lightsaber in cave"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Chances are, the words you are reading right now are white, and the background behind them is black.&lt;/p&gt;

&lt;p&gt;It's not like you don't have a choice in the matter. There are all manner of tools that allow you to view webpages in dark mode as a default (like &lt;a href="https://chrome.google.com/webstore/detail/dark-reader/eimadpbcbfnmbkopoojfekhnkhdbieeh?hl=en-US"&gt;this one&lt;/a&gt;). If you are reading this article on &lt;a href="https://shinetechnicallywrites.netlify.app/"&gt;my website&lt;/a&gt;, the sun/moon icon up in the top navigation bar allows you to toggle between light and dark mode with a single click. You probably just switched over to light mode out of curiosity, and then immediately switched it back to dark mode.&lt;/p&gt;

&lt;p&gt;How am I doing so far? Am I way off base? Or am I so uncannily on point that you're nervously glancing over your shoulder?&lt;/p&gt;

&lt;h2&gt;
  
  
  Survey says...
&lt;/h2&gt;

&lt;p&gt;Predicting that you prefer dark mode over light mode is hardly much of a party trick, although I'm happy to take credit for any amazement you might have felt. An overwhelming majority of survey respondents expressed a preference for dark mode over light mode. &lt;a href="https://www.androidauthority.com/author/jimmywestenberg/"&gt;Jimmy Westenberg&lt;/a&gt; with &lt;a href="https://www.androidauthority.com/"&gt;&lt;em&gt;Android Authority&lt;/em&gt;&lt;/a&gt; reported that of the 2,514 &lt;em&gt;Android Authority&lt;/em&gt; users who responded to a survey, &lt;a href="https://www.androidauthority.com/dark-mode-poll-results-1090716/#:~:text=Results,dark%20mode%20on%20their%20smartphones."&gt;over 80% use dark mode on their phones, apps, and wherever it is available&lt;/a&gt;. Developer &lt;a href="https://medium.com/@tomayac"&gt;Thomas Steiner&lt;/a&gt; conducted an informal survey of contrast polarity preference through Twitter, and the results &lt;a href="https://medium.com/dev-channel/let-there-be-darkness-maybe-9facd9c3023d"&gt;are very similar&lt;/a&gt; to the &lt;em&gt;Android Authority&lt;/em&gt; poll referenced above.&lt;/p&gt;

&lt;p&gt;According to these results and others, the consensus seems to be a preference for dark mode, although one could argue that respondents of an internet survey &lt;a href="https://meetingst.com/what-are-the-pros-and-cons-of-online-survey-research/"&gt;tend to skew younger, wealthier, and more educated than a general sample of the population&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oAugG3Fa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/x9x6aeZ.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oAugG3Fa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/x9x6aeZ.jpg" alt="Darth Vader"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So dark mode wins, right?
&lt;/h2&gt;

&lt;p&gt;Not so fast. One of the oft-cited reasons for preferring dark mode over light mode is that dark mode is easier on your eyes. However, even a &lt;a href="https://www.google.com/search?q=is+dark+mode+easier+on+your+eyes&amp;amp;sxsrf=ALeKk009268I_d4s0_wPMG2pIN3JDzRsbg%3A1617812576324&amp;amp;ei=YNxtYMifE4fY-gSXhaf4CQ&amp;amp;oq=is+dark+mode+easier+on+your+eyes&amp;amp;gs_lcp=Cgdnd3Mtd2l6EAMyAggAMgUIABCGAzoHCAAQRxCwAzoHCCMQsAIQJzoGCAAQBxAeOgQIABANUKAPWM0TYLcZaAFwAngAgAGZAYgB9gOSAQM1LjGYAQCgAQGqAQdnd3Mtd2l6yAEIwAEB&amp;amp;sclient=gws-wiz&amp;amp;ved=0ahUKEwjIz7K_xezvAhUHrJ4KHZfCCZ8Q4dUDCA0&amp;amp;uact=5"&gt;cursory Google search&lt;/a&gt; should be enough to cast doubt on this assertion. Everyone is entitled to their opinion, but UX/UI designers know better than to base their designs on what users say. I think Jakob Nielsen &lt;a href="https://www.nngroup.com/articles/first-rule-of-usability-dont-listen-to-users/"&gt;said it best&lt;/a&gt; when he opined that good user design is in no way predicated on what users say – observing what they do is much more reliable and accurate.&lt;/p&gt;

&lt;p&gt;As someone who spends a majority of my working hours in front of a computer screen (not to mention a fair amount of leisure time in front of other screens), the health and wellness aspect of this issue of screen contrast polarity is of particular interest to me. Although I have recently found dark themes to be my aesthetic preference, my even more recent realization that dark mode provides nearly no research-backed benefit to the reading comfort and eye health of average users was surprising to me, and provided the motivation to write this article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--axIi3QpV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/KBjA2ro.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--axIi3QpV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/KBjA2ro.jpg" alt="Baby Yoda staring out bright window"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The case for light mode
&lt;/h2&gt;

&lt;p&gt;Opinions aside, the majority of actual evidence-based, rigorous research seems to point to light mode being more advantageous for both eye health and legibility. Researchers with the Journal of the Human Factors and Ergonomics Society published a study in 2013 that &lt;a href="https://journals.sagepub.com/doi/abs/10.1177/0018720813515509?journalCode=hfsa&amp;amp;"&gt;found that use of positive polarity (light mode) led to an improved perception of detail&lt;/a&gt;. Another study in 2017 found that readers reading with positive polarity could read accurately with greater speed and ease &lt;a href="https://pubmed.ncbi.nlm.nih.gov/28166901/"&gt;than they could with negative polarity (dark mode)&lt;/a&gt;. Interestingly, the same study found that these results were even more pronounced under dark ambient illumination, meaning that reading dark mode legibly in low-light situations is even harder than in high-light situations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.nngroup.com/articles/author/raluca-budiu/"&gt;Raluca Budiu&lt;/a&gt; wrote a &lt;a href="https://www.nngroup.com/articles/dark-mode/"&gt;thorough summary&lt;/a&gt; of research findings on this issue. The results seem to indicate light mode leading to superior results in tasks across the board, both for visual acuity and proofreading. However, her summary did contain some points in favor of dark that are worth considering as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/uNgUzhakqXkyI/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/uNgUzhakqXkyI/giphy.gif" alt="Only a sith deals in absolutes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Few things in life are absolute
&lt;/h2&gt;

&lt;p&gt;Although &lt;a href="https://www.nngroup.com/articles/dark-mode/"&gt;Budiu's summary&lt;/a&gt; indicates that light mode wins across the board in terms of legibility and efficiency in reading, and that contrast polarity had no significant impact on eye fatigue, there are still [a couple] reasons to consider sticking with dark mode. A 2018 study found that the way that the eye behaves when reading positive polarity &lt;a href="https://www.nature.com/articles/s41598-018-28904-x"&gt;may lead to myopia (nearsightedness) in the long term&lt;/a&gt;. The suggested remedy to offset this effect is, naturally, to use dark mode.&lt;/p&gt;

&lt;p&gt;As far as I can tell, this study is the only published academic study that supports this conclusion. One of the core tenets of proper research is replicability, so although I cannot dismiss this study's findings, more research supporting this conclusion is needed to affirm the findings (in my opinion).&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Although I will keep checking the research to see if any more published studies corroborate the 2018 myopia study's findings, I feel that the great majority of research into the effect of contrast polarity on eyes indicates that light mode is the way to go. So, although dark themes are cool and trendy, I'm switching back to light all the way (I'm typing this article in a light-theme VSC. It is so weird...).&lt;/p&gt;

&lt;p&gt;What will you do? Join the dark side or turn towards the light? Whatever you choose, make sure you understand the risks and benefits of your options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LCZIQceA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/mei9JUj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LCZIQceA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/mei9JUj.jpg" alt="stormtrooper takes off helmet to reveal that he is Batman"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Attributions&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Baby Yoda at window image by Victor Serban (UnSplash)&lt;/li&gt;
&lt;li&gt;Stormtrooper Batman image by Daniel Cheung (UnSplash)&lt;/li&gt;
&lt;li&gt;Red lightsaber in cave image by Cade Roberts (UnSplash)&lt;/li&gt;
&lt;li&gt;Darth Vader image by Tommy van Kessel (UnSplash)&lt;/li&gt;
&lt;li&gt;Obi Wan gif linked from Giphy&lt;/li&gt;
&lt;li&gt;Black and white coffee image by Alex (UnSplash)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>screen</category>
      <category>health</category>
      <category>lightmode</category>
      <category>darkmode</category>
    </item>
    <item>
      <title>Vanilla JS Tutorial - Part One
Building blocks of the DOM</title>
      <dc:creator>Mike Shine</dc:creator>
      <pubDate>Sun, 11 Apr 2021 17:01:06 +0000</pubDate>
      <link>https://dev.to/shinetechnicallywrites/vanilla-js-tutorial-part-one-building-blocks-of-the-dom-nh9</link>
      <guid>https://dev.to/shinetechnicallywrites/vanilla-js-tutorial-part-one-building-blocks-of-the-dom-nh9</guid>
      <description>&lt;p&gt;This post is the first part of a code-along tutorial, where you'll learn some rudimentary skills in vanilla JS DOM manipulation. If you missed my previous entry, where I discussed what vanilla JS is and why it's important, check it out &lt;a href="https://shinetechnicallywrites.netlify.app/posts/lets-talk-about-vanilla-js/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;

&lt;p&gt;1) Make a folder/directory on your computer. Name it something appropriate, like "vanilla-js-practice."&lt;br&gt;
2) Create two files within this folder, &lt;code&gt;index.html&lt;/code&gt; and &lt;code&gt;index.js&lt;/code&gt;.&lt;br&gt;
3) Next, let's add some boilerplate code to our new &lt;code&gt;index.html&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"utf-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt; Vanilla JS Practice &lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"index.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Normally, in the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; section of an HTML file, we would see all sorts of elements, like &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;h2&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, and &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;, to name a few. However, in our HTML file, the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; section contains only a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag and nothing else. This &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag essentially tells the web browser to read the &lt;code&gt;index.js&lt;/code&gt; file for valid code to run.&lt;/p&gt;

&lt;p&gt;This is the last time we will touch our &lt;code&gt;index.html&lt;/code&gt; file; every other line of code you see in this article will be in our &lt;code&gt;index.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building Blocks #1 &amp;amp; 2 - &lt;code&gt;document.createElement()&lt;/code&gt; and &lt;code&gt;.textContent&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FRZYHhmL.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FRZYHhmL.jpg" alt="pile of blue legos"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All right, now that we are in our &lt;code&gt;index.js&lt;/code&gt; file, type the following code block into your editor, and then read on for an explanation of what you just typed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Vanilla JS practice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Perhaps the most important snippet of code to remember when using vanilla JS for DOM manipulation is &lt;code&gt;document.createElement()&lt;/code&gt;. Put simply, this is the code you use to create an HTML element. The HTML element you want to create goes inside the parentheses in quotation marks. In our example, we used &lt;code&gt;document.createElement('h1')&lt;/code&gt; to create an &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; element, which we then stored in &lt;code&gt;header&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another very important building block is &lt;code&gt;.textContent&lt;/code&gt;. As you may have deduced, this is the property that allows us to set or change the text of an element. In the second line of our example, we take the element that we created in the previous line (&lt;code&gt;header&lt;/code&gt;) and set its text to &lt;code&gt;Vanilla JS practice&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Whew! That was kind of a lot of explanation for something that simple. Using vanilla JS to write HTML code ends up being quite a bit lengthier than just writing the HTML itself. The HTML equivalent of our two lines of vanilla JS above would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Vanilla JS Practice&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You would be hard-pressed to find a web developer who says that using vanilla JS is the most quick and concise way to write code. However, remember that you're learning this not because it's the quickest or most elegant way to code. You're learning this because it is a great way to remove layers of abstraction and really understand the mechanics of the language and the DOM. So, on we go!&lt;/p&gt;

&lt;p&gt;Okay, time to see what we've got so far. Open up your &lt;code&gt;index.html&lt;/code&gt; file in your web browser to see our new header:&lt;/p&gt;

&lt;p&gt;...Where is it? Why has it not appeared?? 😱😱😱&lt;br&gt;
&lt;a href="https://i.giphy.com/media/hQ0GvkpZwYcgM/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/hQ0GvkpZwYcgM/giphy.gif" alt="despairing Psyduck"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The short answer: &lt;em&gt;It isn't there because you haven't told it to be there&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fen.meming.world%2Fimages%2Fen%2Fthumb%2F2%2F2c%2FSurprised_Pikachu_HD.jpg%2F300px-Surprised_Pikachu_HD.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fen.meming.world%2Fimages%2Fen%2Fthumb%2F2%2F2c%2FSurprised_Pikachu_HD.jpg%2F300px-Surprised_Pikachu_HD.jpg" alt="surprised Pikachu face"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Building Blocks #3, 4, and 5 - &lt;code&gt;.appendChild()&lt;/code&gt;, &lt;code&gt;.removeChild()&lt;/code&gt;, and &lt;code&gt;.remove()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Don't worry, you haven't made a mistake! This is an important distinction between HTML and vanilla JS. In HTML, under normal circumstances, any elements with proper syntax in between the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; tags will render to the DOM. When using vanilla JS, this is not the case; we have to intentionally add each element we create to the DOM. Kind of a pain, don't you think? It's one of the reasons why using vanilla JS for an entire project isn't generally advisable unless you are doing it for the sake of practice, like we are.&lt;/p&gt;

&lt;p&gt;Anyway, this is where &lt;code&gt;.appendChild()&lt;/code&gt; comes in.&lt;/p&gt;

&lt;p&gt;Here's how we will add &lt;code&gt;header&lt;/code&gt; to the DOM:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;document.body.appendChild(header)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Document.body&lt;/code&gt; references the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; section of our &lt;code&gt;index.html&lt;/code&gt; file, and &lt;code&gt;appendChild&lt;/code&gt; is a native method we can use to add the specified element (in this case, &lt;code&gt;header&lt;/code&gt;) to the DOM. Please note that &lt;code&gt;appendChild&lt;/code&gt; adds the specified element to the DOM &lt;strong&gt;below&lt;/strong&gt; any previously appended elements.&lt;/p&gt;

&lt;p&gt;Now then, the three lines of code we should have typed out in our code editor so far are as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Vanilla JS practice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open up your &lt;code&gt;index.html&lt;/code&gt; file in your browser once more and you should see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FYblptHE.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FYblptHE.jpg" alt="Text vanilla js practice on a white background"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Good job! You have created your first element in vanilla JS, an &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; header.&lt;/p&gt;

&lt;p&gt;Let’s try to create a little bit more. Use the steps we took to create and append the header to create some text below your header. Try this on your own, and then scroll below if you need some guidance or to compare your efforts to mine. Good luck, you can do this!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/unQ3IJU2RG7DO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/unQ3IJU2RG7DO/giphy.gif" alt="cat furiously pawing computer keyboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How did you do? Hopefully you were able to add some text below your header. More importantly, I hope you are at least a little more solid now in your understanding of JS, HTML, and the DOM, compared to where you were before you scrolled all the way down here. &lt;/p&gt;

&lt;p&gt;Here’s what I did as my text addition:&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Vanilla JS practice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Go hang a salami, I&lt;/span&gt;&lt;span class="se"&gt;\'&lt;/span&gt;&lt;span class="s1"&gt;m a lasagna hog.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Read the previous sentence backwards.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FIPpkuDE.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FIPpkuDE.jpg" alt="same heading as before with text below"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nicely done!&lt;/p&gt;

&lt;p&gt;Just as we can add elements to the DOM with &lt;code&gt;appendChild&lt;/code&gt;, we can take them away with &lt;code&gt;removeChild&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, if I wanted to to remove my &lt;code&gt;text2&lt;/code&gt; variable that I created above, I could do so one of two ways:&lt;/p&gt;

&lt;p&gt;1) &lt;code&gt;document.body.removeChild(text2);&lt;/code&gt;&lt;br&gt;
2) &lt;code&gt;text2.remove();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The first way would be using &lt;code&gt;removeChild&lt;/code&gt; to remove an element in exactly the same way that we used &lt;code&gt;appendChild&lt;/code&gt; to add an element; we invoke a method at the parent level (&lt;code&gt;document.body&lt;/code&gt; is the parent of &lt;code&gt;text2&lt;/code&gt;) to add or remove a child element (&lt;code&gt;text2&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FIWz3oMe.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FIWz3oMe.jpg" alt="crying toddler"&gt;&lt;/a&gt;&lt;br&gt;
Don't cry, child. Your parents won't remove you using vanilla JS!&lt;/p&gt;

&lt;p&gt;The second way is different; it utilizes the &lt;code&gt;remove&lt;/code&gt; method instead of &lt;code&gt;removeChild&lt;/code&gt;. Since we aren't referencing parent or child elements with this method, it can be called directly on the element to be removed, thus &lt;code&gt;text2.remove()&lt;/code&gt; would be our syntax.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building Blocks #6 &amp;amp; 7 - &lt;code&gt;.setAttribute()&lt;/code&gt; and &lt;code&gt;.removeAttribute()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The process of labeling elements in certain ways and then using those labels to access or modify those elements is essential in web development. In HTML, the three "label types" that we have are types, classes, and ids. If you are just hearing about this for the first time, &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Type_Class_and_ID_Selectors" rel="noopener noreferrer"&gt;click here&lt;/a&gt; and take a few minutes to learn about these labels (more accurately called selectors).&lt;/p&gt;

&lt;p&gt;We can use vanilla JS to set and remove these attributes. &lt;code&gt;.setAttribute()&lt;/code&gt; requires two arguments; the attribute to be set and the name of the attribute.&lt;/p&gt;

&lt;p&gt;Let's look at some examples.&lt;/p&gt;

&lt;p&gt;1) Adding the class "palindrome" to &lt;code&gt;text1&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;text1.setAttribute("class", "palindrome");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2) Adding the id "giantWalrus" to &lt;code&gt;text2&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;text2.setAttribute("id", "giantWalrus");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Removing attributes with &lt;code&gt;.removeAttribute()&lt;/code&gt; works in almost the same way, except when removing the value of the selector doesn't need to be specified. For example, to remove the id "giantWalrus" from &lt;code&gt;text2&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;text2.removeAttribute("id");&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Building Blocks #8 &amp;amp; 9 - &lt;code&gt;.querySelector()&lt;/code&gt; and &lt;code&gt;.querySelectorAll()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Now that we have learned how to use vanilla JS to set attributes onto our elements, we probably ought to know how to access them through their attributes too.&lt;/p&gt;

&lt;p&gt;The methods we use for accessing elements by attribute are &lt;code&gt;.querySelector()&lt;/code&gt; and &lt;code&gt;.querySelectorAll()&lt;/code&gt;. &lt;code&gt;.querySelector()&lt;/code&gt; returns the first element in the document that matches the provided selector, while &lt;code&gt;.querySelectorAll()&lt;/code&gt; returns all matching elements, in the form of a NodeList. A NodeList is similar to an array, but with fewer available methods.&lt;/p&gt;

&lt;p&gt;For either of these methods, more than one attribute can be provided as a criteria; additional attributes are separated by commas.&lt;/p&gt;

&lt;p&gt;Let's look at some examples. The answers to #1 and #2 are provided below the prompt. The answers for #3 and #4 are down a couple lines; try them yourself first and then check your work!&lt;/p&gt;

&lt;p&gt;1) Create a variable &lt;code&gt;firstElem&lt;/code&gt; that contains the first element of class &lt;code&gt;sampleClass&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const firstElem = document.querySelector(".myClass");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2) Create a variable &lt;code&gt;allElems&lt;/code&gt; that contains all elements of the class &lt;code&gt;classDismissed&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const allElems = document.querySelectorAll(".classDismissed");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3) Create a variable &lt;code&gt;greatId&lt;/code&gt; that contains the element with the id &lt;code&gt;thisIsAGreatExampleIdName;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;4) Create a variable &lt;code&gt;divsAndPs&lt;/code&gt; that contains all &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; elements in the document:&lt;/p&gt;




&lt;p&gt;Answer to #3 - &lt;code&gt;const greatId = document.querySelector("#thisIsAGreatExampleIdName");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Answer to #4 - &lt;code&gt;const divsAndPs = document.querySelectorAll("div", "p");&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Great work, you made it to the end of part 1! You learned about vanilla JS, why it is worth spending time on, and got some hands-on practice. In this tutorial, you:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Created HTML elements with vanilla JS&lt;/li&gt;
&lt;li&gt;Appended and removed those elements to and from the DOM&lt;/li&gt;
&lt;li&gt;Selected elements by attributes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope you found this helpful! Check out part 2 soon, where we take on using vanilla JS to style content.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Attributions&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Jenga photo by Nathan Dumlao (Unsplash)&lt;/li&gt;
&lt;li&gt;Vanilla ice cream cone image by Dana DeVolk (Unsplash)&lt;/li&gt;
&lt;li&gt;Blue blocks image by Iker Urteaga (Unsplash)&lt;/li&gt;
&lt;li&gt;Psyduck gif credited to The Pokémon Company International, Inc.&lt;/li&gt;
&lt;li&gt;Surprised pikachu face image credited to The Pokémon Company International, Inc.&lt;/li&gt;
&lt;li&gt;Cat mashing keyboard gif by @AaronsAnimals (Giphy)&lt;/li&gt;
&lt;li&gt;Crying toddler image by Arwan Sutanto (Unsplash)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Let's Talk About Vanilla JavaScript</title>
      <dc:creator>Mike Shine</dc:creator>
      <pubDate>Wed, 07 Apr 2021 19:06:31 +0000</pubDate>
      <link>https://dev.to/shinetechnicallywrites/let-s-talk-about-vanilla-javascript-51gp</link>
      <guid>https://dev.to/shinetechnicallywrites/let-s-talk-about-vanilla-javascript-51gp</guid>
      <description>&lt;p&gt;You: “May I use a framework such as React or Vue for this prompt?”&lt;/p&gt;

&lt;p&gt;Interviewer: “No, please complete the prompt in vanilla JavaScript.”&lt;/p&gt;

&lt;p&gt;You: &lt;a href="https://i.giphy.com/media/KcnIdXKOuvxNjLqYc1/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/KcnIdXKOuvxNjLqYc1/giphy.gif" alt="gif of man sweating"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ahh, vanilla JavaScript… it makes even seasoned web developers break out into a cold sweat. But, with a little practice in the fundamentals, you might learn that vanilla JavaScript (JS) can be your friend! :)&lt;/p&gt;

&lt;p&gt;This article serves as an introduction to a multi-part tutorial on vanilla JavaScript. The tutorial begins in the next article.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is vanilla JS, and why should I spend any time on it?
&lt;/h3&gt;

&lt;p&gt;“Vanilla JS” is a casual nickname the programming language JavaScript in its pure form, without the benefit of any added frameworks or libraries (such as React or Vue, as mentioned above). Such frameworks and libraries are not only helpful in a professional setting, they are imperative! However, most veteran programmers would recommend that beginning programmers not start with such tools, as they can sometimes impair your underlying conceptual understanding of web development. Starting with the no-frills version of JS will lead to more concrete understanding of how the code interacts with the browser, which is a core component of web development.&lt;/p&gt;

&lt;p&gt;This &lt;a href="http://vanilla-js.com/"&gt;tongue-in-cheek website&lt;/a&gt; is a humorous repackaging of vanilla JS into a sleek new framework. Developers love shiny new frameworks, and truthfully if vanilla JS was sentient and had any interest in "rebranding itself" this would be an excellent strategy.&lt;/p&gt;

&lt;p&gt;Sometimes the term vanilla JS sometimes implies that some of the HTML and CSS coding for a project that might otherwise occur in separate HTML/CSS files will be handled in the JS file. So, rather than using vanilla JS only for the JavaScript portion of your project, you might do some of the styling or DOM manipulation in the .js file as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Some Disclaimers
&lt;/h3&gt;

&lt;p&gt;If you have coding experience, especially web development experience, odds are the following tutorial will be way too slow for you. No quick tricks here; this is more of a methodical walkthrough for absolute beginners to DOM manipulation trying to learn basics.&lt;/p&gt;

&lt;p&gt;Now, the tutorial that follows this article does presuppose a basic knowledge of HTML and JS. Try a couple of &lt;a href="https://replit.com/@masongalland/Beginner-JS-Toy-Problems"&gt;these toy problems&lt;/a&gt;; if they seem easy or at least doable, you should be good to go. If they seemed a little (or a lot) beyond you, I recommend spending some time &lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/"&gt;here&lt;/a&gt; completing some of the Basic Javascript section. Then, head back here when you feel ready.&lt;/p&gt;

&lt;p&gt;You also need to have a code editor installed or some sort of development environment that you have some competence with; my weapon of choice is VSC &lt;a href="https://code.visualstudio.com/"&gt;(Visual Studio Code)&lt;/a&gt;, but any code editor will do the trick.&lt;/p&gt;

&lt;h3&gt;
  
  
  What should I expect from one of your tutorials?
&lt;/h3&gt;

&lt;p&gt;The focus in this tutorial will be JS (obviously), so we will be doing all of our coding in a SINGLE JS file; no HTML or CSS at all!*&lt;/p&gt;

&lt;p&gt;&lt;em&gt;*…Except for a boilerplate HTML with a script tag linking to our JS file.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I know, I lied, but this HTML file is really only to make the browser happy. Browsers like HTML code, and even though we humans are reading this article to learn to make HTML with JS, web browsers don’t have the same willingness to learn as you do. So, kudos to you for being a better student than your browser!&lt;/p&gt;

&lt;p&gt;There will be opportunities throughout the tutorial for you to code along. You really should do this; have a file open in your text editor of choice and code along. It will help reinforce the knowledge, trust me (or don’t trust me and code along anyway).&lt;/p&gt;

&lt;p&gt;Thanks for bearing with me through the [admittedly somewhat boring] details and background. It is important to make sure you are at the right starting point so you will understand the content. Not understanding this stuff now will lead to you having a bad time when things get more complex (and they will DEFINITELY get more complex).&lt;/p&gt;

&lt;p&gt;In the following article, our vanilla JS tutorial begins!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Attributions&lt;/em&gt;: Panic reaction (sweating) GIF by Billy Budgen, linked from giphy.com&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
