<?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: Alex Dimitrov</title>
    <description>The latest articles on DEV Community by Alex Dimitrov (@xavortm).</description>
    <link>https://dev.to/xavortm</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%2F52091%2F27a79637-7cb1-4d54-be56-86bde031e01d.jpg</url>
      <title>DEV Community: Alex Dimitrov</title>
      <link>https://dev.to/xavortm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xavortm"/>
    <language>en</language>
    <item>
      <title>Two tips to keep your CSS simpler</title>
      <dc:creator>Alex Dimitrov</dc:creator>
      <pubDate>Fri, 07 May 2021 05:50:49 +0000</pubDate>
      <link>https://dev.to/xavortm/two-tips-to-keep-your-css-simpler-7fp</link>
      <guid>https://dev.to/xavortm/two-tips-to-keep-your-css-simpler-7fp</guid>
      <description>&lt;p&gt;One of the approaches to write &lt;strong&gt;clean code&lt;/strong&gt; is to also write &lt;strong&gt;simple code&lt;/strong&gt;. Meaning — do your task in a way that is easy to implement, has fewer things that could break, and overall is easy to understand by other developers.&lt;/p&gt;

&lt;p&gt;But how does that happen? Well, while &lt;em&gt;achieving greatness&lt;/em&gt; might not be what we are going for, trying to simplify things most likely is 🙂&lt;/p&gt;

&lt;h2&gt;
  
  
  Goal #1 — clean up your code from unneeded rules
&lt;/h2&gt;

&lt;p&gt;One of the easiest simplifications you can use is to &lt;strong&gt;not&lt;/strong&gt; write code. Or if you already wrote it to delete it. Which one? Welp, the one that &lt;strong&gt;does nothing&lt;/strong&gt; in particular. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="nc"&gt;.some-selector&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;black&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;line-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1.7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just looking at this snippet without knowing what is going on around it is not enough to decide what to delete. But, for the sake of the example, let's consider &lt;strong&gt;the most common use case&lt;/strong&gt; that you might encounter and see what can we do with the rules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="nc"&gt;.some-selector&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;black&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c"&gt;/* Inherittted? */&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* That is default */&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;line-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1.7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* for consistency, we might want this set to the parent */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If we never set &lt;code&gt;color&lt;/code&gt; , then it's inherited as black, so there is no point in defining it here.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;font-size&lt;/code&gt; is the same, unless you changed the font size, there is no point in writing its default value. This often happens while people are debugging, trying to visualize, or changing values while testing.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;line-height&lt;/code&gt; is slightly different — if you want visual consistency, often you'd like to have a specific font size for regular content and for low character-per-line content. If you define line-heights in many places, keeping things consistent could be a problem.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, with cleaning up the above, it's possible to end up with &lt;strong&gt;the same result&lt;/strong&gt;, but code that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="nc"&gt;.some-selector&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We just overwrite the margin sizings. And this could be further simplified if that margin is consistent across the website - if that is the case, we can set it to the top root level just as &lt;code&gt;p { margin: 1em }&lt;/code&gt; without the &lt;code&gt;...some-selector&lt;/code&gt; wrapper around it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other code that can be cleaned&lt;/strong&gt; is generally something along the lines of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;div&lt;/code&gt; with &lt;code&gt;width: 100%&lt;/code&gt; that doesn't have any other rules or parents that would redefine it&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;height: 100%&lt;/code&gt; for regular content (like a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag inside normal &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; tag). That height does nothing.&lt;/li&gt;
&lt;li&gt;Complex styles for simple results, but this is harder to showcase or explain — many times there are simpler ways to achieve a design&lt;/li&gt;
&lt;li&gt;Reduce depth — in vanilla CSS it's deleting the parent, in Sass, it's moving your selectors a level or two up.&lt;/li&gt;
&lt;li&gt;Rules that do nothing in particular (could be any)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Goal #2 - Style only what is needed
&lt;/h2&gt;

&lt;p&gt;Well, this one sounds obvious, isn't it? But we quickly forget it and just &lt;em&gt;go with it&lt;/em&gt;, it's not like there is some price to how much code is written, right? Well, there is — time and money come into play when maintaining a medium to large codebase becomes sluggish and people stay away from it.&lt;/p&gt;

