{"id":159225,"date":"2013-12-29T11:19:50","date_gmt":"2013-12-29T18:19:50","guid":{"rendered":"http:\/\/css-tricks.com\/?p=159225"},"modified":"2013-12-29T14:35:47","modified_gmt":"2013-12-29T21:35:47","slug":"dom","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/dom\/","title":{"rendered":"What is the DOM?"},"content":{"rendered":"<p>A reader recently wrote in asking me what the DOM was. They said they&#8217;ve heard it mentioned and alluded to, but aren&#8217;t sure they really understand it. <\/p>\n<p>We can fix that. <\/p>\n<p><!--more--><\/p>\n<figure id='post-159227 media-159227' class='align-none'><img data-recalc-dims=\"1\" src=\"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2013\/12\/is-html-the-dom.jpg\" alt='Is HTML the DOM? Not really.' \/><\/figure>\n<p>But the HTML you write is parsed by the browser and turned into the DOM.<\/p>\n<figure id='post-159228 media-159228' class='align-none'><img data-recalc-dims=\"1\" src=\"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2013\/12\/view-source-no.jpg\" alt='' \/><\/figure>\n<p>View Source just shows you the HTML that makes up that page. It&#8217;s probably the exact HTML that you wrote. <\/p>\n<p>It might look like different code if, for example, you work in template files in a backend language and don&#8217;t look at the compiled HTML output very often. Or there is a &#8220;build process&#8221; that happens after you write your HTML and the code is put out to the live website. Perhaps that HTML is compressed or otherwise changed.<\/p>\n<p>View Source is a little weird actually. The only people that would care to look at that code are developers and all the major browsers have built in developer tools now. It has probably out-lived its usefulness.<\/p>\n<figure id='post-159229 media-159229' class='align-none'><img data-recalc-dims=\"1\" src=\"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2013\/12\/devtools-dom.jpg\" alt='Is DevTools the DOM? Yep.' \/><\/figure>\n<p>When you&#8217;re looking at the panel in whatever DevTools you are using that shows you stuff that looks like HTML, that is a visual representation of the DOM! We made it!<\/p>\n<figure id='post-159230 media-159230' class='align-none'><img data-recalc-dims=\"1\" src=\"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2013\/12\/looks-like-html.jpg\" alt='' \/><\/figure>\n<p>Well, yeah, it does. It was created directly from your HTML remember. In most simple cases, the visual representation of the DOM will be just like your simple HTML.<\/p>\n<p>But it&#8217;s often not the same&#8230;<\/p>\n<h3>When is the DOM different than the HTML?<\/h3>\n<p>Here&#8217;s one possibility: there are mistakes in your HTML and the browser has fixed them for you. Let&#8217;s say you have a <code>&lt;table&gt;<\/code> element in your HTML and leave out the required <code>&lt;tbody&gt;<\/code> element. The browser will just insert that <code>&lt;tbody&gt;<\/code> for you. It will be there in the DOM, so you&#8217;ll be able to find it with JavaScript and style it with CSS, even though it&#8217;s not in your HTML.<\/p>\n<p>The most likely case though, is&#8230;<\/p>\n<h3>JavaScript can manipulate the DOM<\/h3>\n<p>Imagine you have an empty element like this in your HTML:<\/p>\n<pre rel=\"HTML\"><code class=\"language-markup\">&lt;div id=\"container\"&gt;&lt;\/div&gt;<\/code><\/pre>\n<p>Then later in your HTML, there is a bit of JavaScript:<\/p>\n<pre rel=\"HTML\"><code class=\"language-markup\">&lt;script&gt;\r\n  var container = document.getElementById(\"container\");\r\n  container.innerHTML = \"New Content!\";\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>Even if you don&#8217;t know JavaScript, you can reason that bit of code out. On the screen you&#8217;ll see <em>New Content!<\/em> rather than nothing, because that empty <code>div<\/code> was filled with some, ahem, new content.<\/p>\n<p>If you use DevTools to check out the visual representation of the DOM, you&#8217;ll see:<\/p>\n<pre rel=\"HTML\"><code class=\"language-markup\">&lt;div id=\"container\"&gt;New Content!&lt;\/div&gt;<\/code><\/pre>\n<p>Which is different than your original HTML or what you would see in View Source.<\/p>\n<h3>Ajax and Templating<\/h3>\n<p>Let&#8217;s not go off the deep end here, but I bet you can imagine if you were to use Ajax to snag content from elsewhere and put it onto the page, the DOM is going to be very different than your original HTML. The same with loading in data of some sort and using <a href=\"https:\/\/css-tricks.com\/video-screencasts\/127-basics-of-javascript-templating\/\">client side templating<\/a>. <\/p>\n<p>Try going to Gmail and viewing source. It&#8217;s just a bunch of scripts and data from the original page load. Barely recognizable compared to what you see on the screen.<\/p>\n<h3>JavaScript vs. the DOM<\/h3>\n<p>JavaScript is a language that the browser reads and does stuff with. But the DOM is where that stuff happens. In fact a lot of what you might think of as a &#8220;JavaScript Thing&#8221; is more accurately a &#8220;DOM API&#8221;. <\/p>\n<p>For instance, we can write JavaScript that watches for a <code>mouseenter<\/code> event on an element. But that &#8220;element&#8221; is really a DOM node. We attach that listener via a DOM property on that DOM node. When that event happens, it&#8217;s the DOM node that emits that event.<\/p>\n<figure id='post-159232 media-159232' class='align-none'><img data-recalc-dims=\"1\" src=\"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2013\/12\/dom-dom-dom-dom.jpg\" alt='' \/><\/figure>\n<p>Appologies if I worded any of that stuff incorrectly. But you get the point I hope. The DOM is the lifeblood here. It&#8217;s where everything goes down in the browser. JavaScript is just the syntax, the language. It can be used totally outside the browser with no DOM APIs at all (see Node.js).<\/p>\n<h3>This article isn&#8217;t nearly nerdy and in-depth enough for me.<\/h3>\n<p>Well, the DOM stands for &#8220;Document Object Model&#8221; blah blah blah. I didn&#8217;t want to (and am not qualified) to write that article. Here&#8217;s some meaty ones:<\/p>\n<ul>\n<li>W3C: <a href=\"http:\/\/www.w3.org\/TR\/DOM-Level-2-Core\/introduction.html\" rel=\"noopener\">What is the Document Object Model?<\/a><\/li>\n<li>MDN: <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/DOM\/DOM_Reference\/Introduction\" rel=\"noopener\">Introduction &#8211; Document Object Model<\/a><\/li>\n<li>Wikipedia: <a href=\"http:\/\/en.wikipedia.org\/wiki\/Document_Object_Model\" rel=\"noopener\">Document Object Model<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A reader recently wrote in asking me what the DOM was. They said they&#8217;ve heard it mentioned and alluded to, [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[],"class_list":["post-159225","post","type-post","status-publish","format-standard","hentry","category-articles"],"acf":{"show_toc":"No"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":155634,"url":"https:\/\/css-tricks.com\/modular-future-web-components\/","url_meta":{"origin":159225,"position":0},"title":"A Guide to Web Components","author":"Rob Dodson","date":"November 11, 2013","format":false,"excerpt":"The following is a guest post by Rob Dodson (@rob_dodson). Rob and I were going back and forth in CodePen support getting Polymer (a web components polyfill, kinda) working on a demo of his. We did get it working, and things kind of evolved into this article. Take it away\u2026","rel":"","context":"In &quot;Articles&quot;","block_context":{"text":"Articles","link":"https:\/\/css-tricks.com\/category\/articles\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":352388,"url":"https:\/\/css-tricks.com\/twitters-div-soup-and-uglyfied-css-explained\/","url_meta":{"origin":159225,"position":1},"title":"Twitter&#8217;s div Soup and Uglyfied CSS, Explained","author":"Chris Coyier","date":"September 22, 2021","format":false,"excerpt":"When I came up in web development (2005-2010 were formative years for me), one of the first lessons I learned was to have a clean foundation of HTML. \"What Beautiful HTML Code Looks Like\" is actually one of the most popular posts on this very site. The image in that\u2026","rel":"","context":"In &quot;Links&quot;","block_context":{"text":"Links","link":"https:\/\/css-tricks.com\/category\/links\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-21-at-9.58.00-AM.png?fit=1200%2C709&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-21-at-9.58.00-AM.png?fit=1200%2C709&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-21-at-9.58.00-AM.png?fit=1200%2C709&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-21-at-9.58.00-AM.png?fit=1200%2C709&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-21-at-9.58.00-AM.png?fit=1200%2C709&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":352342,"url":"https:\/\/css-tricks.com\/comparing-methods-for-appending-and-inserting-with-javascript\/","url_meta":{"origin":159225,"position":2},"title":"Comparing Methods for Appending and Inserting With JavaScript","author":"Eric Markfield","date":"September 24, 2021","format":false,"excerpt":"Let's say we want to add something to a webpage after the initial load. JavaScript gives us a variety of tools. Perhaps you've used some of them, like append, appendChild, insertAdjacentHTML, or innerHTML. The difficult thing about appending and inserting things with JavaScript isn\u2019t so much about the tools it\u2026","rel":"","context":"In &quot;Articles&quot;","block_context":{"text":"Articles","link":"https:\/\/css-tricks.com\/category\/articles\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/09\/js-insert-append.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/09\/js-insert-append.jpg?fit=1200%2C600&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/09\/js-insert-append.jpg?fit=1200%2C600&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/09\/js-insert-append.jpg?fit=1200%2C600&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/09\/js-insert-append.jpg?fit=1200%2C600&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":316913,"url":"https:\/\/css-tricks.com\/what-ya-need-there-is-a-bit-of-templating\/","url_meta":{"origin":159225,"position":3},"title":"What ya need there is a bit of templating","author":"Chris Coyier","date":"July 21, 2020","format":false,"excerpt":"I had a fella write in to me the other day. He had some HTML, CSS, and JavaScript, and it just wasn't behaving like he thought it ought to. The HTML had some placeholders in it and the JavaScript had some data in it, and the assumption was that the\u2026","rel":"","context":"In &quot;Articles&quot;","block_context":{"text":"Articles","link":"https:\/\/css-tricks.com\/category\/articles\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/04\/ellipses-brackets.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/04\/ellipses-brackets.jpg?fit=1200%2C600&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/04\/ellipses-brackets.jpg?fit=1200%2C600&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/04\/ellipses-brackets.jpg?fit=1200%2C600&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/04\/ellipses-brackets.jpg?fit=1200%2C600&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":320854,"url":"https:\/\/css-tricks.com\/defining-view-source\/","url_meta":{"origin":159225,"position":4},"title":"Defining &#8220;View Source&#8221;","author":"Chris Coyier","date":"September 10, 2020","format":false,"excerpt":"Last time there was a little flurry of activity around the concept of \"View Source,\" I did get the sense that not everyone was on the same page about what that even means. Jim Nielsen: First, when we talk about \u201cView Source\u201d what precisely are we talking about? I think\u2026","rel":"","context":"In &quot;Links&quot;","block_context":{"text":"Links","link":"https:\/\/css-tricks.com\/category\/links\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/01\/complex-code.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/01\/complex-code.png?fit=1200%2C600&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/01\/complex-code.png?fit=1200%2C600&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/01\/complex-code.png?fit=1200%2C600&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/01\/complex-code.png?fit=1200%2C600&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":305838,"url":"https:\/\/css-tricks.com\/styling-in-the-shadow-dom-with-css-shadow-parts\/","url_meta":{"origin":159225,"position":5},"title":"Styling in the Shadow DOM With CSS Shadow Parts\u00a0","author":"Ollie Williams","date":"April 13, 2020","format":false,"excerpt":"Safari 13.1 just shipped support for CSS Shadow Parts. That means the ::part() selector is now supported in Chrome, Edge, Opera, Safari, and Firefox. We\u2019ll see why it\u2019s useful, but first a recap on shadow DOM encapsulation\u2026 The benefits of shadow DOM encapsulation I work at giffgaff where we have\u2026","rel":"","context":"In &quot;Articles&quot;","block_context":{"text":"Articles","link":"https:\/\/css-tricks.com\/category\/articles\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/03\/intersecting-parts.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/03\/intersecting-parts.png?fit=1200%2C600&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/03\/intersecting-parts.png?fit=1200%2C600&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/03\/intersecting-parts.png?fit=1200%2C600&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/03\/intersecting-parts.png?fit=1200%2C600&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/159225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=159225"}],"version-history":[{"count":8,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/159225\/revisions"}],"predecessor-version":[{"id":159277,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/159225\/revisions\/159277"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=159225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=159225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=159225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}