<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yahya Jamaldine on Medium]]></title>
        <description><![CDATA[Stories by Yahya Jamaldine on Medium]]></description>
        <link>https://medium.com/@yahyajamaldine?source=rss-d8e53ec8cde0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*B_jIwX8oWGxxjgnioTEhMQ.jpeg</url>
            <title>Stories by Yahya Jamaldine on Medium</title>
            <link>https://medium.com/@yahyajamaldine?source=rss-d8e53ec8cde0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 19:45:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yahyajamaldine/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Why Use a Database When You Can Use the Web Storage API?]]></title>
            <link>https://javascript.plainenglish.io/why-use-a-database-when-you-can-use-the-web-storage-api-b87be20e2f1b?source=rss-d8e53ec8cde0------2</link>
            <guid isPermaLink="false">https://medium.com/p/b87be20e2f1b</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Yahya Jamaldine]]></dc:creator>
            <pubDate>Fri, 16 Jul 2021 11:17:59 GMT</pubDate>
            <atom:updated>2021-07-16T11:19:21.877Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="cables connted to some hard disk drive." src="https://cdn-images-1.medium.com/max/1024/1*mOkPFygZViNwb7MRSyK2DA.jpeg" /><figcaption>Photo by: @<a href="https://unsplash.com/@kirill2020">kirill2020</a> on Unsplash.</figcaption></figure><p>Whenever we decide to save information on our web application, we think immediately about the database. If the information we are about to save, let’s say, concerns a piece of data, we may choose to use Session in PHP.</p><p>If you decided to use Session or database, you have to get the server in the equation. Using the server requires a backend language like PHP, Python, or a framework like Express.js, and that needs some extra work.</p><h4>Web Storage API</h4><p>To avoid storing a small amount of information via Session and database, we can rely on Web Storage API, as defined by our friends in Mozilla Developer Network:</p><blockquote>The Web Storage API provides mechanisms by which browsers can securely store key/value pairs.</blockquote><p>Web Storage act like a key-value data type in JavaScript, we have a property, each property has a value. Web Storage technique differs when it comes to one thing — it can stay intact when refreshing the page and even when closing the tab.</p><p>It’s a good idea to save data between pages or even when you close the browser without using a server.</p><p>Although we’re running from using a database, Web Storage still acts as a tiny database that stores data in your browser on a particular device. Using Web storage is not helpful when dealing with users that use multiple devices.</p><p>I learned about Web Storage last year when I used the localStorage mechanism, but I didn’t use Web Storage APIs tell 2021 in a recent project.</p><p>In my project, I needed to store a value selected by the user the stored value will determine in which language we should display the website.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PMithO9D2_zAHa9JJKvejQ.png" /><figcaption>Photo by the author</figcaption></figure><p>The image above represents a form that contains a select list. After choosing the desired language and clicking save, we’ll use Web Storage API to store the selected value (shown below).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xt7UvDwZ13tDcF0tNdgOUw.png" /></figure><p>As you see in the example above, after getting the selected value we used the localStorage mechanism from Web Storage API to store it under the name of “langaugetype”, though this may seem complicated if you do not know Web Storage API.</p><p>Now let’s take a look at how Web Storage API works.</p><h4>Using Web Storage API</h4><p>Web Storage API has two mechanisms: sessionStorage and localStorage. Both are used to storing data, but each mechanism has a unique use case.</p><h4><strong>SessionStorage mechanism</strong></h4><p>For sessionStorage we store the data only for a session. This mechanism stores the data until you close the browser/tab. we can say that it has an expiration date.</p><p>Developers rarely use sessionStorage, thusly, we’ll not dive into this mechanism. You don’t have to worry about how sessionStorageworks, we will concentrate on localStorage that works similarly.</p><p>If you are interested in sessionStorage you can check <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage"><em>Window.sessionStorage</em></a> in Mozilla Developer Network.</p><h4>LocalStorage mechanisme</h4><p>Data in localStorage doesn’t expire like sessionStorage. The data will persist even if you have closed the browser/window, the only way to destroy this data is through JavaScript or by clearing the browser cache.</p><p>To set data in the localStorageobject, we’ll use the setItem() method:</p><pre>localStorage.setItem(&#39;dataname&#39;, &#39;datavalue&#39;);<br> <br>//example</pre><pre>localStorage.setItem(&#39;birthday&#39;,new Date().toLocaleDateString());</pre><p>To get the data from the localStorage object, we can use the getItem() method:</p><pre>const birthday= localStorage.getItem(&#39;birthday&#39;);</pre><p>To get the value that you have stored locally, you need to pass its name to the getItem() method, this is identical to storing and getting data in Map.</p><p>If you no longer need the stored data, you can remove it using the removeItem() method:</p><pre>localStorage.removeItem(&#39;birthday&#39;);</pre><p>However, the Web Storage mechanism has limited storage that differs from one browser to another. If you decided to clean the occupied space you could use the clear() method.</p><pre>localStorage.clear();</pre><p>localStorage has some drawbacks like not being able to store data in any format other than string, you can get over this by using JSON.stringify() method to convert your data to a string, and JSON.parse() to reverse that.</p><h4>Finally.</h4><p>Whether you used sessionStorage or localStorage, these mechanisms of Web Storage API are very straightforward to use.</p><p>With Web Storage API and the fetch() method, you can push your SPA or single-page application to the next level without the need for an external party.</p><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b87be20e2f1b" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/why-use-a-database-when-you-can-use-the-web-storage-api-b87be20e2f1b">Why Use a Database When You Can Use the Web Storage API?</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[These 2 Methodologies Will Make You Rethink Naming Things in CSS]]></title>
            <link>https://javascript.plainenglish.io/these-2-methodologies-will-make-you-rethink-naming-things-in-css-cd15e2e3469c?source=rss-d8e53ec8cde0------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd15e2e3469c</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Yahya Jamaldine]]></dc:creator>
            <pubDate>Thu, 01 Jul 2021 02:07:55 GMT</pubDate>
            <atom:updated>2021-07-01T02:07:55.765Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>There are only two hard things in Computer Science: cache invalidation and naming things. – Phil Karlton.</em></h4><figure><img alt="A woman working on a computer" src="https://cdn-images-1.medium.com/max/1024/1*uRxEl50FaUrVgRYoJmvd_w.jpeg" /><figcaption>Photo by<strong> @surface </strong>on: Unsplash.</figcaption></figure><p>Frontend development is the most deceptive field in IT. When you get into it everything seems easy, you spend some time learning HTML, then you move on to CSS, you don’t even understand most of its principles but you move on to JavaScript.</p><p>Once you decide to build an interesting thing with HTML, CSS, and JavaScript, reality will hit you with the complexity of these things, especially CSS.</p><p>CSS is deceptive; its easy syntax hides the complexity of building a robust layout and responsive interfaces and even dealing with how to name things in CSS.</p><p>You may not take naming things in CSS seriously, but its importance reveals durable development and how to deal with old projects.</p><h4>How to name things in CSS</h4><p>Naming things in serious frontend project is important, take in consideration that other developer and even you in the future should understand how your CSS code impact each shrink in your markup.</p><p>Better CSS naming is all about using class selectors, a semantic one. Why use semantic class selectors?</p><p>Class selectors have a low specificity, which means they are easy to reuse in your project. If you don’t know what the heck is specificity, you can check an article of mine, where I explain it clearly.</p><p><a href="https://javascript.plainenglish.io/how-to-calculate-css-selector-specificity-72aa076c6a18">How to Calculate CSS Selector Specificity</a></p><p>I have mentioned the word semantic. When we use the word semantic, we refer to meaningful things. As with semantic HTML, a semantic class name means that the class names should describe what it does or which things it affects in your markup.</p><p>For example, you should not use these kinds of class names: red-text, blue-button, fontw-900 Why? It’s related to the existing design. Take into consideration that the design may change.</p><p>Using class=&quot;blue-button&quot; for primary action buttons works fine, but what if the primary buttons become brown? Now our class button is confusing and does not make sense. A better choice is to use a name like .primary-btn.</p><p>After this little talk about naming in CSS, let’s look at two methodologies for naming things in CSS. Each one has a unique use case to help you during development.</p><h4><strong>Block-Element-Modifier (BEM)</strong></h4><p>Block-Element-Modifier or BEM is a methodology for naming things in CSS, created at Yandex. The core idea of this methodology is to think of your website as a collection of reusable component blocks to build interfaces, just like the component in the library of React..</p><p>A block in BEM stands for a section in your document as a footer, header, or sidebar. A <em>block</em> can contain other blocks, for example, a section can contain two articles, a header may contain navigation, and a logo as depicted below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*FJsBKkys_xtJZ2yjA3pt3g.png" /><figcaption>Photo from <a href="https://en.bem.info/methodology/key-concepts/#element"><strong>BEM website</strong></a></figcaption></figure><p>With BEM, things don’t stop here. We can dig deeper into <em>elements</em> and <em>modifiers.</em> E<em>lements</em> are the underlying structure of a <em>block</em>.</p><p>The BEM documentation define an <em>element</em> as:</p><blockquote>A constituent part of a <a href="https://en.bem.info/methodology/key-concepts/#block"><strong>block</strong></a><strong> </strong>that can’t be used outside of it. For example, a menu item is not used outside of the context of a menu block, therefore it is an element.</blockquote><p>A menu <em>block</em>, for example, contains many menu elements (pictured below), the BEM methodology recommends not to use elements <a href="https://en.bem.info/methodology/faq/#why-not-create-elements-of-elements-block__elem1__elem2">within</a> other elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/1*4Oo7l0npbVwwX49UZwjb1A.png" /><figcaption>Photo from <a href="https://en.bem.info/methodology/key-concepts/#element"><strong>BEM website</strong></a></figcaption></figure><p>Based on the rules of BEM, <a href="https://en.bem.info/methodology/key-concepts/#block"><strong>block </strong></a>names must be unique within a project, an <a href="https://en.bem.info/methodology/key-concepts/#element"><strong>element</strong></a> name must be unique within a block, and for a block appearance that may vary based on the user preferences, use <a href="https://en.bem.info/methodology/key-concepts/#modifier"><strong>modifiers</strong></a><strong>. </strong><a href="https://en.bem.info/methodology/key-concepts/#block">Blocks</a>, <a href="https://en.bem.info/methodology/key-concepts/#element">elements</a>, and <a href="https://en.bem.info/methodology/key-concepts/#modifier">modifiers</a> are all called BEM entities.</p><p><strong>According to the Naming rules of BEM:</strong></p><p>block-name__elem_name--mod-name--mod-val</p><ul><li>Names are written in lowercase Latin letters.</li><li>Words within the names of BEM entities are separated by a hyphen (-). menu-bloc</li><li>The element name is separated from the block name by a double underscore (__). menu__item</li><li>The modifier name is separated from the name of the block or element by a double hyphen (--). menu--theme</li><li>The value of a modifier is separated from its name by a double hyphen (--). menu--theme--dark</li></ul><p>As usual, it’s much easier to see than to explain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6eUFZd2JNGq4_YJ3F0S8Gg.png" /></figure><p>In the example above menu — theme-dark and menu__item — theme — dark are additional class names, in case we have used the dark mode on a menu. Class selectors are the only selector used in the BEM methodology, but it’s safe to use child, descendant, and type selectors.</p><p>BEM methodology has advantages like:</p><ul><li>You can reuse Your CSS.</li><li>You won’t struggle with specificity because you are using low-level selector(class selector).</li><li>There’s no conflict between classes.</li></ul><p>For its disadvantages</p><ul><li>Class names can become a bit long and ugly</li><li>The HTML can have a lot of classes</li></ul><p>BEM may seem complicated at the very beginning. Once you try it out, you’ll love it as I did. For more about BEM, its advantages and disadvantages, you can check this <a href="https://mono.company/frontend/learning-to-love-bem/">great article by Johan Ronsse</a>, or by visiting the official website of <a href="https://en.bem.info/methodology/key-concepts/">BEM.</a></p><h4>Atomic CSS</h4><p>Another approach to name things in CSS is Atomic CSS, named and explained in “<a href="https://www.smashingmagazine.com/2013/10/challenging-css-best-practices-atomic-approach/">Challenging CSS Best Practices</a>” by Thierry Koblentz. Atomic CSS uses class selectors as BEM.</p><p>The naming system used by Atomic is very close to the one used by Bootstrap (see below). Most of the class names in Atomic CSS describe their use, but they don’t tell which content they affect.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OWmBM05VnF7b0SYz3TMh9Q.png" /><figcaption>An example of using Boostrap classes.</figcaption></figure><p>To illustrate how Atomic CSS works, we’ll create some CSS code and use it within an HTML markup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-IINKuB40QPkHOhc-vOvxQ.png" /></figure><p>Now we have a bunch of CSS classes to use in our markup. In short, each class represents a CSS property. That’s why we have a lot of CSS code. The use in our markup will be like this:</p><p>&lt;p class=&quot;bw-2 bss p-1x sans tc-w br-1x bg-e bc-e&quot;&lt;/p&gt;</p><p>The markup we have used will create a message box that shows an error. We have used a lot of class names here. But it worth it when you realize that turning that message box from an error box to a warning box requires changing one letter.</p><p>&lt;p class=&quot;bw-2 bss p-1x sans tc-w br-1x bg-w bc-w&quot;&lt;/p&gt;</p><p>As you can see we have just changed the letter eby w in bg- and bc- to turn an error message box into a warning message box.</p><p>Atomic CSS has some advantages like:</p><ul><li>Avoiding conflicts by using a low-specificity selector.</li><li>Creating highly reusable classes.</li><li>You know what you are going to change because each class represents a style, it’s straightforward to remove or add a class.</li></ul><p>For its disadvantages, Atomic CSS is a mess. It feels like using the style attribute from the early days of the web. The major problem with Atomic CSS concerns separating the content from the presentation.</p><p>If class=&quot;gray&quot; change the text color to be gray, what do we do if we decided to change everything with a.gray class to be black?</p><p>We can remove .gray and add another class for example .black, But now we are changing our HTML and breaking the rule of<a href="https://learn.co/lessons/separation-of-content-and-presentation"> <em>separating the content from the presentation</em></a>.</p><h4>What Should I use Atomic CSS or BEM?</h4><p>It’s obvious Atomic CSS works for those who prefer to write an arsenal of CSS rules then apply them to the markup, a single developer, or a small team that knows each rule.</p><p>BEM works for large teams. The naming system in this BEM is descriptive. Most developers in the team will figure out what each class does and which kind of things it affects.</p><h4>Finally</h4><p>We have come a long way to understand how to name our CSS code. We can use semantic class selectors or the BEM or Atomic CSS methodology. Each one of these methodologies has its unique naming system.</p><p>It’s up to you now to decide if you want to use Atomic CSS, the BEM methodology, or none of them.</p><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd15e2e3469c" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/these-2-methodologies-will-make-you-rethink-naming-things-in-css-cd15e2e3469c">These 2 Methodologies Will Make You Rethink Naming Things in CSS</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[6 Useful Resources to Sharpen Your Skills as a Front-End Developer]]></title>
            <link>https://javascript.plainenglish.io/6-useful-resources-to-sharpen-your-skills-as-a-front-end-developer-9ea9836ca86a?source=rss-d8e53ec8cde0------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ea9836ca86a</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Yahya Jamaldine]]></dc:creator>
            <pubDate>Sat, 19 Jun 2021 20:07:48 GMT</pubDate>
            <atom:updated>2021-06-19T20:07:48.332Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aquQwFrT9WARFqwizQ4MBw.jpeg" /><figcaption>Photo by <strong>@benchaccounting </strong>on: Unsplash.</figcaption></figure><p>Staying up to date with the latest trends in front-end development and sharpening our skills is one of the liabilities that we have as developers, you are not doing that just to become the cool developer in the room or on Twitter, you’re doing it typically to keep your job alive and your economic value high.</p><p>Companies and start-ups will change their stack if that is going to impact the budget of the production, the number of hired engineers, or the product in a good manner, and we can take as example Coinbase and how they switched to building their mobile application in React Native lately.</p><p>As a front-end developer, you are risking your job by not sharpening your skills, and not staying up to date, because this field has libraries, frameworks and tools, and things to learn every day more than any other field.</p><p>Thusly, I listed some resources that have been my source of information over the last year to learn new things and challenge myself in front-end development.</p><h4>1. Frontend Mentor</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XWLIXNx3W-qvuwBEbLVuzQ.png" /><figcaption>Photo by the author from Frontend Mentor website.</figcaption></figure><p>As developers, we struggle a lot with starting projects from scratch why? You can’t build what you can see or imagine. Everything should start with a previous plan of what you are going to build.</p><p>Frontend Mentor by far is one of the most websites that I found very useful for front-end developers who just learned the essential things on the web, in Frontend Mentor you’ll find various challenges for coding design projects.</p><p>The entire process in Frontend Mentor is like this: you pick a web design challenge, you code it, then you submit your solutions, your code or solutions will be public, so other developers can give you feedback.</p><p>What’s more interesting about Frontend Mentor, they provide you with tips that will help you in solving the given challenges.</p><h4><strong>2. HackerEarth</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HMPexcLg8qVdntsyxQbHrg.png" /><figcaption>Screen by the author from Hacker earth website.</figcaption></figure><p>HackerEarth is one of the websites that I have come across early in my days while learning to code, it’s a website where you can find thousand of challenges to develop your solving problems skills, and that’s by learning more about the basic of programming, algorithms, data-structure.</p><p>One of the amazing things that you’ll find in HackerEarth, is how they organize hackathons and competitions where you can test your skills and earn prizes.</p><h4>3. Alligator.io</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x_z57Zpp2qAQ4hLX2Yabwg.png" /><figcaption>Screen by the author from Alligtor.io website.</figcaption></figure><p>Whenever I get into a conversation about front-end development and how you can improve yourself in this field, I can’t literally get off without mentioning <a href="https://medium.com/Alligtor.io.">Alligtor.io.</a></p><p>This website is a savior for me, each time I stuck with understanding a new feature in React, or CSS I head up to Alligator, the authors there explain things like you are five.</p><h4>4. CSS-TRICKS</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JniY2mP5lEAUS1cAeSPkGw.png" /><figcaption>Screen by the author from CSS-TRICKS website.</figcaption></figure><p>If you are familiar with reading online articles about front-end development, I assume that you have already read some articles from CSS-TRICKS, this website started in 2007, it’s a kind of legacy website for front-end developers.</p><p>Each time I struggle with adding a feature to a project I head up to CSS-TRICKS, it contains thousands of articles about how to implement tricks and tips primarily in CSS, but also there are some articles about HTML and JavaScript</p><h4>5. Smashing Magazine</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JK5MVWdkwy05bFr0fTQTJw.png" /><figcaption>Screen by the author from Smashing Magazine website.</figcaption></figure><p>Smashing Magazine is like CSS-TRICKS and Alligator.io, except that it’s more inclusive, the platform contains thousands of articles about UI/UX design, tips in CSS and HTML, and how to add amazing features to projects in React, Vue.js, and other frameworks.</p><p>Furthermore, you’ll find some interesting articles about advice on how to find a job in the tech industry or how you can elevate yourself as a developer.</p><h4>6. Frontend Practice</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aN2uJSBEGASCRWBRAM_yAg.png" /><figcaption>Screen by the author from Frontend Practice website.</figcaption></figure><p>Frontend Practice is based on the same idea as Frontend Mentor. They provide you with an existing design, and your job is to make it happen, by coding it..</p><p>Frontend Mentor contains various challenges to code designs, by contrast, Frontend Practice challenges are all about building landing pages.</p><p>This website will provide you with everything from colors to CSS features that you will use in your project, and that will include photos and fonts soon even If you didn’t have any ideas about the used CSS features, the website will provide you with resources to learn them.</p><h4>Conclusion</h4><p>These are some resources that I’m using to sharpen my skills as a front-end developer, I hope you find them useful. There’s no limit to what you can or where you can learn about web development, just do not stop learning.</p><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ea9836ca86a" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/6-useful-resources-to-sharpen-your-skills-as-a-front-end-developer-9ea9836ca86a">6 Useful Resources to Sharpen Your Skills as a Front-End Developer</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learn about Ajax and How to Make Your First Server Request in JavaScript]]></title>
            <link>https://javascript.plainenglish.io/learn-about-ajax-and-how-to-make-your-first-server-request-in-javascript-dc332f406d39?source=rss-d8e53ec8cde0------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc332f406d39</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Yahya Jamaldine]]></dc:creator>
            <pubDate>Fri, 14 May 2021 14:02:38 GMT</pubDate>
            <atom:updated>2021-05-14T14:02:38.044Z</atom:updated>
            <content:encoded><![CDATA[<h4>The fetch method in JavaScript.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9BRXTuC0y1UcWNxd" /><figcaption>Photo by <a href="https://unsplash.com/@clark_fransa?utm_source=medium&amp;utm_medium=referral">Arnold Francisca</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Ajax History</h3><p>Creating dynamic websites without refreshing the entire page and pop-up windows was the door to celebrity or fame for JavaScript. The fundamental concept behind creating dynamic pages was first developed by Microsoft when they introduced the XMLHttpRequest object in Outlook in 1998.</p><p>In 2004 and 2005, Google pushed things to the next level for dynamic websites by introducing Gmail and Google maps, and this time they used Ajax. Jesse James Garrett coined the term Ajax, and it stands for Asynchronous JavaScript and XML.</p><p>The major goal for Ajax was loading data from the server to the browser without a visible refresh. To use Ajax, you’ll need to use the following technologies: JavaScript, XMLHttpRequest, the DOM, and Textual data that could be XML, HTML, or JSON format.</p><h3><strong>The Fetch Method in JavaScript</strong></h3><p>Before the jQuery library or the fetch method, making an Ajax request required a little hassle from developers using the XMLHttpRequest object in Ajax wasn’t easy, you can take a look on <a href="https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started"><strong>the Mozilla Developer network</strong></a> to see what I’m talking about.</p><p>The jQuery library solved the problem of XMLHttpRequest object by introducing the $.ajax() global function. However, using an external library wasn’t pleasant for a lot of developers.</p><p>To end all this headache, the Web Hypertext Application Technology Working Group (WHATWG), come up with a solution that works across the web platform: <a href="https://fetch.spec.whatwg.org/"><strong>the fetch() method</strong></a>.</p><p>The fetch method takes one required argument, and it’s the URL or the path that you want to fetch data from, supplying more arguments is optional. When you call fetch with an URL, it’ll return a Promise, thusly, we’ll need to handle the response by passing a call back to the then() method.</p><p>There’re many URLs or resources out there to fetch data from, but we are going to build an endpoint locally that contains some data, thus, we’ll need a JSON file.</p><p>We’ll build a simple Html interface to fetch user information from a JSON file with JavaScript. Our JSON file will continue the following information, an object with some properties inside, our goal will be fetching the value inside each property.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0bd1b192e803ae4737fb3a829c361c7f/href">https://medium.com/media/0bd1b192e803ae4737fb3a829c361c7f/href</a></iframe><p>The UI to show the user data will be as depicted below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vAkyzx1FtL-eNN8PeDm9nQ.png" /></figure><p>The HTML markup for the example above:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80d205bc0d8b2ebe2c55e20bc29cb2d1/href">https://medium.com/media/80d205bc0d8b2ebe2c55e20bc29cb2d1/href</a></iframe><p>At the end of the article, I’ll include a direct link to a GitHub repository containing all used files.</p><p>In that spirit, return once again to our HTML file, inside the script tag we have started by defined some constants, the first one contain a reference to the button that’ll show the fetched data, the other constants contains a reference to each input field we have used in our document.</p><p>The event handler or the call-back function for the click event will use the event object to prevent any default behavior by using the preventDefault() method, and last, we have the fetch method.</p><p>The fetch method will take the name of the previous JSON file as an argument. Passing one argument to fetch means we’re making A GET Request if we made a POST request we’ll need to supply more arguments with specified options about the request.</p><p>Calling the fetch method in our example we’ll return a promise that’ll resolve a response, therefore, we’ll need to handle the response using the then() method, in the previous example if you look closely at the fetch() method you’ll realize that we’ve used two then() methods.</p><p>The returned result by fetch will continue some extra information like the body, headers, the status code that indicate if the request succeeded or not, in our situation we need just the information inside the body, to do that we’ll use the json() method, this method returns a promise too.</p><p>The second then() method will handle the response return by the json() method, the result will be the main information inside our JSON file, and it’s an object that contains some properties, we’ll assign each property to the value of the desired input field as you can see below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cyvAh0JGVTkqnXg74pqoIA.png" /></figure><h3>Finally</h3><p>The fetch method took a lot of burden from JavaScript developers by giving them a suitable way to make Ajax requests without using an external library or interacting directly with the XMLHttpRequest object.</p><p>Fetch doesn’t serve us just in making a GET request to the server, we can use it to make a POST request too, however, doing so we’ll need more argument with specified options.</p><p><em>More content at </em><a href="http://plainenglish.io/"><em>plainenglish.io</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc332f406d39" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/learn-about-ajax-and-how-to-make-your-first-server-request-in-javascript-dc332f406d39">Learn about Ajax and How to Make Your First Server Request in JavaScript</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[9 JavaScript Concepts You Should Know Before Learning React]]></title>
            <link>https://javascript.plainenglish.io/9-javascript-concepts-you-should-know-before-learning-react-316069f54ce4?source=rss-d8e53ec8cde0------2</link>
            <guid isPermaLink="false">https://medium.com/p/316069f54ce4</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Yahya Jamaldine]]></dc:creator>
            <pubDate>Mon, 10 May 2021 09:56:12 GMT</pubDate>
            <atom:updated>2021-05-11T19:04:16.029Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yrtLYFNQ3AuKb7to" /><figcaption>Photo by <a href="https://unsplash.com/@flowforfrank?utm_source=medium&amp;utm_medium=referral">Ferenc Almasi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The syntax of JavaScript has changed a lot since its release in 1995, and so has its uses. The language went from being a scripting language for the web to a language that affects lots of domains in the IT industry.</p><p>Technically, the syntax of JavaScript saw its biggest change after the release of ECMAScript 5 and 6. This release brought the so-called modern JavaScript into being, a new syntax that’s full of dots, arrows, and classes.</p><p>I’m proud to be one of those developers who finished an entire book and tutorial about JavaScript, and then figured out that they must learn modern JavaScript.</p><p>The old syntax is still valid, but modern JavaScript libraries like React have emphasized a lot on using ES6, or modern JavaScript syntax for those who want to get most out of the library. <a href="https://reactjs.org/docs/react-without-es6.html">Although you can still use React without ES6.</a></p><h3>JavaScript And React</h3><p>If you head to the official website of React or any book or tutorial that teaches it, you’ll notice the existence of previous knowledge in ES6 in the request — and I’m not saying that in a naïve way, it’s something that you’ll see inevitably.</p><p>Check out any React code and you’ll see new keywords and symbols like let, const, arrows, classes, along with objects,and arrays. Thus, I listed some concepts that you must know before learning React.</p><h3><strong>1. Variables and assignment</strong></h3><p>You might think that declaring variables isn’t worth mentioning but it is for JavaScript. Before ES6, the only way that you could declare variables was by using the var keyword, and that caused some problems.</p><p><strong>Declaring variables with const</strong></p><p>Regular things like constant didn’t exist in JavaScript, but now it does. Using the const keyword means the values of the variables won’t change. However, that’s not the case for arrays, objects, and other collections.</p><p>Declaring an array or object with the const keyword doesn’t make their elements immutable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zC7tBVRQubct5AH8GPmqPQ.png" /></figure><p><strong>Declaring variables with let</strong></p><p>Besides not maintaining the values of variables, the other problem that used to upset JavaScript developers when using the var keyword was the scope of variables. You can consider scope as the place where the variable can be accessed.</p><p>Declaring two variables that have the same name, one that lives inside an if/else block, and the other outside of the block should not cause any problem. This rule doesn’t apply to variables that are declared with the var keyword as depicted below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eNWSF1Ow6RfazzCCewnwHA.png" /></figure><p>If we replaced the declaration inside the block with let or const, we’ll get something else, because letand constscope the variable to its block. In other words, any variable declared inside a block belongs only to that block.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xQe9iLyFScoe9bWl0kvPvA.png" /></figure><p>There are other interesting situations when it comes to using let. Just covering these will require an entire article. So, let’s save time and space for other concepts. If interested, you can visit a provided link in the resource section at the end of this article.</p><h3>2. Template Literals</h3><p>One thing that’ll upset you while constructing an HTML markup in JavaScript, is the fact that you should concatenate variables with strings surrounded by quotes. Two or three lines of HTML maybe fine but imagine 20 lines?</p><p>You’ll have to deal with endless headaches of closing and opening quotes and lots of the + operator. PHP developers didn’t have to deal with that, since they could write variables inside two doubles quotes with no burden.</p><p>ES6 gave this gift to JavaScript developers by introducing template literals. It depends on two things: first, a template literal is surrounded by backticks (``). Second: variables inside it are surrounded by a dollar sign “$”, and curly braces {}, like this ${element}.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I5QkPdC76cAULce-ND0ywA.png" /></figure><h3>3. Arrow Functions</h3><p>Writing functions in JavaScript is a lot of work. And that may sound weird to you because you’ve been doing that work without even knowing. Imagine with me writing a function by using just an arrow? Like this one “=&gt;”.</p><p>We have replaced the keywords “function” and “return”, parentheses “()”, curly braced {}, with an arrow and “=&gt;”, do you see how much burden this arrow has taken off our shoulders? However, there are some rules to follow while using it.</p><p>First, an arrow function must be used as an anonymous function, so it should be assigned to a variable, as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7aX3PhHMcSNLZUW02O_ksg.png" /></figure><p>You may notice that we didn’t omit the parentheses. One of the other rules of arrow functions is that when you have over one parameter, you must use parentheses. Moreover, if the function body contains more than one instruction, you must wrap the body of the function inside curly braces {}.</p><p>The power of arrow functions shines when we use them with array methods. You’ll see that in the array section.</p><h3>4 ES6 Classes</h3><p>Knowing that JavaScript doesn’t have classes shocked me. I was so eager to learn the OOP (object oriented programming) approach and see how to use classes. However, before ES6, there were no classes. So, I learned OOP through creating an object regularly or through using a constructor function.</p><p>Now the language has its own class system that looks familiar to any class system in languages like C++, PHP, VB.net. Nothing has really changed. Everything you learned about objects and prototype inheritance is still the same, except that we have a fresh way to declare the structure of objects using the syntax of classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nqSwSxSjV24jHgHB6yp2cg.png" /></figure><p>In the example above we have created a House class that contains two properties door, window, and a method that shows the values of these properties. After that, we created an instance of the House class.</p><p>Understanding classes is a must-know for anyone who wants to learn React. Personally, the first time I used React, I encountered classes, because you’ll usually create React components through extending an existing class.</p><p>Although there’s a new approach to creating components in React through functions, and React developers are advised to use it. It’s much easier than creating class-based components.</p><h3>5. Arrays, objects and other data collections</h3><h4><strong>Arrays</strong></h4><p>The array structure hasn’t changed. What has changed is the number of arrays methods to manipulate each item in the array. For example, we have: map(), filter(), find(), forEach(), reduce(), match(). Each one has a unique use-case.</p><p>You’ll encounter the map method a lot in React, practically when mapping each item in an array to an &lt;li&gt; element. The map method will give you the ability to iterate over each item in the array plus changing its shape. As an example let’s multiple each element in an existent array with two.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6oizQ9SOyjsj94JJmNdNUg.png" /></figure><p>As you see above, iterating over the array and changing its items is much easier with the map method, than the old way with the for loop. If you look closely, you’ll see that we have used an arrow function as a callback to the map method. Arrow functions do their best job when used with arrays.</p><h4><strong>Objects</strong></h4><p>Objects now have some awesome methods that’ll take away all the burden of dealing with the structure of objects. As an example we have :</p><p><strong>Object.values()</strong>: this method will return an array that contains each value in the object.</p><p><strong>Object.keys()</strong>: it’ll return an array that contains the name of each property in the object</p><p><strong>Object.assign()</strong>: this method is very powerful, it’ll give you the ability to update an object with properties from another object while preserving the old properties</p><h4>Other data collection</h4><p>Before ES6, the conversation about data collection was limited to objects and arrays. Now the list is growing. We have Map (don’t confuse it with the map() method), Set, WeakMap and WeakSet. You’ll not be using these collections often, but it’s good to know about them.</p><h3>6. Destructuring objects and arrays</h3><h4>Destructuring objects</h4><p>Most of the time, we use constructors to create objects and put data inside them. What about getting data from an object without the traditional way of using the object property or the array notation? In this case, we are going to use the destructuring assignment. Let’s see how it works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jZQgPZtJm8QIY4bth9e9kw.png" /></figure><p>As do you see in the example above, we have created an object that contains four properties, then we pulled the value of the color and memory property using destructuring.</p><p>To make the destructuring assignment work, the name of the variable you are assigning the value to should match the name of the desired property in the object in other words an assignment like this: const {size}= phone;will give you an undefined value.</p><h4>Destructuring arrays</h4><p>For arrays, the syntax stays the same. And fortunately, we don’t need to know the name of anything, because arrays don’t contain any keys. You need just to respect the order of each item in the array, so you can get the desired value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8ognOvu5b-Iw58wVEgMGtg.png" /></figure><p>In the example above, we cannot get the value of the second item in the array without getting the value of the first. If we needed just the fourth value, we would have to create three unnecessary variables. Fortunately, we can use a comma instead of writing names of variables as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FTRm7JbkktGEjO3PHLT20g.png" /></figure><h3>7. Default Parameters</h3><p>When it comes to a function, there’ll be some situations when the function should get three parameters, but it received just two parameters. In a normal situation, this will throw an error or undefined, but with a default parameter, the default value will be used instead.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2xYdbC9RKhv2406E7Ld5Fg.png" /></figure><h3>8. The Spread and Rest operators</h3><h4>The Spread operator</h4><p>As I mentioned early on, the syntax of modern JavaScript is full of dots. Why? Well, that’s because of the spread operator. It becomes widely used with variables. Thus, you’ll see it probably in every ES6+, or modern JavaScript file.</p><p>You’ll use the spread operator to convert an array to a list. In other words, you’ll spread the items of the array. Moreover, you’ll use it with other collections like Map, and with functions too. Let’s see how it works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mom_yfDmeDJH9AKMhYCcaw.png" /></figure><p>In the situation above, we have concatenated the <em>vehicle array</em> with <em>the items array</em> and that’s by using the spread operator. If you attempt to find out the index of the item “car”, without using the spread operator you’ll get -1, in short, it doesn’t exist.</p><p>We can use the spread operator with destructuring assignments too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OorFVhrTbe-elTe31UbrTg.png" /></figure><p>What’s more interesting about the spread operator is that we can use it to manipulate the values inside an array, without mutating the original array.</p><h4>The Rest operator</h4><p>The three dots syntax of the spread operator will serve us when using the rest operator, and this one works when we don’t know how many arguments the function should get. So, we’ll just use the spread operator syntax: three dots followed by the parameter name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gUonbSCYrzewwlhYFXKeLg.png" /></figure><h3>9. Modules</h3><p>Modules — a JavaScript file that exports its code for another file to use. And we know that when you export something, you should import it. The syntax of exporting and importing data will vary and that’s based on your preference. For exporting data, you can use named exports or default exports.</p><h4>Default export</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2HqhvrXTEWqgJ825mZS2ug.png" /><figcaption>file name: <strong>prototype.js</strong></figcaption></figure><p>Above, we have created a module that exports a class named Junior. This class has 4 properties. The first properties will get their values when creating an instance with a constructor, the last ones will have default values. What matters here is that we have used the export and default keywords to export this class.</p><p>Exporting a module data using the default keywords (default export) means: the data will be imported, no matter which name we have used to import it. Let’s show this in the example below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cw9kbUv9EO-9KuVM8ICvRw.png" /></figure><p>Notice how we imported the class from the prototype.js file with a different name, and how that didn’t cause any error.</p><h4>Named export</h4><p>As it seems, named export will export data based on its name. You need to just remove the default keyword, remember the name of the data when importing it, and using the curly braces to wrap the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ulnWgQEGC_s34C6AQU6K7w.png" /><figcaption>file name: <strong>prototype.js</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-wdgkQ4kgCqifiao4gAzfA.png" /></figure><p>You can use the export and import statement based on what you want to export and how you want to export it. And by the way, object destructuring is involved here. I have already written an article about that. If interested, you can check out the link below:</p><p><a href="https://javascript.plainenglish.io/object-destructuring-and-integration-with-modules-in-javascript-391ca0434ea9">Object Destructuring and Integration with Modules in JavaScript</a></p><h3>Final words</h3><p>I have tried to give you an overview of each concept that you may encounter while trying to learn React, or even Angular. Coming across these concepts is something inevitable. You must strengthen and update your syntax knowledge about JavaScript.</p><p>For each concept, I confess that I barely scratched the surface with one example. So I’m providing further resources for you.</p><h3>Resources:</h3><ol><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements#declarations"><strong>Variables and assignment</strong></a></li></ol><p>2.<strong> </strong><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals"><strong>Template Literals</strong></a></p><p>3. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions"><strong>Arrow Functions</strong></a></p><p>4. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class"><strong>ES6 Classes</strong></a></p><p>5. <strong>Arrays, objects &amp; other data collections.</strong></p><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"><strong>Arrays</strong></a></li><li><a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics"><strong>Object</strong></a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects#keyed_collections"><strong>Other Data collection.</strong></a></li></ul><p>6. <a href="https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/"><strong>Destructuring objects and arrays.</strong></a></p><p>7 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters"><strong>Default Parameters.</strong></a></p><p>8. <a href="https://javascript.info/rest-parameters-spread"><strong>The Spread and Rest operator.</strong></a></p><p>9<strong> </strong><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules"><strong>Modules.</strong></a></p><p><em>More content at </em><a href="http://plainenglish.io/"><em>plainenglish.io</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=316069f54ce4" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/9-javascript-concepts-you-should-know-before-learning-react-316069f54ce4">9 JavaScript Concepts You Should Know Before Learning React</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create Native Modals in HTML with Example]]></title>
            <link>https://javascript.plainenglish.io/create-native-modals-in-html-with-example-242b465be938?source=rss-d8e53ec8cde0------2</link>
            <guid isPermaLink="false">https://medium.com/p/242b465be938</guid>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[html]]></category>
            <dc:creator><![CDATA[Yahya Jamaldine]]></dc:creator>
            <pubDate>Sat, 08 May 2021 19:13:46 GMT</pubDate>
            <atom:updated>2021-05-08T19:13:46.603Z</atom:updated>
            <content:encoded><![CDATA[<h3>Native Modals in HTML with Example</h3><h4>Part 2: styling the modal &amp; the main button.</h4><figure><img alt="A guy writing some code" src="https://cdn-images-1.medium.com/max/1024/1*F_5ve5Bf9Q6V2Cy-MarDTw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@trueagency">@trueagency</a> on: Unsplash</figcaption></figure><p>In the first part of this article about native modals or the &lt;dialog&gt; element in Html, we have talked a little about the definition of modals, their underlying utility, plus how they belong to alerts and not to notifications as we may think in the first while.</p><p>After that, we have built a simple modal using the dialog element in HTML. If you followed with me to the end of the article, you’ll notice that I mentioned how this modal (the one we have built) is ugly and need to be styled. So in this article, we’ll try to style our modal, thusly we’ll do some CSS stuff.</p><p>You can check the first part of this article at the link below:</p><p><a href="https://javascript.plainenglish.io/native-modals-in-html-with-example-e280a91d3d3a">Native Modals in HTML with Example</a></p><h3>Styling the main button</h3><p>When it comes to code, the first thing we have started within the first part was the button that will trigger the opening of the modal, let’s parcel our work by styling this button first, and the bulk of the work will be on styling the modals.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2gkmDqrYR0SYWNio.png" /></figure><p>Well, this button above is pretty ugly, right? Let’s start by defining some variables, in short, CSS custom properties, we’ll use them to store some style’s values that are going to be used over and over, we’re going to use these variables globally, so we better have them under the root pseudo-class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uAcL4TKy5SLENhGfJaKV1w.png" /></figure><p>Now let’s use these variables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9_FEQzH4-q-ESzmW6oSDlQ.png" /></figure><p>We haven’t done any interesting thing tell now, let’s change the content of the button, then we’ll apply some interesting things to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ay3CyChj6BTHsj6moowB3g.png" /></figure><p>After these changes we’ll get something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G10KGUg9lkWpulYvoeiTOg.png" /></figure><p>A light-purple background, a tiny button with a text and emoji inside, that’s not enough, let’s use some magic..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hnKVBXyeOLVccwgmpZ_skw.png" /></figure><p>Now our button should be like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SMuscfMpTx75hvl0JMUh3A.png" /></figure><p>Everything is clear for our style declaration except for the units that we have used, and the box-shadow property. We have used the rem unit for the font size, so each time we change its value other properties that have the em unit like the padding will adjust with that.</p><p>The white curve that you see on the right side of the border has been specified using the box-shadow property, this property takes up to five values: [horizontal offset], [vertical offset], [blur radius], [spread radius], [color]. In our CSS declaration, we have specified just the horizontal offset and the color.</p><p>The horizontal offset of the box-shadow will change based on the sign of its value. If it’s negative, the shadow will be on the left side, and if it’s positive it will be on the right side.</p><p>For the color of the box, we gave it a white color, based on these CSS declarations, our button will tend to have a switch behavior.</p><p>To gain this switching behavior, we will need to add some last touch on the button when we hover over it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GVse9SSmLM_v-jtY7v7Ihg.png" /></figure><p>The style above will move our button to the right side and that’s by increasing the left margin of our button, then we will override the box-shadow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_zY6Awnvq7iDDnzezaKtJQ.png" /></figure><h3>Styling the dialog</h3><p>Clicking on the button in the last example will trigger the opening of the modal, and we’ll end up with something like this in the picture below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZbREOkqmBoN-cVk8wdUIHg.png" /></figure><p>I have changed a little bit the markup of the dialog, but it’s still pretty ugly!! Let’s add some magic again to the equation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-hlxd8TLefzx6ZnwC-7Uzg.png" /></figure><p>The only thing that you have to watch out for here is the :<em>:backdrop pseudo-selector</em>, it’ll give us the ability to style the backdrop for the modal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FfXiZnfjvPXGLC0_irpsSQ.png" /></figure><p>Finally, we’ll add some styles to style the button inside the dialog.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z31J_cbPds1oyZUKye7HJg.png" /></figure><p>The style above replicates the same CSS declaration we have used in our first button, but we changed some values and we get rid of the box-shadow property. The ultimate result will be like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SKanhBhfVSl0b51TyMfdqQ.png" /></figure><h3>Browser support</h3><p>At the time of writing this article, only Chrome fully supports the dialog element, browser like Firefox and Opera has support behind the flag, you can check the link below for more information:</p><p><a href="https://caniuse.com/?search=dialog">https://caniuse.com/?search=dialog</a></p><h3>Conclusion</h3><p>We have not done so much with our modal, in a real project, you’ll use modals or the dialog element to get useful information from the user using the input-form as an example, or to ask him about certain things that are related to privacy like cookies.</p><h4>Resources</h4><p>The full example to build the modal in Codepen: <a href="https://codepen.io/yahyajamaldine/pen/KKWwQqK">https://codepen.io/yahyajamaldine/pen/KKWwQqK</a></p><p>The box-shadow property explained: <a href="https://css-tricks.com/almanac/properties/b/box-shadow/">https://css-tricks.com/almanac/properties/b/box-shadow/</a></p><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=242b465be938" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/create-native-modals-in-html-with-example-242b465be938">Create Native Modals in HTML with Example</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Native Modals in HTML with Example]]></title>
            <link>https://javascript.plainenglish.io/native-modals-in-html-with-example-e280a91d3d3a?source=rss-d8e53ec8cde0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e280a91d3d3a</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Yahya Jamaldine]]></dc:creator>
            <pubDate>Thu, 06 May 2021 01:08:23 GMT</pubDate>
            <atom:updated>2021-05-13T17:58:04.238Z</atom:updated>
            <content:encoded><![CDATA[<h3>Create Native Modals in HTML with Example.</h3><h4>Part 1: Introduction to the dialog element.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eEvZS4qQL4EoS7kG" /><figcaption>Photo by <a href="https://unsplash.com/@florianolv?utm_source=medium&amp;utm_medium=referral">Florian Olivo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Confirmations or modals are one of the best approaches to get the attention of the user. In fact, they are useful not just for getting their attention but we can go ahead and block their interaction with the website until they acknowledge the modal itself.</p><p>Any modern web application should use modal to inform users about important information. However, it’s hard to convince web developers to use modals or notifications. Most of them still think that they are for mobile platforms.</p><h3>Alerts and Notifications</h3><p>Modals are a part of alerts though you might think they are a part of notifications. There’s a small difference between alerts and notifications. So, you need to understand when to use alerts and when to use notifications.</p><p>Alerts are used when something important happened and you need to ensure that the user acknowledges that. You can move further by getting a piece of information from the user when the modal or the alert pops up.</p><p>For notifications, one can think of them as a thing that happened, but it’s not important enough to block the user until they interact with it.</p><h3>Native Modals in HTML</h3><p>Using a modal wasn’t an easy thing to do in HTML. It required a lot of work — from building the structure with HTML to styling it with CSS and controlling its behavior with JavaScript.</p><p>Many libraries and frameworks introduced some handy ways to use modals in web applications, for example, Bootstrap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VO_nmfblIWPhZ8zBWaPEPg.png" /><figcaption>Photo from the official website of bootstrap</figcaption></figure><p>Bootstrap modals are good. A few div elements, some classes and you end up with a beautiful modal to use in your website. However, not everyone wants to use an external library in their website. On the other hand, no one wants to do some extra work with HTML, CSS, and JavaScript just to add an element to improve the UX.</p><p>The HTML 5.2 spec solved this problem by introducing the &lt;dialog&gt; element, and they defined it as a part of an application that a user interacts with to perform a task. For example, a dialog box, inspector, or window, as depicted below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1zFetWPM30Pg7HnMfmoORw.png" /></figure><p>The code in the picture above is very clear. It doesn’t contain any new element except for the dialog element. Open this basic markup in the browser and you’ll see a blank page. To open the dialog element we need to add JavaScript code in the equation. Below you’ll find a complete use of the dialog element.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa5942e1349fbee5708226125deeaf32/href">https://medium.com/media/aa5942e1349fbee5708226125deeaf32/href</a></iframe><p>Well, don’t get scared. There’s nothing complex in this code. Let’s start with the markup. Firstly, we have a division that contains a button, it’s very clear that we are going to use this button to open the modal. Secondly, we have the dialog tag, the content inside it represents the information inside the modal.</p><figure><img alt="a simple button in html" src="https://cdn-images-1.medium.com/max/1024/1*1LcRxlwYlBZZFoTuuRnQMw.png" /><figcaption>Photo by the Author</figcaption></figure><p>After clicking this button, the modal will pop up. This is where JavaScript comes into the picture. The dialog element has two methods, <em>showModal()</em> to open the modal, and <em>close() </em>to close the modal. To use them, you need to reference the modal with a class, id, or anything that will let you interact with the DOM.</p><figure><img alt="an exmaple presenting native modals in html" src="https://cdn-images-1.medium.com/max/1024/1*780OzCa5uIgrRA5cQHmlag.png" /><figcaption>Photo by the Author</figcaption></figure><p>We have used the <em>addEventListener()</em> method to handle the closing or the opening of the modal. The rest of the code is for CSS. We haven’t done any styling with it, we have just positioned the button in the middle, plus given it a specific size.</p><h3>Finally</h3><p>In this article, we talked a little bit about modal, and how they belong to alerts. We talked about the difference between notifications and alerts, then we used the dialog element to build a simple modal.</p><p>Let’s be honest. The example above is ugly and doesn’t represent a good use of modals or the dialog element. So, in the next article, we’ll learn how to style our modal. In short, we’ll give it a beautiful look.</p><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e280a91d3d3a" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/native-modals-in-html-with-example-e280a91d3d3a">Native Modals in HTML with Example</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use These Methods to Iterate Over Objects Easily in JavaScript]]></title>
            <link>https://javascript.plainenglish.io/use-these-methods-to-iterate-over-objects-easily-in-javascript-f085ab4fd111?source=rss-d8e53ec8cde0------2</link>
            <guid isPermaLink="false">https://medium.com/p/f085ab4fd111</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Yahya Jamaldine]]></dc:creator>
            <pubDate>Tue, 04 May 2021 14:17:50 GMT</pubDate>
            <atom:updated>2021-05-05T06:17:50.973Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YJGoXsrw5POIOK6Z" /><figcaption>Photo by <a href="https://unsplash.com/@roozbeheslami?utm_source=medium&amp;utm_medium=referral">Roozbeh Eslami</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Objects are the weirdest method of data collection in JavaScript, and they are likely the most used one, even though you might think that it might be arrays. The core of JavaScript and how we treat data is all about objects.</p><p>Everything can be treated as an object in JavaScript. Remember how many methods you have used to manipulate strings, how many arrays methods you have worked with, or how you’ve been creating objects through functions.</p><p>I’m not saying that in a fancy way. Create a function, array, and an object, then test if they are instances of an object by using the instanceof operator as I did in the example below. The result will blow your mind.</p><figure><img alt="Using the instanceof operator to check if functions, arrays are instance of an object. Everything is an object in JavaScript" src="https://cdn-images-1.medium.com/max/1024/1*dNv9lgouJzg4TDxqUdEQeA.png" /><figcaption>First Example</figcaption></figure><h3>Objects methods.</h3><p>The first time I learned how to use objects, I had the question that any JavaScript developer had when they learned about objects — how can I iterate over an object? Well, the answer was using the for in loop. Iterating over an object was a little bit weird and confusing compared to arrays.</p><p>Using the for in loop for a beginner would be confusing on the first try, because you’ll mix between getting the values of keys and the names of keys, plus you might fall in the trap of mutating the object while iterating over it.</p><p>Built-in objects methods took the burden of writing a for in loop just to get the keys or values of an object, so instead of stocking the values of object properties inside an array using the for in loop with three lines, we can do the same in a single line with the <em>Object.values(</em>) method.</p><h4>Object.values()</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A8RpCHVroCheNna0Yd4lCg.png" /><figcaption>Second Example</figcaption></figure><p>The example above uses the <em>Object.values()</em> method to get all the values inside an object, store them inside an array, then iterate over that array using the <em>map()</em> method.</p><p>What if we decided to get the keys of an object? Well, this time we’ll use the <em>Object.keys()</em> method in the same way we have used the <em>Object.values()</em> method.</p><h4>Object.keys()</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RHeMnU8WWSt1QhZPsEiQbg.png" /><figcaption>Third Example</figcaption></figure><p>What if I decided to get both keys and values? Should I get back to the old style with for in loop or what? Nah, object methods have got our back here too. You can use the Object.entries() method, but you should watch out for this one.</p><h4>Object.entries()</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ouxdgqehewq_dXwZgVFScg.png" /><figcaption>Fourth Example</figcaption></figure><p>In the example above, we are not getting a single value as we used to do with other methods. This time, we are dealing with a key-value store. So, we’ll end up with an array of arrays, where each internal array contains two items, the first one for the key name, and the second for the key-value as depicted below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*84jCoMHlOWerpK23gG-zyg.png" /><figcaption>Fifth Example</figcaption></figure><p>The <em>Object.entries()</em> method is very handy when converting an object to an array.</p><h3>Final word</h3><p>Now you have great tools to iterate over complex collections like objects and even transform them into arrays, but try to use them wisely or when they make sense. If they are too complex, you can still count on the for in loop.</p><p><a href="http://plainenglish.io/"><em>More content at plainenglish.io</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f085ab4fd111" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/use-these-methods-to-iterate-over-objects-easily-in-javascript-f085ab4fd111">Use These Methods to Iterate Over Objects Easily in JavaScript</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[3 Effective Things to Do While Reading Books About Software Development]]></title>
            <link>https://javascript.plainenglish.io/3-effective-things-to-do-while-reading-books-about-software-development-b27dc3ec08c5?source=rss-d8e53ec8cde0------2</link>
            <guid isPermaLink="false">https://medium.com/p/b27dc3ec08c5</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[books]]></category>
            <dc:creator><![CDATA[Yahya Jamaldine]]></dc:creator>
            <pubDate>Sat, 10 Apr 2021 07:28:34 GMT</pubDate>
            <atom:updated>2021-04-10T07:29:01.451Z</atom:updated>
            <content:encoded><![CDATA[<h4>Bits of advice that will help you as a Computer Science student</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*u7ZqsM1aI4W82JMLTXAjaw.jpeg" /><figcaption>Photo from: <a href="https://unsplash.com/photos/505eectW54k"><strong>Unsplash</strong></a><strong>.</strong></figcaption></figure><p>Reading books about software development, or Computer Science tends to be a daunting task in the first chapters, and this has something to do with the first impression, and the expectation we made before reading the book.</p><p>However, after going through the first chapters of the book, ideas you’ve seen will start getting connected, the funny parts will reveal, the book will be more enjoyable, and the concept you are reading will start making sense to you.</p><p>Before getting into these parts, we skip a lot of concepts in the book because it frustrates us or because it’s very complicated; the problems don’t stop here; we forget some of the concepts we have learned, and that’s by the end of the book, or after a couple of weeks of reading it.</p><p>To overcome these two challenges of dealing with complicated concepts and forgetting the easy ones, I adopted some approaches that helped me, not just with that, but also with enhancing my productivity in how many books I read, and how I apply those things I learned.</p><h3>1. Get an electronic copy</h3><p>I didn’t want to mention this one, because for software development or IT most of us pick the electronic copy, but others satisfy themselves with the physical copy, however, to get most of this article try to pick the electronic copy of your book.</p><p>Reading ebooks about software development is funny more than you think, Imaging looking at code in kindle or Adobe reader in your machine, then writing that code directly in your editor, error you made will be less, understanding the code or concepts will be easier than ever</p><p>There are other situations to take into consideration, for example reading about a technology that depends on a book you have read before, so it will be a straightforward thing to open multiple books on your reader plus navigating to the most important parts of them.</p><h3>2. Highlight ideas</h3><p>Now after you picked up the electronic copy of your book, you can highlight the important ideas easily, by selecting the desired portion of the paragraph as shown in the picture below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*uOk7KgR4PNX9TqjSdf1Z5w.png" /><figcaption>Photo by the Author (from The Docker Succinctly ebook).</figcaption></figure><p>What’s interesting about highlighting ideas in readers like Adobe reader, you can choose which color the highlight should get, and the reader itself will count how many highlights you’ve made, plus saving their position in the book, so you can find them easily.</p><h3>3. Take notes</h3><p>In my first days of reading books, I remember how much information went in vain since I wasn’t taking notes, It’s something that you should do whenever you decide to pick a book, and even when watching tutorials, ignore this one and you’ll end up always forgetting what you have learned.</p><p>Thusly, take notes each time you find interesting ideas. The difference between highlighting ideas and taking notes about them is that you could construct notes in a way that looks simple for you to understand, the next time you check them..</p><p>I write notes in two different ways, the first one by writing them down in a textual file or a personal note, the second way is by writing them beside highlights as shown in the picture below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*pwd-acov7_qRkPmRhwXyEQ.png" /><figcaption>Photo by the Author (from The Docker Succinctly ebook).</figcaption></figure><p>After clicking on “open pop-up note” as shown in the example above you’ll get a small field beside the highlight where you can write your note, I’m using Adobe Pdf reader in these examples, another reader may not have these features.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*enNJ9C-utzdqdyqfCJZC_w.png" /><figcaption>Photo by the Author (from The Docker Succinctly ebook).</figcaption></figure><h3>Conclusion</h3><p>When it comes to software development or anything related to computer science, you’ll have to choose between learning things by reading books or by watching tutorials.</p><p>I decided to learn things by reading books and articles years ago, however, this doesn’t mean that I don’t watch courses and tutorials, if I didn’t find a book that suits me, or if the concept I’m trying to understand doesn’t require a lot of work, I might choose tutorials.</p><p>Applying the three approaches I mentioned above helped me a lot, even though they may seem travails, they become like a sort of norm for me, and that’s for one reason, it’s making every book I read useful.</p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b27dc3ec08c5" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/3-effective-things-to-do-while-reading-books-about-software-development-b27dc3ec08c5">3 Effective Things to Do While Reading Books About Software Development</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Calculate CSS Selector Specificity]]></title>
            <link>https://javascript.plainenglish.io/how-to-calculate-css-selector-specificity-72aa076c6a18?source=rss-d8e53ec8cde0------2</link>
            <guid isPermaLink="false">https://medium.com/p/72aa076c6a18</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Yahya Jamaldine]]></dc:creator>
            <pubDate>Sat, 27 Mar 2021 23:27:58 GMT</pubDate>
            <atom:updated>2021-03-27T23:27:58.029Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part 1: What you don’t know about CSS Selectors</h4><figure><img alt="Woman and man sitting in front of a computer." src="https://cdn-images-1.medium.com/max/1024/1*n3yERMhGi61EGXzmLs9bdw.jpeg" /><figcaption>Image by <a href="https://unsplash.com/@heylagostechie">@heylagostechie</a> on Unsplash.</figcaption></figure><p>Many times when I talk to junior front-developers or those people who just learned HTML and CSS for curiosity, they shock me by saying CSS is an easy language, yeah, the syntax of CSS seems easy at the first sight or in those first months of learning web development.</p><p>That easy syntax in CSS might be the most deceptive thing you’ll ever meet in the language itself because it contains the complexity of an algorithm that selects between many CSS declarations to determine the exact value for a given CSS property, we are talking about the cascade algorithm here.</p><p>Furthermore, we have the headache of<em> Box Alignment module, Specificity, </em>and I could argue that mastering these concepts is just one of the things that separate a junior CSS developer from a senior one or why not, a CSS master? Hi !! I’ve said: just one of the things.</p><figure><img alt="Aligning items using flexbox model in CSS" src="https://cdn-images-1.medium.com/max/671/1*mW5XXHYxob2u9pjl6dPmQA.png" /><figcaption>A Photo from <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container">developer.mozilla.org</a> (Aligning Items with flexbox).</figcaption></figure><p>Because mastering CSS involves other things like f<em>ile organization, naming systems, debugging, and optimization,</em> there’s more to mention but let’s focus today on specificity, it’s an important concept in CSS development, this why you should give it some time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y22Eil-6rWNA951q2JrB2A.png" /><figcaption>A Photo from<a href="https://www.toptal.com/css/smacss-scalable-modular-architecture-css"> toptal.com (</a>f<em>ile organization in CSS)</em></figcaption></figure><h3>Introduction to Specificity</h3><p>First, what’s Specificity? Well, think of it as the factor that will specify which style declaration should be applied for an element, this factor act as a score that’s related to your CSS selectors, and it has some straightforward goals like solving problems related to the global nature of CSS.</p><p>Now things are getting ugly, “the global nature of CSS”, “Specificity”. What the heck these things stand for? Well, let’s clarify the meaning behind the global nature, then we’ll concentrate on the Specificity.</p><p>Do you remember the first time you have tried to learn CSS, I guess you’ve created a couple of paragraphs using the &lt;p&gt; tag then you applied a specific color to their text and background using a style declaration with the p selector.</p><p>You end up being shocked with every paragraph having that style, your intention or the expectation you have set was just adding some styles to the first paragraph.</p><figure><img alt="A BASIC HTML PAGE, html code, css" src="https://cdn-images-1.medium.com/max/1024/1*Y904nb1hTdQakdS52BSH4g.png" /></figure><p>This when “the global nature” comes in, CSS has a global scope so when you apply a style declaration using the p element selector, this style will be applied to every &lt;p&gt;element in your HTML document, even though if it wasn’t the intended desire.</p><figure><img alt="couple of paragraphs in html" src="https://cdn-images-1.medium.com/max/1024/1*bdQyhhOB3DEwula6xhPouw.png" /><figcaption>Photo by the Author.</figcaption></figure><p>Things get messy when you try to solve these global nature problems using combined or single selectors for example <strong>div#para p</strong> and <strong>div.para p</strong>, let’s say you wrote two different styles declaration with these two selectors.</p><p><strong>Div#para p</strong> will apply a red background to the paragraph, and <strong>div.para p</strong> will apply a black one, which color will be applied to the background of the &lt;p&gt; element under the &lt;div&gt; element?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*npr4RwIwqsztyylREjyW7g.png" /><figcaption>Photo by the Author.</figcaption></figure><p>You thought that the fourth paragraph will have a black background? Not a red one. Why did this happen? You might think that it has something to do with the last style that it has been declared? Nah, the last style declaration has been used with <strong>div.para p </strong>selector, but it didn’t affect the fourth paragraph.</p><p>It’s a matter of specificity, and as I’ve mentioned early it acts as a score, each selector in CSS has a score of specificity, based on that score a style declaration will be applied to the desired element, in the last example we had a red background because the ID selector has a high score in its specificity.</p><h3>Calculating Specificity</h3><p>The browser attaches the declared styles based on the specificity of selectors, but how do we figure out which selector has a high or low score of specificity? Well, we have some rules that will help us in calculating the score (value) of specificity in each selector.</p><p>Calculating selectors specificity is explained as follows in the<em> </em><a href="https://drafts.csswg.org/selectors-3/#specificity"><em>World Wide Web Consortium(W3C)</em></a><em>:</em></p><ul><li>count the number of ID selectors in the selector (= a)</li><li>count the number of class selectors, attributes selectors, and pseudo-classes in the selector (= b)</li><li>count the number of type selectors and pseudo-elements in the selector (= c)</li><li>ignore the universal selector(*)</li></ul><p>This seems a little confusing but give it a deep stare, in one selector, the values (a, b, c) are combined to produce a single value, let’s follow these rules to determine the specificity of the selectors we have used early.</p><p><strong>Div.para p</strong> contains two types selector div and p, with a single class selector let’s determine the values for (a, b, c).</p><p>We don’t have any ID selector in this combined selector above this means<strong> a=0, </strong>one class selector so <strong>b=1</strong>, plus two type selectors, therefore, <strong>c=2, combined </strong>(a=0, b=1, c=2) and that’s equal (0,1,2) of specificity.</p><p>For the <strong>div#para p</strong> selector we have, one ID selector (a=1), two type selectors (c=2), so the specificity value equal (1,0,2), in this case, we have 0 in the value of, because we have zero selectors that belong to the b type.</p><p>Now let’s compare these specificity values, for these two selectors <strong>div#para p</strong> (1,0,2) is bigger than (0,1,2) for the <strong>div.para p</strong> selector, and back to the early example, the red background has been applied because the <strong>div#para p </strong>selector had a high specificity value.</p><h3>Equal specificity</h3><p>What if we had some combined selectors with equal specificity value? As example: input[type=text] and input.text.</p><p>In this case we have one type selector with an attribute selector or class selector, input[type=text]= (0,1,1), input.text= (0,1,1), in this kind of situation, the last selector you have used in your CSS file, or style tag in your HTML document will be applied.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qyhsFRUMDG7DSpc58PiGYA.png" /><figcaption>Photo by the Author.</figcaption></figure><p>As do you see the last style declaration for the input element has been applied, I guess this kind of situation seems trivial because this is what we used to, the last style declaration always overrides the previous one.</p><p>If you want to play around with the example above you can add a pseudo-class like: hover to the input[type=text]selector, after reloading your document, each time you hover over the input’s background color will turn out to aquamarine, a color that combines between the green and blue color.input[type=text]:hover</p><p>Adding the hover pseudo-class to the input[type=text] selector, will change its specificity from (0,1,1) to (0,2,1) and that’s because pseudo-classes belong to the selectors of type b, however, the input[type=text]:hover specificity won’t change till you hover over the input element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tVcu3rYOhtRGURTb7bYPIA.png" /><figcaption>Photo by the Author.</figcaption></figure><h3>Note:</h3><p><em>The last update of Visual studio code will help you out in calculating the specificity, select any selector in CSS, and the editor will pop up a small window that contains the value of specificity as shown below.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hNdozlnJ4mR72Ifw7SZVsQ.png" /><figcaption>Photo by the Author.</figcaption></figure><h3>Conclusion</h3><p>Calculating the specificity value may seem complicated in the first stare. It will took a while for you to understand it, just stick to the process.</p><p>Constructing too many combined selectors and calculating their specificity value, plus checking if the style declaration attached to the selector with the highest specificity value has been applied, helped me a lot, so why not? Try it.</p><h3>What’s next?</h3><p>In the next article about CSS specificity, we’ll dive into more interesting situations that carry complex selectors, when to choose high specificity selectors over low ones, and how to deal with chained class selectors.</p><h3>Resources:</h3><p><strong>Specificity Calculator</strong>: specificity.keegan.st</p><p><strong>Selectors Names With Examples</strong>: <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors#basic_selectors">developer.mozilla.org</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72aa076c6a18" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/how-to-calculate-css-selector-specificity-72aa076c6a18">How to Calculate CSS Selector Specificity</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>