&lt;p&gt;One of the ways to reduce this complication is to &lt;strong&gt;really think about what has to be styled&lt;/strong&gt;. How are components connected and which component styles what? Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* Demo component that does one thing only. */&lt;/span&gt;
&lt;span class="nc"&gt;.card&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.card&lt;/span&gt; &lt;span class="nc"&gt;.card-title&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2em&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;/* A &amp;lt;section&amp;gt; element right in the website wrapper */&lt;/span&gt;
&lt;span class="nc"&gt;.section-welcome&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5em&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we need a card to be &lt;strong&gt;blue&lt;/strong&gt; when it's inside the &lt;strong&gt;&lt;code&gt;.section-welcome&lt;/code&gt;&lt;/strong&gt; element, we have two realistic ways to do it: One is to follow the requirement 1:1 and write &lt;code&gt;.section-welcome .card {}&lt;/code&gt;, which will overwrite the &lt;code&gt;.card&lt;/code&gt; styles for us. The other approach is to write a &lt;strong&gt;modifier&lt;/strong&gt; for the &lt;code&gt;.card&lt;/code&gt; element like &lt;code&gt;.card--secondary&lt;/code&gt; for example.&lt;/p&gt;

&lt;p&gt;In this example, we &lt;strong&gt;can decide&lt;/strong&gt; which of the two approaches is correct by looking into the design and the requirements. If the welcome section is very specific in its design (something unique for the website) and the &lt;code&gt;.card&lt;/code&gt; element has to look different &lt;strong&gt;only there&lt;/strong&gt;, then it's understandable to nest it inside the &lt;code&gt;.section-welcome&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;.card&lt;/code&gt; element changes are less specific and could be reused elsewhere, then it's best to have a modifier. A modifier is an extra class you can set to the component, which will style it and its children differently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When you might want to use a Modifier:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the visual change is common enough that it can be used elsewhere in the application or website like color, size, spacing changes etc.&lt;/li&gt;
&lt;li&gt;When you can find it used elsewhere in the website. Note* if there are &lt;strong&gt;minor&lt;/strong&gt; differences like 4px smaller padding or some opacity to only one element and consistency isn't what your designer needs but instead 1:1 results, then you can mix modifiers with nested selectors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When you might want to nest your selector:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When there is only one change to a component on the whole app/website for a specific wrapper.&lt;/li&gt;
&lt;li&gt;When you have many wrappers that slightly change a component in a different way. If you were to use modifiers, you would struggle to name them and you'd notice that you can't reuse them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some of the tips above are derived from &lt;strong&gt;bad practice in design&lt;/strong&gt; - inconsistency, but in the real world, you will be bombarded with all kinds of scenarios — time limitations, bad designs, unrealistic expectations, difficult team members, lack of testing, change of directions and requirements, browser support that requires hacking, the editing experience (not just static code), backend team changing markup or forgetting a class (modifiers especially) and more. So, it takes experience in all of these problems to come up with the &lt;strong&gt;least problematic&lt;/strong&gt; approach and apply it.&lt;/p&gt;

&lt;p&gt;Overall, each situation might require you to think for a minute on the best approach. &lt;strong&gt;Don't forget you can refactor code!&lt;/strong&gt; It's a major reason why big projects get hard to maintain and grow with bad structure — even if you think well of it, put a good architecture, when problems like the ones described above come up, we as developers might often forget things like "If you hack something &lt;em&gt;now&lt;/em&gt;, come back to refactor it &lt;em&gt;as soon as possible".&lt;/em&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>clean</category>
      <category>html</category>
      <category>structure</category>
    </item>
    <item>
      <title>CSS Naming Conventions</title>
      <dc:creator>Alex Dimitrov</dc:creator>
      <pubDate>Mon, 03 May 2021 08:59:47 +0000</pubDate>
      <link>https://dev.to/xavortm/css-naming-conventions-nen</link>
      <guid>https://dev.to/xavortm/css-naming-conventions-nen</guid>
      <description>&lt;p&gt;If you are new to development as a whole, naming conventions might be something you've come across as a "best practice" and tips like "Pick one and follow it" were scattered across books, articles and tutorials.&lt;/p&gt;

&lt;p&gt;I won't spend too much time in the &lt;em&gt;what&lt;/em&gt; it is, but instead on the &lt;em&gt;why&lt;/em&gt;. To be brief, a naming convention is a set of rules on &lt;strong&gt;naming things&lt;/strong&gt;. Such rules might be "Always write with lower case" and "If there are two words, separate with a dash". Some might be more complex like "If a function returns a value, write get infront of it".&lt;/p&gt;

&lt;p&gt;In CSS, there are such naming conventions as well, and they solve a problem, which exists due to the nature of how CSS is parsed by the browser.&lt;/p&gt;

&lt;p&gt;The problem: &lt;strong&gt;You don't really have much of a scope control&lt;/strong&gt;. Of course, you have at-rules &lt;code&gt;@&lt;/code&gt;, that act as a block scope, but not much else. And nesting rules isn't really scoping, not in CSS. Because both examples: &lt;code&gt;.parent {}&lt;/code&gt; and &lt;code&gt;.parent .children {}&lt;/code&gt; sit in the same global scope. Yes, of course, &lt;code&gt;.children&lt;/code&gt;'s styles would be applied only if there is a &lt;code&gt;.parent&lt;/code&gt; up the tree, but both rules are written in the same level, both are in the same file.&lt;/p&gt;

