{"id":302876,"date":"2020-02-13T08:13:56","date_gmt":"2020-02-13T15:13:56","guid":{"rendered":"https:\/\/css-tricks.com\/?p=302876"},"modified":"2020-02-14T15:28:44","modified_gmt":"2020-02-14T22:28:44","slug":"why-javascript-is-eating-html","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/why-javascript-is-eating-html\/","title":{"rendered":"Why JavaScript is Eating HTML"},"content":{"rendered":"\n

Web development is always changing. One trend in particular has become very popular lately, and it fundamentally goes against the conventional wisdom about how a web page should be made. It is exciting for some but frustrating for others, and the reasons for both are difficult to explain.<\/p>\n\n\n\n

A web page is traditionally made up of three separate parts with separate responsibilities: HTML code<\/strong> defines the structure and meaning of the content on a page, CSS code<\/strong> defines its appearance, and JavaScript code<\/strong> defines its behavior. On teams with dedicated designers, HTML\/CSS developers and JavaScript developers, this separation of concerns<\/a> aligns nicely with job roles: Designers determine the visuals and user interactions on a page, HTML and CSS developers reproduce those visuals in a web browser, and JavaScript developers add the user interaction to tie it all together and \u201cmake it work.\u201d People can work on one piece without getting involved with all three.<\/p>\n\n\n\n

In recent years, JavaScript developers have realized that by defining a page\u2019s structure in JavaScript instead of in HTML (using frameworks such as React<\/a>), they can simplify the development and maintenance of user interaction code that is otherwise much more complex to build. Of course, when you tell someone that the HTML they wrote needs to be chopped up and mixed in with JavaScript they don\u2019t know anything about, they can (understandably) become frustrated and start asking what the heck we\u2019re getting out of this.<\/p>\n\n\n\n

As a JavaScript developer on a cross-functional team, I get this question occasionally and I often have trouble answering it. All of the materials I\u2019ve found on this topic are written for an audience that is already familiar with JavaScript \u2014 which is not terribly useful to those who focus on HTML and CSS. But this HTML-in-JS<\/strong> pattern (or something else that provides the same benefits) will likely be around for a while, so I think it\u2019s an important thing that everyone involved in web development should understand.<\/p>\n\n\n\n\n\n\n\n

This article will include code examples for those interested, but my goal is to explain this concept in a way that can be understood without them.<\/p>\n\n\n

Background: HTML, CSS, and JavaScript<\/h3>\n\n\n

To broaden the audience of this article as much as possible, I want to give a quick background on the types of code involved in creating a web page and their traditional roles. If you have experience with these, you can skip ahead.<\/p>\n\n\n

HTML is for structure and semantic meaning<\/h4>\n\n\n

HTML (HyperText Markup Language) code defines the structure and meaning of the content on a page. For example, this article’s HTML contains the text you’re reading right now, the fact that it is in a paragraph, and the fact that it comes after a heading and before a CodePen.<\/p>\n\n\n\n

Let\u2019s say we want to build a simple shopping list app. We might start with some HTML like this:<\/p>\n\n\n\n