&lt;h3&gt;
  
  
  A detour to JavaScript-land:
&lt;/h3&gt;

&lt;p&gt;To illustrate a difference, let's look into how CSS is scoped in the JavaScript world with libraries/frameworks/tools like CSS in JS. There, you can write your CSS again in the same &lt;em&gt;level&lt;/em&gt;, but it is truely scoped &lt;strong&gt;in the component&lt;/strong&gt;. It generates unreadable unique classes like &lt;code&gt;.ts7hf&lt;/code&gt; which is &lt;strong&gt;only&lt;/strong&gt; available to the component you defined it to. The tool manages the scope for you, you don't have to worry about &lt;strong&gt;leaking CSS&lt;/strong&gt; to other components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Back to CSS
&lt;/h3&gt;

&lt;p&gt;In CSS, all your code goes into one file. Well, that is not &lt;em&gt;required, *&lt;/em&gt;&lt;strong&gt;*but in the common case that's how it goes. You might bundle CSS files into one, or you might compile Sass files into one, in the end, most often it's the same file. And all rules inside sit **in the same scope&lt;/strong&gt;. How do you then manage what is applied where, and ensure that no styles &lt;strong&gt;leak&lt;/strong&gt;? By defining a set of rules that a developer must follow. To &lt;strong&gt;know and follow&lt;/strong&gt;, because they are not kept in check by a tool. &lt;/p&gt;

&lt;p&gt;First, let's see how a style can leak. In the example below, we define a very simple rule, where &lt;strong&gt;all h4 children of our About us page must be red.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.page-template-about-us&lt;/span&gt; &lt;span class="nt"&gt;h4&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in that about us page, we want to output a &lt;em&gt;card&lt;/em&gt; component that happens to use the h4 tag for it's heading element:&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;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;header&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card-header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h4&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"title"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello world!&lt;span class="nt"&gt;&amp;lt;/h4&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/header&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now, we want &lt;em&gt;all card elements&lt;/em&gt; to have &lt;strong&gt;tomato&lt;/strong&gt; headings. We are doing wild design work here, don't judge!&lt;/p&gt;

&lt;p&gt;So, we style it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.card&lt;/span&gt; &lt;span class="nt"&gt;h4&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;tomato&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some more experienced developers will already notice at least 4-5 problems with the examples above.&lt;/p&gt;

&lt;p&gt;Which color would the headnig end up with? The correct answer is one: It depends on the &lt;strong&gt;order of the rules&lt;/strong&gt;, because both the first and the second styling snippet contain the same &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity"&gt;Specificity&lt;/a&gt; (part of the CSS fundamentals).&lt;/p&gt;

&lt;p&gt;The h4 tag will apply the first color defined in the stylesheet, and then get overwritten by the next one, because it has the same level of specificity. The problem here is that you have to worry about the order of your rules. As stated before, they all sit in the &lt;strong&gt;global scope&lt;/strong&gt;, so anything coule be inherited or applyed from any place. Yes, even that small file someone created 6 years ago that is still bundled and you are afraid to delete, because you &lt;strong&gt;don't know what elements it styles&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Fix problems by following rules.
&lt;/h2&gt;

&lt;p&gt;The problem above happens when no rules are followed from the start of the project. Fixing that is a real struggle and requires a lot of dev time, QA-ing and risk of breaking the app/website. So it's rarely suggested. Instead, slowly rewrite parts of the website with &lt;strong&gt;new styles.&lt;/strong&gt; There will be another post on that topic exactly.&lt;/p&gt;

&lt;p&gt;❌ &lt;strong&gt;Don't target pure HTML tags with large-scale selectors.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The example above uses a class on the &lt;code&gt;body&lt;/code&gt; element. All HTML elements H4 inside will have the style applied. With that, you only create problems for yourself. Instead, you should better understand the &lt;strong&gt;business goal&lt;/strong&gt; that needs that page to style it's h4 tags and see if there is a better solution. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Style tags by class name as often as possible&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;.card&lt;/code&gt; element, you should select the &lt;strong&gt;heading element&lt;/strong&gt; by it's class selector. Most often, it's possible that it's HTML tag could change at some point and that change should be then reflected in CSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Follow a naming convention for your classes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The example above uses &lt;code&gt;.title&lt;/code&gt; for heading. Yes, it makes sense to name it title, but it's &lt;strong&gt;too generic&lt;/strong&gt; and it isn't scoped to the &lt;code&gt;.card&lt;/code&gt; element. A simple change like writing &lt;code&gt;.card-title&lt;/code&gt; would increase readability and reduce the chances of styles clashing. Think of it this way — how many components on your app/website would have a title of some sorts? Would you feel safe, naming all of them with the same class? How can you be certain no styles would leak?&lt;/p&gt;

&lt;h2&gt;
  
  
  What naming conventions exist?
&lt;/h2&gt;

&lt;p&gt;Alright, so we saw some problems that exist because we don't set rules or think about how everything works together. All great, but the example rules above are too few and don't cover all cases, right? The good thing is that &lt;strong&gt;people already came up with rules&lt;/strong&gt; that you can take for free and follow. They work both on small and large scale applications. Which one is the best for you is maybe a bit trickier to decide, so the best thing you can do is build the same app using different conventions and see for yourself.&lt;/p&gt;

&lt;h3&gt;
  
  
  BEM — Block Element Modifier (&lt;a href="http://getbem.com/naming/"&gt;documentation&lt;/a&gt;)
&lt;/h3&gt;

&lt;p&gt;Maybe the most populer one that comes to mind is BEM. It is easy to understand, the concept is straightforward and just reading a class could tell you about what's going on (when written well of course).&lt;/p&gt;

&lt;h3&gt;
  
  
  OOCSS — &lt;strong&gt;Object Orientated CSS&lt;/strong&gt; (&lt;a href="https://www.smashingmagazine.com/2011/12/an-introduction-to-object-oriented-css-oocss/"&gt;Guide&lt;/a&gt;)
&lt;/h3&gt;

&lt;p&gt;The idea of separating visuals from structural styles is baked into the &lt;strong&gt;Object Orientated CSS&lt;/strong&gt; approach. It focuses a lot less on how you name your classes and more on the idea of what they represent.&lt;/p&gt;

&lt;h3&gt;
  
  
  SMACSS — Scalable and Modular Architecture for CSS (&lt;a href="http://smacss.com"&gt;website&lt;/a&gt;)
&lt;/h3&gt;

&lt;p&gt;SMACSS focses more on organizing your styles — placing each selector in it's proper category like Base, Layout, Module etc. It's similar to the categorization done in the previous post in the series: &lt;a href="https://dev.to/xavortm/the-component-types-in-front-end-development-2g0f"&gt;The Component Types in Front-End Development&lt;/a&gt;. I have personally taken a ton of the practices in SMACSS in my day-to-day work like the state management (is prefix) or the avoidance of element selectors among many others. &lt;/p&gt;

&lt;h3&gt;
  
  
  ITCSS — Inverted Triangle CSS (&lt;a href="https://www.xfive.co/blog/itcss-scalable-maintainable-css-architecture/"&gt;article&lt;/a&gt;)
&lt;/h3&gt;

&lt;p&gt;Similar to the Component types, it's a method of grouping your styles based on the context and their purpose. It goes from the most broad selectors to the most specific ones. The article above is a very good read and following that line of thought is always helpful in your code structure.&lt;/p&gt;

&lt;h3&gt;
  
  
  AMCSS — Attribute modules for CSS (&lt;a href="https://amcss.github.io"&gt;website&lt;/a&gt;)
&lt;/h3&gt;

&lt;p&gt;It's a very interesting approach that takes selecting elements to a new place — ignore the Class attribute and use all the custom Data attributes you can add to an HTML element. Instead of writing .button, why not do [am-Button] ? What I liked here is that you can visually differenciate different types of classes. A button can have it's core .button class, but it can also be a js-toggle-modal, which should be maintaining some interactivity. Both are very different, but also applied to the same attribute "class". With AMCSS, this can be separated visually. However, I am kinda iffy on the look of the selectors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Your naming convention
&lt;/h3&gt;

&lt;p&gt;All of the above are some of the more popular one other developers came up with. But the thing is that you will probably find as many naming conventions as many developers there are. Some will dislike an idea of BEM, so they will skip it and in it's place take something from SMACSS. Some will like the idea of ITCSS's separation, and appliy it with the AMCSS attributes.&lt;/p&gt;

&lt;p&gt;I personally love the BEM idea, but it doesn't match the WordPress standard of writing classes with lower case, separated with a single dash. And it's fine, because I can follow the same thinking of BEM, but without the &lt;code&gt;__&lt;/code&gt; and &lt;code&gt;--&lt;/code&gt; as separators. I could also use the .js- and .is- prefixes too from other approaches. And the cool thing - it works, it makes sense and most importantly &lt;strong&gt;it's consistent&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Which is the final takeaway from this — do whatever works for you, but do it &lt;strong&gt;consistently across the project&lt;/strong&gt;. Ideally, &lt;strong&gt;across all your projects&lt;/strong&gt;. That way, all developers will expect where to find things and how to write styles without looking at long documentations and set of rules.&lt;/p&gt;

&lt;p&gt;Read next: &lt;a href="https://dev.to/xavortm/two-tips-to-keep-your-css-simpler-7fp"&gt;Two tips to keep your CSS simpler&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>naming</category>
      <category>architecture</category>
      <category>structure</category>
    </item>
    <item>
      <title>The Component Types in Front-End Development</title>
      <dc:creator>Alex Dimitrov</dc:creator>
      <pubDate>Sat, 01 May 2021 16:28:17 +0000</pubDate>
      <link>https://dev.to/xavortm/the-component-types-in-front-end-development-2g0f</link>
      <guid>https://dev.to/xavortm/the-component-types-in-front-end-development-2g0f</guid>
      <description>&lt;p&gt;As a front-end developer, it's helpful to be able to differenciate the type of components you are working with (or building). Why? Because by knowing the abstraction level of a component, you can composite them in a way that is easier to maintain, extend and modify in the future. &lt;/p&gt;

&lt;p&gt;In this short article, we will define different types of components and order them by level of abstraction — the lowest on the list, will be components that do one thing only and are very specific, and up the list are very broad components that could have &lt;em&gt;anything&lt;/em&gt; inside really.&lt;/p&gt;

&lt;p&gt;One important thing to note however as could create confusion — we are not talking about components (elements) like &lt;code&gt;div&lt;/code&gt; or &lt;code&gt;a&lt;/code&gt;, but instead components we define as developers.&lt;/p&gt;

&lt;p&gt;The list doesn't look into the type of data these components hold or how it's updated or anything of this sort. This list is used when you get an image of the design you need to create and you produce a clean HTML file from it. That's it. No state, no APIs, no backend, SPAs, anything. &lt;strong&gt;This list is just the HTML structure in it's purest form&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. HTML Element Wrappers
&lt;/h2&gt;

&lt;p&gt;It's a Button component. Or a Image component. A Link, List, Avatar, Date, Select and so on. Of course, they can get very complex very quickly, but in it's base form, it's the same as a regular HTML Element + a custom class or a couple of nested HTML Elements like &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From a very small scale like a &lt;code&gt;&amp;lt;Button&amp;gt;&lt;/code&gt; with a couple of colors like Primary or Secondary, such wrapper could grow to the &lt;a href="https://select2.org"&gt;Select2 library&lt;/a&gt;'s scale for example. It remains a select, but with a ton of options and views.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of HTML Element Wrappers:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Button&lt;/li&gt;
&lt;li&gt;List (like a checklist or ingredient list etc)&lt;/li&gt;
&lt;li&gt;Heading&lt;/li&gt;
&lt;li&gt;Date (like when a post was published)&lt;/li&gt;
&lt;li&gt;Counter (example: &lt;em&gt;17 successful clients&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Group of elements
&lt;/h2&gt;

&lt;p&gt;The grouping is a single step above the HTML Element Wrappers — it's a collection of such elements. It's not really a &lt;em&gt;component&lt;/em&gt; that can be described as a "Card" or "Comment" or "Header of the website" etc. It's only serving the purpose of wrapping the first level, so that you can style them as a whole. &lt;/p&gt;

&lt;p&gt;Let's take the example of the &lt;code&gt;Button&lt;/code&gt; component: When you create a &lt;code&gt;&amp;lt;Button&amp;gt;&lt;/code&gt; component (or &lt;code&gt;&amp;lt;button class="button"&amp;gt;&lt;/code&gt;), it applies all the styles from the &lt;code&gt;.button&lt;/code&gt; class. But how would that look like when there is a second button next to it? It's &lt;strong&gt;too generic&lt;/strong&gt; to give it a name. You can't really call that class="buttons-for-signing-up". It's too generic, because as soon as you need to reuse these styles for moderating a comment, then it compeltely misses the point.&lt;/p&gt;

&lt;p&gt;So, this is where we need the grouping component — an abstract component that cares only about the component that is inside it. Not the data, the component. So, in this case, you can name it &lt;code&gt;.group-buttons&lt;/code&gt; or &lt;code&gt;.ctas-wrapper&lt;/code&gt; or &lt;code&gt;.buttons-wrapper&lt;/code&gt; etc. With a name like that, it doesn't matter if the buttons say "Log in" / "Sign Up" or "Reply" / "Delete". It is still a wrapper. Of what? Of buttons! That's it, nothing more.&lt;/p&gt;

&lt;p&gt;The second you wrap your very generic first level component with a specific name, you lock yourself from reusing it. And this goes agains the goal of building a &lt;strong&gt;reusable, maintainable and extendable structure.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Layout
&lt;/h2&gt;

&lt;p&gt;Layout is a wrapper around other components, not just first level ones. A great example, that many front-end developers know of a Layout is the Grid system. Yep, the same grid you see in &lt;a href="https://getbootstrap.com/docs/5.0/layout/grid/"&gt;Bootstrap&lt;/a&gt;, &lt;a href="https://get.foundation/sites/docs/grid.html"&gt;Foundation&lt;/a&gt; or the good old &lt;a href="https://960.gs"&gt;960 grid&lt;/a&gt;. It's abstract, becasue it doesn't care about what you put inside. It lays down the structure of your page, similar to HTML itself — what is on top, what is on left, right, bottom etc.&lt;/p&gt;

&lt;p&gt;Layouts are a major part of any web app. You can separate a dashboard layout in piece like "top navigation", "main navigation (left)", "main", "footer" and each of those layout sub-components could contain more layouts. This is where CSS Grids shine, btw.&lt;/p&gt;

&lt;p&gt;With this knowledge, you might already guess that naming layouts is tricky! Abstract names like the frameworks above use are perfect — small-6 or lg-3 etc, but, what do you do when you want your own? Like Main content of blog post and it's sidebar? When you name such sections, try to keep your names abstract as well, don't name the sidebar as "sidebar", because sometimes, it might hold something completely different. Example names you can use could be "Primary" and "Secondary" or "Primary" and "Aside". None define what exactly goes inside. Naming is maybe one of the hardest parts in development as a whole, not just Front-End, so don't worry if it takes more time to come up with a good name, it will be worth in the end.&lt;/p&gt;

&lt;p&gt;As a takeaway - keep the names abstract in this level.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Unit
&lt;/h2&gt;

&lt;p&gt;This is the first level where we actually can name a component with a specific name.&lt;/p&gt;

&lt;p&gt;When you hear a Component, this is probably what you think of. I called it Unit, for the lack of better name probably at this moment and the confusion it would create if I called it a "Component". Well, everything in this list is a component in one form or another. Heck, even a &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; element is a component if you think about it.&lt;/p&gt;

&lt;p&gt;A Unit is something you limit a little, compared to the previous two levels. For example, a  (or &lt;code&gt;&amp;lt;div class="comment"&amp;gt;&lt;/code&gt;). You could have literally a single &lt;/p&gt;
&lt;p&gt; tag inside with the comment, but most of the time it would take the author of that commebt, his avatar, a link to reply, a number for which comment this is, when it was posted, replies to it and so on. &lt;/p&gt;

&lt;p&gt;Another unit could be a  (or &lt;code&gt;&amp;lt;div class="card"&amp;gt;&lt;/code&gt;) which can hold a heading and a body, maybe a footer. Each of these subcomponents could hold data of any time. This is a bit more generic than a comment as you could wrapp your comments in cards. But the common remains — they are build using smaller HTML Element Wrappers or groups of such or other small components and even other unit-level components.&lt;/p&gt;

&lt;p&gt;A Unit is your escape hatch when you need to go specific. You are much more free to do it in this level than in the "Specific standalone HTML Element Wrapper" level, because your units, while named, are open to more types of content. They work mostly as wrappers like in the second level.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Specific standalone HTML Element Wrappers
&lt;/h2&gt;

&lt;p&gt;That's a mouthfull, maybe shorter name would be helpful here. But it describes it good — for this one, you are specifically defining what a component should be in it's name. The name this time is not abstract — meaning, it won't hold just any value or content, but something specific.&lt;/p&gt;

&lt;p&gt;A Button component can hold text like "Log in" or "Click me" or "Close" or "Download our FREE Ebook". But a component like &lt;code&gt;PDFDownload&lt;/code&gt; or &lt;code&gt;&amp;lt;button class="pdf-download"&amp;gt;&lt;/code&gt; is much more specific. You shouldn't expect it to open a modal, right? It should &lt;strong&gt;download&lt;/strong&gt; a PDF. Of course, you can inherit styles and attributes from the first level component like Button, but you make it more specific. And this decreases the cases when you can use it.&lt;/p&gt;

&lt;p&gt;While this is a smaler component in terms of HTML tags than a Unit, it's less abstract, meaning, it has less use cases. Ideally, keep your specific elements to a minimum. You can even imagine that you have a budget like $1000 and each specific component will cost you $100. Why? Because the second you want to reuse some portion of your component, you have to create a new abstract one, then inherit it in your old specific component and in your new one. If you don't do it, you get repeated code. Here is an example:&lt;/p&gt;

&lt;p&gt;You create the &lt;code&gt;PDFDownload&lt;/code&gt; component, that should look just like a button. And you even use the &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; HTML tag. It looks like a button, it acts like a button, but it's a PDFDownload. Why? Why not just use &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt;? If you change the boder on all your buttons from 1px to 2px after a year, you will have to manually do the same for your &lt;code&gt;PDFDownload&lt;/code&gt; component and any other of it's kind.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Duplication is Evil&lt;/strong&gt;! The concept of duplication is mentioned in countless contexts, not just in development. Conversion Rate Experts wrote &lt;a href="https://conversion-rate-experts.com/duplication/"&gt;a great article on that topic&lt;/a&gt;, which looks into the problem from the perspective of information management and how that could even ruin a company. The same thinking is applied in front-end development. Write as little code as possible and try to keep your abstract components abstract and introduce as little specific ones as possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Page
&lt;/h2&gt;

&lt;p&gt;Pages are the same as specific elements, but in a much larger scale. Instead of the PDFDownload, which is just a button, a page holds &lt;strong&gt;everything&lt;/strong&gt; you see on the screen. A page is what we call the "Homepage" or "About" or "Contacts". You might ask, well, how is that a component?&lt;/p&gt;

&lt;p&gt;Remember, a component can be a single HTML Element or HTML Element that contains sub-components or children. What is the largest such HTML element you can think? The  one, for sure! Or more often the &lt;/p&gt; as it has all the structure that we care about. That is the reason why WordPress for example puts a class in the  with the page ID, page template, type of page etc. Because with that, you can target all Blog post pages for example or the About Us page and so on.

&lt;p&gt;And this is low on the list as it's even less reusable than the specific standalone HTML wrappers — you can have 50 PDF Download buttons, but how many About Me pages do you have? &lt;/p&gt;

&lt;p&gt;The lower we go on the list, the less styles you want to write for such components. So it's easy to say that it's usually a bad pracice to write styles for pages, because when you do that, you risk to end up with styles like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.page-template-about-me&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nc"&gt;.page-template-homepage&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nc"&gt;.post-1241&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nc"&gt;.page-template-resources&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;......&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maintaining that list of selectors above is a nightmare and it's easy to mess it up as it keeps on growing. Instead, try to break down your complex components into simpler ones, keep your components more abstract. Use modifiers and variations (more on that in an upcoming post).&lt;/p&gt;

&lt;h2&gt;
  
  
  Final words
&lt;/h2&gt;

&lt;p&gt;The list above is a guide to what types of components there are and how specific you should get when you create and reuse them. Every front-end developer might have a different naming for such components, might have a different view even and different best practices. This line of thinking is what helped me work on hundreds of projects, many of which were maintained and continue to grow 4-5 years after the first line of code was written.&lt;/p&gt;

&lt;p&gt;I have noticed that the lower on the scale I go, the harder it becomes to manage the project. Code smells like the selectors example above appear more often, patches with deeper selectors increase in frequency and overall, the joy of working on the project dies little by little. And I will be honest here, it's very hard to find motivation to work long-term on a bad codebase that is constantly patching old codebase with bad practices, that increases the specific elements and the only abstract ones are the oldest from when it was first build.&lt;/p&gt;

&lt;p&gt;It's a responsibility of the developer to keep up with the best practices during all development cycles. A couple of minutes more thinking about a good name, a short search on the existing components, a quick chat with other developers — it all adds up to a better development experience and lower cost for the clients.&lt;/p&gt;

&lt;p&gt;The instant gratification of fixing a bug quickly, commiting and deploying is a slippery slope. Doing that without improving the codebase afterwards results in the described scenario above — messy code with a ton of overwriting, specific classes that can't be reused or cases where you use the CommentForm component for a "Get a demo" page, just because your styles on the commenting form were so nice, that the client wanted the same for the main landing page that convers visitors to potential clients. Visually it's pretty, but in the code... well, it doesn't make much sense.&lt;/p&gt;

&lt;p&gt;Read next: &lt;a href="https://dev.to/xavortm/css-naming-conventions-nen"&gt;CSS Naming Conventions&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>frontend</category>
      <category>architecture</category>
    </item>
    <item>
      <title>What are Components in the front-end and why do we need them? </title>
      <dc:creator>Alex Dimitrov</dc:creator>
      <pubDate>Sat, 01 May 2021 16:25:47 +0000</pubDate>
      <link>https://dev.to/xavortm/what-are-components-in-the-front-end-and-why-do-we-need-them-2o2p</link>
      <guid>https://dev.to/xavortm/what-are-components-in-the-front-end-and-why-do-we-need-them-2o2p</guid>
      <description>&lt;p&gt;Let's first address the following problem: It's not super clear what a Component is. Well, many experienced developers will think of the same thing, but that is not true for those who are new to the Front-End world or haven't really though much about architecture of their codebase.&lt;/p&gt;

&lt;p&gt;One of the reasons is that the front-end field as a whole is big, there are many developers and leaders that name things differently and might not always agree to what is what. Different &lt;em&gt;things&lt;/em&gt; are refered with different names. &lt;/p&gt;

&lt;p&gt;For example, what is the difference between &lt;strong&gt;Component&lt;/strong&gt; and &lt;strong&gt;Element&lt;/strong&gt;? Or something else — when you hear Component, do you think about React? Or Vue? Or an HTML tag or tags?&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's define what is a Component
&lt;/h2&gt;

&lt;p&gt;A component can be thought of as a group of one or more HTML Elements. Elements are the regular HTML elements you can think of — &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;section&amp;gt;&lt;/code&gt; and so on. &lt;/p&gt;

&lt;p&gt;It's good to start simple, so we will look into a &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; component. This is your regular HTML Element, no React, no nothing. It will render a simple button on the page. Or would it?&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%2Fon7B7w1.png" 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%2Fon7B7w1.png" alt="https://i.imgur.com/on7B7w1.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The  element will render this small pill shaped box under the text.&lt;/p&gt;

&lt;p&gt;As you can see, it doesn't look good at all. To make it more useful, we can provide it with text to output inside by wrappin that string with the tag. If we want, we can also give it an &lt;code&gt;attribute&lt;/code&gt; like &lt;code&gt;disabled&lt;/code&gt; which will fade it out. All of this would result in:&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%2FLyrOy8S.png" 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%2FLyrOy8S.png" alt="https://i.imgur.com/LyrOy8S.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is all very simple and intuitive. What we really did was to &lt;em&gt;pass values to a function&lt;/em&gt;. That function in return &lt;em&gt;outputted a result&lt;/em&gt; which was the button you see above. That in of itself is what a Component does! So, from that, we can easily say that &lt;strong&gt;any HTML Element is also a Component&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Great, but that's not all, otherwise we wouldn't use two names for the same thing, right? A Component can also be created by combining more HTML Elements into one. Here is an example of a Card component:&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;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card-title"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card-description"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It has three HTML Elements, but it's one Component — Card. Do you see any React code there? No, because a Component is a concept that can be applied to many things. In the front-end world, this is mostly what you see above. Now, let's see the same component in the context of React:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"card"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"card-title"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"card-description"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;What it does is to provide you with the ability to output that markup by just calling &lt;code&gt;&amp;lt;Card /&amp;gt;&lt;/code&gt; And in the spirit of HTML Elements, you can also provide arguments that can modify the output, just like with the button example above. In the world of React, such arguments are called &lt;strong&gt;props&lt;/strong&gt; and are passed to the function via the props object. No need to go into details here, even if you haven't worked with React it's completely fine.&lt;/p&gt;

&lt;p&gt;So, to sum it up: A component is a group of HTML elements (or just one). That's about it. For libraries like React we can add more settings like passing props and running functions inside, but it is still a Component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we need components?
&lt;/h2&gt;

&lt;p&gt;This question is important to understand how to use them. In order to use a tool, you first need to know what the job is, right?&lt;/p&gt;

&lt;p&gt;The Front-End developer's job (aside from styling) is to provide the markup structure of an application or a website. Throughout my years of development, I've noticed that the markup is the most overlooked bit. Many developers ignore it and jump straight to styling and functionality. But, turns out that this creates a major problem. The markup if your app is it's skeleton. You can't really build properly without having a good structure that can be reused, modified, extended and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One of the core skills of a programmer is the ability to break down one large problem into many smaller problems and tackle them separately.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But, the trick is to be able to stich them together to solve that big problem. Your large application is your big problem. Break it down into smaller ones, by separating pieces of it into components. All these components then must work together. And the structure of combining them and reusing them is this important skill we need to develop.&lt;/p&gt;

&lt;p&gt;To think in Components in the context of HTML is like to think in Functions in the context of a program. You don't want it all dumped into one place, right?&lt;/p&gt;

&lt;p&gt;Breaking down the application into components helps with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a map of the application - it's easier to navigate and understand your building blocks from just reading class names.&lt;/li&gt;
&lt;li&gt;Scoping - you can style HTML Elements without the worry of breaking others. You don't really need CSS in JS to achieve that.&lt;/li&gt;
&lt;li&gt;Reusability - You can define your components once and then call them, by passing arguments to fill in the data. This works great with libraries like React, but it's also completely achievable with more standard PHP projects or HTML Templating languages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And together with everything above you open up the door to your teammates.&lt;/p&gt;

&lt;h2&gt;
  
  
  This is just the start
&lt;/h2&gt;

&lt;p&gt;If you found out the above as helpful, then please continue reading the other posts of the series as I will be getting into practical examples and guides that will help you structure your code. If you already knew everything there, I still believe that in my following articles you will find good tips or at least a different point of view, which will help you structure your codebase and reduce the time you spend on debugging, adding new code or maintaining the codebase.&lt;/p&gt;

&lt;p&gt;Read next: &lt;a href="https://dev.to/xavortm/the-component-types-in-front-end-development-2g0f"&gt;The Component Types in Front-End Development&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>frontend</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
