<?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"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://alok-raturi.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 17 Jun 2026 22:52:41 GMT</lastBuildDate><atom:link href="https://alok-raturi.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How the Host property Works in Angular @Component]]></title><description><![CDATA[In Angular, the host property is used to bind properties, attributes, and events to the component’s host element, which is the DOM element matching the component’s selector. In Angular, the component’s selector is visible in the DOM tree. As shown in...]]></description><link>https://alok-raturi.hashnode.dev/how-the-host-property-works-in-angular-component</link><guid isPermaLink="true">https://alok-raturi.hashnode.dev/how-the-host-property-works-in-angular-component</guid><category><![CDATA[Angular]]></category><category><![CDATA[Angular 2]]></category><category><![CDATA[Angular 4]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Alok Raturi]]></dc:creator><pubDate>Sat, 05 Oct 2024 18:28:56 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1728152929045/bec30502-a96c-432d-8148-8574491f3aa0.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In Angular, the host property is used to bind properties, attributes, and events to the component’s host element, which is the DOM element matching the component’s selector. In Angular, the component’s selector is visible in the DOM tree. As shown in the image below, <code>app-host</code> and <code>app-root</code> are the component selectors present in the DOM tree.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728148718000/c2303278-9244-46b3-b035-a424420aad35.png" alt class="image--center mx-auto" /></p>
<p>We use the host property to bind attributes, properties, and events to these component selectors. These property values can be static or dynamically calculated based on the component's current state.</p>
<h3 id="heading-how-to-use-the-host-property">How to Use the Host Property?</h3>
<p>We can bind events, properties, and attributes to host elements by using the host property provided in the <code>@Component</code> decorator. The host property takes an object where we specify these properties as key-value pairs.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;

<span class="hljs-meta">@Component</span>({
  selector: <span class="hljs-string">'app-host'</span>,
  standalone: <span class="hljs-literal">true</span>,
  imports: [],
  templateUrl: <span class="hljs-string">'./host.component.html'</span>,
  styleUrl: <span class="hljs-string">'./host.component.css'</span>,
  host:{
    <span class="hljs-string">'class'</span>:<span class="hljs-string">'host-class'</span>,
    <span class="hljs-string">'id'</span>:<span class="hljs-string">'test'</span>,
    <span class="hljs-string">'(click)'</span>:<span class="hljs-string">'hostClick()'</span>
  }
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> HostComponent {
  hostClick(){
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Host clicked'</span>);
  }
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728149113729/d393e261-5129-494d-bb4f-62b4b44cb279.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-hostbinding-and-hostlistener-in-angular">@HostBinding and @HostListener in Angular</h3>
<p>We can also use the <code>@HostBinding</code> and <code>@HostListener</code> decorators to bind events, attributes, and properties to our host element.</p>
<ul>
<li><p><code>@HostBinding</code> lets us bind properties and attributes.</p>
</li>
<li><p><code>@HostListener</code> lets us bind event listeners to the host element.</p>
</li>
</ul>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> { Component, HostBinding, HostListener } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;

<span class="hljs-meta">@Component</span>({
  selector: <span class="hljs-string">'app-host'</span>,
  standalone: <span class="hljs-literal">true</span>,
  imports: [],
  templateUrl: <span class="hljs-string">'./host.component.html'</span>,
  styleUrl: <span class="hljs-string">'./host.component.css'</span>,

})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> HostComponent {
  <span class="hljs-meta">@HostBinding</span>() id = <span class="hljs-string">'host-id'</span>;
  <span class="hljs-meta">@HostBinding</span>(<span class="hljs-string">'class'</span>) cl =<span class="hljs-string">'host-class'</span>

  <span class="hljs-meta">@HostListener</span>(<span class="hljs-string">'click'</span>,[<span class="hljs-string">'$event'</span>])
  onClick(event: MouseEvent) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'click on host'</span>, event);
  }
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728150806369/35a198d9-0303-4fdb-9636-f7eb810a62fb.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728150837065/ca4cd5ea-91f0-42e0-9d2a-ab6b8540537b.png" alt class="image--center mx-auto" /></p>
<p>As you can see, we can add different attributes and properties to the host element using @HostBinding, and we can add event listeners using the @HostListener property.</p>
<p>When building modern applications with Angular, we should prefer using the host property over the @HostBinding and @HostListener decorators, as these decorators are mainly for backward compatibility.</p>
<p>If a component has both @HostBinding/@HostListener and the host property, and there is a conflict, the properties inside the host property in the component decorator will take precedence.</p>
<h3 id="heading-binding-collison">Binding Collison</h3>
<p>Binding collision can occur if a developer also provides the same attributes/listener while using the component instance in other components' templates. In those cases, the collision is resolved according to these rules:</p>
<ul>
<li><p>If both values are static, the instance binding wins.</p>
</li>
<li><p>If one value is static and the other dynamic, the dynamic value wins.</p>
</li>
<li><p>If both values are dynamic, the component's host binding wins.</p>
</li>
</ul>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> { Component, HostBinding, HostListener } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;

<span class="hljs-meta">@Component</span>({
  selector: <span class="hljs-string">'app-host'</span>,
  standalone: <span class="hljs-literal">true</span>,
  imports: [],
  templateUrl: <span class="hljs-string">'./host.component.html'</span>,
  styleUrl: <span class="hljs-string">'./host.component.css'</span>,
  host:{
    <span class="hljs-string">'id'</span>: <span class="hljs-string">'test-class'</span>,
  }
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> HostComponent {}
</code></pre>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">app-host</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"abc"</span>/&gt;</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728152617734/376c7197-e1c9-4473-85a0-62cb358e7ec4.png" alt class="image--center mx-auto" /></p>
<p><em>As you can see, the first rule applies as the instance value overrides the host binding when both values are static. The same is true for the other two rules.</em></p>
<h3 id="heading-a-quick-summary">A Quick Summary</h3>
<p>The host property in Angular’s @Component decorator offers a simpler way to attach various properties, attributes, and event listeners to a component's instance in the DOM. It allows us to dynamically update the host element based on the component's state. Additionally, Angular automatically checks for any changes in the host properties during its change detection cycle. While @HostBinding and @HostListener provide backward compatibility and better maintainability, the host property lets us declare multiple properties in one place in a very compact way.</p>
<p>Every Angular developer should learn about the host property because it is essential for better development, easy to use, and improves code readability.</p>
<p><strong><em>Thank you for your time! Please let me know if there are any opportunities for improvement in this blog or any additions I can make in the comment section.</em></strong></p>
]]></content:encoded></item><item><title><![CDATA[Understanding JavaScript Callback Functions]]></title><description><![CDATA[Callback functions are one of the main pillars that support the asynchronous nature of JavaScript. With callbacks, developers can specify a piece of code to execute when a particular event occurs. Besides events, callback functions are crucial for ha...]]></description><link>https://alok-raturi.hashnode.dev/understanding-javascript-callback-functions</link><guid isPermaLink="true">https://alok-raturi.hashnode.dev/understanding-javascript-callback-functions</guid><category><![CDATA[callback functions]]></category><category><![CDATA[callback hell ]]></category><category><![CDATA[asynchronous JavaScript]]></category><category><![CDATA[promises]]></category><category><![CDATA[async/await]]></category><dc:creator><![CDATA[Alok Raturi]]></dc:creator><pubDate>Thu, 26 Sep 2024 04:10:46 GMT</pubDate><content:encoded><![CDATA[<p>Callback functions are one of the main pillars that support the asynchronous nature of JavaScript. With callbacks, developers can specify a piece of code to execute when a particular event occurs. Besides events, callback functions are crucial for handling asynchronous tasks like API calls, <code>setTimeout()</code>, and <code>setInterval()</code> functions.</p>
<h3 id="heading-what-is-a-callback-function">What is a Callback Function</h3>
<p>A callback function in JavaScript is a function passed to another function as an argument and executed after a specific event is triggered or some operation is completed.</p>
<p>A callback function that is passed to a function can be executed in that function body at any point in that function. It is the responsibility of the developer to call the callback function with the correct argument in the function body.</p>
<p>A callback function can be used to perform a synchronous operation or a asynchronous operation.</p>
<h3 id="heading-example">Example</h3>
<p>In JavaScript, we can pass any type of function into other functions as arguments because functions are first-class citizens. The function we pass can be an anonymous function, an arrow function, or a regular function. Below are a few examples of callback functions in JavaScript. Callbacks can be used for both asynchronous and synchronous tasks.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// callback for input validation</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">outerFun</span>(<span class="hljs-params">a, cb</span>)</span>{
    <span class="hljs-keyword">if</span>(cb(a)){
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Callback function returned true"</span>)
        <span class="hljs-comment">// Further processing on a</span>
    }<span class="hljs-keyword">else</span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Callback function returned false"</span>)
    }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkLength</span>(<span class="hljs-params">name</span>)</span>{
    <span class="hljs-keyword">return</span> name.length==<span class="hljs-number">4</span>
}

outerFun(<span class="hljs-string">"alok"</span>,checkLength)
</code></pre>
<p>The example above shows how callbacks can be used to perform a synchronous task, making the code more modular.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"click"</span>,<span class="hljs-function">()=&gt;</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Button Clicked"</span>);
});
</code></pre>
<p>The event handler callback can be either synchronous or asynchronous. Once the event is triggered, the callback will be executed immediately.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Timer Expired"</span>)
},<span class="hljs-number">2000</span>)
</code></pre>
<p>The timer function's callbacks are always asynchronous. Similarly, AJAX requests are also asynchronous.</p>
<h5 id="heading-use-cases-of-callbacks">Use cases of Callbacks:</h5>
<p>There are many uses of callbacks. Some of them are:</p>
<ol>
<li><p>Event handling: Callback functions are used for event handling in JavaScript. A callback is executed whenever an event is triggered.</p>
</li>
<li><p>AJAX Requests: Callback functions are used to process the data from AJAX requests.</p>
</li>
<li><p>JavaScript’s Higher-order functions: Many JavaScript functions take callbacks as input and return some data based on that callback function. Examples include map, filter, reduce, and find.</p>
</li>
<li><p>Timer Functions: Timer functions like setTimeout and setInterval also take a function and execute it after a certain time interval.</p>
</li>
<li><p>In Promises: Promise methods (.then and .catch) also take callbacks as arguments and execute them once the promise is resolved.</p>
</li>
</ol>
<h3 id="heading-challenges-with-callbacks">Challenges with Callbacks</h3>
<p>There are two challenges associated with callbacks. These challenges make the code difficult to maintain and debug. They are:</p>
<ol>
<li><p><strong>Callback Hell</strong></p>
<ul>
<li><p>Callback hell happens when we pass a callback function to another function, and that callback function calls another function that also takes a callback, and so on.</p>
</li>
<li><p>This creates a situation where multiple nested callback functions form a complex structure that is hard to read, maintain, and debug.</p>
</li>
<li><p>This structure is also known as the pyramid of doom.</p>
</li>
<li><p>It makes our code complex and unreadable, harder to debug and maintain, and results in poor code quality.</p>
</li>
<li><p>Below is an example of callback hell. In the code below, the <code>greet</code> function takes a callback function, which then calls another function <code>askStatus</code>, which also takes a callback, and so on, leading to callback hell.</p>
</li>
<li><pre><code class="lang-javascript">        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">callback,name</span>) </span>{
          <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            callback(<span class="hljs-string">`Hello <span class="hljs-subst">${name}</span>, `</span>);
          }, <span class="hljs-number">1000</span>);
        }

        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">askStatus</span>(<span class="hljs-params">data, callback</span>) </span>{
          <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            callback(data + <span class="hljs-string">"  What are you doing? "</span>);
          }, <span class="hljs-number">1000</span>);
        }

        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">goodBye</span>(<span class="hljs-params">data, callback</span>) </span>{
          <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            callback(data + <span class="hljs-string">"  Take Care. Good Bye.  "</span>);
          }, <span class="hljs-number">1000</span>);
        }

        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callback</span>(<span class="hljs-params">a</span>) </span>{
          askStatus(a, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">b</span>) </span>{
            goodBye(b, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">c</span>) </span>{
                <span class="hljs-built_in">console</span>.log(c);
            });
          });
        }
        greet(callback,<span class="hljs-string">"alok"</span>);
</code></pre>
</li>
</ul>
</li>
<li><p><strong>Inversion of Control</strong></p>
<ul>
<li><p>Whenever we pass a callback function to another function (caller) as an argument, it is up to that function to execute the callback correctly. The caller has the power to run the function as many times as it wants.</p>
</li>
<li><p>When the caller is running the callback function, it is handing over control to the callback, meaning the caller function has to wait until the callback finishes.</p>
</li>
<li><p>This inversion of control can create challenges in tracking the execution flow and debugging the program.</p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-solution">Solution</h3>
<p>Callback functions in JavaScript are essential for handling asynchronous operations, but they can introduce many challenges that affect code quality. To address these challenges, JavaScript offers features that help reduce the problems associated with callbacks.</p>
<h6 id="heading-use-promises"><strong>Use Promises:</strong></h6>
<ul>
<li><p>A promise object represents the eventual completion of an asynchronous operation.</p>
</li>
<li><p>Promises come with a .then() method that takes a callback as an argument. We can chain as many .then() methods as we want, and the callback inside .then() will execute once the promise and the .then() method above it are resolved.</p>
</li>
<li><p>Promises also come with a .catch() method, which takes a callback and is executed if there is an error during promise resolution.</p>
</li>
<li><p>Below is the code that executes the above callback hell code using promises.</p>
</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve, reject</span>) </span>{
    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
      resolve(<span class="hljs-string">`Hello <span class="hljs-subst">${name}</span>, `</span>);
    }, <span class="hljs-number">1000</span>);
  });
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">askStatus</span>(<span class="hljs-params">data</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve, reject</span>) </span>{
    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
      resolve(data + <span class="hljs-string">"  What are you doing? "</span>);
    }, <span class="hljs-number">1000</span>);
  });
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">goodBye</span>(<span class="hljs-params">data</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve, reject</span>) </span>{
    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
      resolve(data + <span class="hljs-string">"  Take Care. Good Bye.  "</span>);
    }, <span class="hljs-number">1000</span>);
  });
}

greet(<span class="hljs-string">"Alok"</span>)
  .then(askStatus)
  .then(goodBye)
  .then(<span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> <span class="hljs-built_in">console</span>.log(data))
  .catch(<span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> <span class="hljs-built_in">console</span>.log(err));
</code></pre>
<p>For a clearer understanding of promises, check out <a target="_blank" href="https://rahulrautela.hashnode.dev/simplify-your-asynchronous-code-with-promises-over-callbacks">this blog</a> by Rahul Singh Rautela.</p>
<p><strong>Use Async/Await:</strong></p>
<ul>
<li><p>The <code>async</code> keyword in JavaScript is used to declare a function as an asynchronous function, which always returns a promise.</p>
</li>
<li><p>The <code>await</code> keyword can only be used inside an async function. It is used to wait for the result of an asynchronous task. The function execution is paused until the result from the async task is obtained.</p>
</li>
<li><p>Below is the code that executes the above callback hell example using async/await.</p>
</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve, reject</span>) </span>{
    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
      resolve(<span class="hljs-string">`Hello <span class="hljs-subst">${name}</span>, `</span>);
    }, <span class="hljs-number">1000</span>);
  });
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">askStatus</span>(<span class="hljs-params">data</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve, reject</span>) </span>{
    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
      resolve(data + <span class="hljs-string">"  What are you doing? "</span>);
    }, <span class="hljs-number">1000</span>);
  });
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">goodBye</span>(<span class="hljs-params">data</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve, reject</span>) </span>{
    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
      resolve(data + <span class="hljs-string">"  Take Care. Good Bye.  "</span>);
    }, <span class="hljs-number">1000</span>);
  });
}

<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">main</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> greeting = <span class="hljs-keyword">await</span> greet(<span class="hljs-string">"Alok"</span>);
    <span class="hljs-keyword">const</span> status = <span class="hljs-keyword">await</span> askStatus(greeting);
    <span class="hljs-keyword">const</span> finalMsg= <span class="hljs-keyword">await</span> goodBye(status);
    <span class="hljs-built_in">console</span>.log(finalMsg);
  } <span class="hljs-keyword">catch</span> (err) {
    <span class="hljs-built_in">console</span>.log(err);
  }
}
main();
</code></pre>
<p>In addition to these solutions, we should try to modularize our code to avoid nesting callbacks. We should also implement proper error handling techniques to manage errors in callbacks more efficiently.</p>
<p>For a better understanding of async/await, check out <a target="_blank" href="https://madhurgupta.hashnode.dev/javascript-asyncawait-deep-dive">this blog</a> by Madhur Gupta.</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>Callback functions enable asynchronous programming in JavaScript. They are used extensively, especially in event handling, network requests, and timer functions. By understanding callbacks in JavaScript, we can write more modular, flexible, and asynchronous code. However, callbacks come with challenges like callback hell and inversion of control, making the code error-prone. By using promises and async/await, we can overcome these challenges and make our code easier to debug, maintain, and read. Overall, it is essential for a JavaScript developer to have a good understanding of callbacks.</p>
<p>Thank you for your time! Please let me know if there are any opportunities for improvement in this blog or any additions I can make in the comment section.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Call, Apply, and Bind Methods in JavaScript]]></title><description><![CDATA[Before understanding the call, apply, and bind methods, it is essential to understand the concept of the this keyword in JavaScript. The this keyword refers to the context in which a function is executing. Its value is not fixed and depends on how a ...]]></description><link>https://alok-raturi.hashnode.dev/understanding-call-apply-and-bind-methods-in-javascript</link><guid isPermaLink="true">https://alok-raturi.hashnode.dev/understanding-call-apply-and-bind-methods-in-javascript</guid><dc:creator><![CDATA[Alok Raturi]]></dc:creator><pubDate>Sun, 22 Sep 2024 13:58:54 GMT</pubDate><content:encoded><![CDATA[<p>Before understanding the <strong>call</strong>, <strong>apply</strong>, and <strong>bind</strong> methods, it is essential to understand the concept of the <strong>this</strong> keyword in JavaScript. The <strong>this</strong> keyword refers to the context in which a function is executing. Its value is not fixed and depends on how a function is invoked. Understanding the <strong>this</strong> keyword enables developers to write flexible and reusable code, as functions can operate in different contexts depending on their invocation.</p>
<p>The value of <strong>this</strong> is determined at runtime based on how a function is called. This dynamic binding allows functions to operate on various objects without requiring them to be rewritten. For instance, a method defined on one object can be invoked on another object, promoting code reuse and modularity.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">name</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>);
}

<span class="hljs-keyword">let</span> obj = {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"alok"</span>,
    <span class="hljs-attr">printName</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)</span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>);
    }
}

name()
obj.printName()
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727008217042/b08ca10a-c985-4b62-8176-acf92b295c5c.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-javascript"><span class="hljs-meta">"use strict"</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">name</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>);
}

name()
<span class="hljs-built_in">window</span>.name()
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727008284077/7d8deb23-02ed-4d41-98d3-4af7ed9641e8.png" alt class="image--center mx-auto" /></p>
<p>As you can see, the value of this depends on how the function is invoked and also on the use of strict mode. It behaves differently for arrow functions and normal functions. We can use the value of the this keyword to write more efficient and modular JavaScript functions.</p>
<h3 id="heading-why-to-use-call-apply-and-bind-methods">Why to use call, apply and bind methods</h3>
<p>In JavaScript, whenever we want to control the context in which our function is executing, we can use these three methods (call, apply, bind) which are present in the prototype of the function (Function.prototype).</p>
<h3 id="heading-call-method">Call ( ) Method</h3>
<p>The call method in JavaScript is a predefined method in the function prototype. This method allows us to call a function with an arbitrary this value and provided arguments without first attaching the function to the object as a property. In simple words, you can call a function and also specify the context in which you want to execute it.</p>
<h5 id="heading-syntax">Syntax:</h5>
<pre><code class="lang-plaintext">call(this)
call(this, ...args)  // ...args is comma separated values of arguments
</code></pre>
<h5 id="heading-example">Example:</h5>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> myName = {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"alok"</span>,
}

<span class="hljs-keyword">let</span> myName2 = {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"abhay"</span>,
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello, <span class="hljs-subst">${<span class="hljs-built_in">this</span>.name}</span>`</span>)
}

greet.call(myName) <span class="hljs-comment">// Hello, alok</span>
greet.call(myName2) <span class="hljs-comment">// Hello, abhay</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727009443954/92f9b944-8572-4bbc-ba2c-c8826fdee8fc.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// With Arguments</span>
<span class="hljs-keyword">let</span> myName = {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"alok"</span>,
}

<span class="hljs-keyword">let</span> myName2 = {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"abhay"</span>,
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">msg, day</span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello, <span class="hljs-subst">${<span class="hljs-built_in">this</span>.name}</span> - <span class="hljs-subst">${msg}</span>. Today is <span class="hljs-subst">${day}</span>`</span>)
}

greet.call(myName,<span class="hljs-string">"Good Morning"</span>,<span class="hljs-string">"Wednesday"</span>)
greet.call(myName2,<span class="hljs-string">"Good evening"</span>,<span class="hljs-string">"Friday"</span>)
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727009706842/718de546-b7eb-4cda-b050-7cad11cb2a96.png" alt class="image--center mx-auto" /></p>
<p>If we don’t provide any arguments to the call function, it will still execute, but the value of this will be either undefined or the global object, depending on the use of strict mode or non-strict mode.</p>
<h3 id="heading-apply-method">Apply ( ) Method</h3>
<p>Apply does the same work as the call function does, but the difference is that apply takes the function arguments as a list of values rather than comma-separated values as the call function does.</p>
<h5 id="heading-syntax-1">Syntax:</h5>
<pre><code class="lang-plaintext">apply(this)
apply(this, [arg1, arg2, arg3, ...... ,argN])
</code></pre>
<h5 id="heading-example-1">Example:</h5>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> myName = {
    <span class="hljs-attr">firstName</span>: <span class="hljs-string">"alok"</span>,
    <span class="hljs-attr">lastName</span>: <span class="hljs-string">"raturi"</span>,
    <span class="hljs-attr">printName</span>:<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">msg, day</span>)</span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello, <span class="hljs-subst">${<span class="hljs-built_in">this</span>.firstName}</span> <span class="hljs-subst">${<span class="hljs-built_in">this</span>.lastName}</span>- <span class="hljs-subst">${msg}</span>. Today is <span class="hljs-subst">${day}</span>`</span>);
    }
}

<span class="hljs-keyword">let</span> myName2 = {
    <span class="hljs-attr">firstName</span>: <span class="hljs-string">"abhay"</span>,
    <span class="hljs-attr">lastName</span>: <span class="hljs-string">"singh"</span>,
}

myName.printName.apply(myName2, [<span class="hljs-string">'Good Morning'</span>,<span class="hljs-string">"Wednesday"</span>]);
myName.printName.call(myName2, <span class="hljs-string">'Good Morning'</span>,<span class="hljs-string">"Wednesday"</span>);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727010582941/f36d4fa0-9a96-47e6-a7ad-15a66e72d5dd.png" alt class="image--center mx-auto" /></p>
<p>As you can see, invoking the <code>printName</code> function by call and apply methods gives the same output; only the syntax is different. You can use call and apply interchangeably, but you have to be careful with the syntax.</p>
<p><strong>Function borrowing:</strong> Using methods like call, apply, and bind, we can use the function associated with a specific object to be called by another object without rewriting the function. For example, in the above example, the <code>myName2</code> object is also using the <code>printName</code> function, which is in the <code>myName</code> object..</p>
<h3 id="heading-bind-method">Bind ( ) Method</h3>
<p>Bind is another function provided in the function prototype (Function.prototype). It returns a new function with the specified this value, which can be called later at any point in our program. The set of arguments required for the function can be provided with the bind function itself, or we can give them to the new function when we invoke it later.</p>
<h5 id="heading-syntax-2">Syntax:</h5>
<pre><code class="lang-plaintext">bind(this)
bind(this, ...args)  // comma separated values of arguments
</code></pre>
<p>Example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> myName = {
    <span class="hljs-attr">firstName</span>: <span class="hljs-string">"alok"</span>,
    <span class="hljs-attr">lastName</span>: <span class="hljs-string">"raturi"</span>,
    <span class="hljs-attr">printName</span>:<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">msg, day</span>)</span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello, <span class="hljs-subst">${<span class="hljs-built_in">this</span>.firstName}</span> <span class="hljs-subst">${<span class="hljs-built_in">this</span>.lastName}</span>- <span class="hljs-subst">${msg}</span>. Today is <span class="hljs-subst">${day}</span>`</span>);
    }
}

<span class="hljs-keyword">let</span> myName2 = {
    <span class="hljs-attr">firstName</span>: <span class="hljs-string">"abhay"</span>,
    <span class="hljs-attr">lastName</span>: <span class="hljs-string">"singh"</span>,
}


<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">SayHi</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hi, <span class="hljs-subst">${<span class="hljs-built_in">this</span>.firstName}</span>`</span>);
}

<span class="hljs-keyword">let</span> sayHi1 = SayHi.bind(myName);
<span class="hljs-keyword">let</span> sayHi2 = SayHi.bind(myName2);
<span class="hljs-keyword">let</span> printName  = myName.printName.bind(myName2, <span class="hljs-string">"Good morning"</span>,<span class="hljs-string">"Tuesday"</span>)

sayHi1()
sayHi2()
printName()
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727011794988/620a6651-a801-4c11-b69b-02f7b0e424ed.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>The <code>call</code>, <code>apply</code>, and <code>bind</code> methods in JavaScript are essential functions that every JavaScript programmer should know to ensure code reusability and maintainability. By leveraging these methods, developers can write cleaner, more modular code that adheres to best practices.</p>
<p>Thank you for your time! Please let me know if there are any opportunities for improvement in this blog or any additions I can make in the comment section.</p>
]]></content:encoded></item><item><title><![CDATA[Introduction to GRID Layout in CSS]]></title><description><![CDATA[CSS Grid Layout simplifies the creation of complex layouts by organizing the layout into rows and columns, making it easier to design web pages. Similar to Flexbox, Grid allows us to create layouts with greater consistency and ease by dividing the pa...]]></description><link>https://alok-raturi.hashnode.dev/introduction-to-grid-layout-in-css</link><guid isPermaLink="true">https://alok-raturi.hashnode.dev/introduction-to-grid-layout-in-css</guid><dc:creator><![CDATA[Alok Raturi]]></dc:creator><pubDate>Tue, 17 Sep 2024 06:41:48 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1726555101791/78ade2ea-558a-48f5-aab4-043febb89d78.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>CSS Grid Layout simplifies the creation of complex layouts by organizing the layout into rows and columns, making it easier to design web pages. Similar to Flexbox, Grid allows us to create layouts with greater consistency and ease by dividing the page into rows and columns.</p>
<h3 id="heading-defining-a-grid"><strong>Defining a grid</strong></h3>
<p>To define a container as a grid layout, use the <code>display</code> property. Just like <code>display: flex</code> for a Flexbox container, use <code>display: grid</code> for a Grid layout container.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.grid</span>{
    <span class="hljs-attribute">display</span>: grid;
}
</code></pre>
<p>In the above example, <code>.grid</code> is the class of the container, which we are selecting using a class selector in CSS and setting the display of the container as <code>grid</code>.</p>
<p>By default, setting <code>display: grid</code> on an HTML element will create a 1-column grid, meaning the elements inside the container will be stacked vertically, one below the other.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726506297001/015a0a45-5c06-4729-b5b7-0c408b553f23.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-organize-your-elements-in-more-columns">Organize your elements in more columns</h3>
<p>To add more columns to your grid, use the <code>grid-template-columns</code> property on the container.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.grid</span>{
    <span class="hljs-attribute">display</span>: grid;
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">20%</span> <span class="hljs-number">40%</span> <span class="hljs-number">30%</span>;
}
</code></pre>
<p>In this example, <code>grid-template-columns: 20% 40% 30%</code> will divide the layout into 3 columns with widths of 20%, 40%, and 30% respectively. Instead of percentages, you can use any length unit like <code>rem</code>, <code>em</code>, <code>px</code>, etc.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726506969296/81056576-0d88-4aee-a112-9fdfd0dbdf13.png" alt class="image--center mx-auto" /></p>
<h4 id="heading-fr-unit-in-grid"><code>fr</code> unit in grid:</h4>
<p>The <code>fr</code> unit in Grid is used to allocate a fraction of the available width within a grid container. It removes the need to calculate percentages or fixed widths.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.grid</span>{
    <span class="hljs-attribute">display</span>: grid;
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">1</span>fr <span class="hljs-number">2</span>fr <span class="hljs-number">1</span>fr;
}
</code></pre>
<p>As shown in the image, <code>grid-template-columns: 1fr 2fr 1fr</code> will divide the container's width into 4 parts and allocate 1 part to the first column, 2 parts to the second column, and 1 part to the third column.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726508037761/e0e367fb-3665-4874-ac5b-563a49eb96c2.png" alt class="image--center mx-auto" /></p>
<p>The <code>fr</code> unit is specifically used for the <code>grid-template-rows</code> and <code>grid-template-columns</code> properties in CSS.</p>
<h4 id="heading-gaps-in-grid">Gaps in grid:</h4>
<p>In CSS Grid, there are three properties to create space between items within the container:</p>
<ol>
<li><p><code>column-gap</code> : Specifies the gap between columns.</p>
</li>
<li><p><code>row-gap</code>: Specifies the gap between rows.</p>
</li>
<li><p><code>gap</code>: A shorthand property that sets both column-gap and row-gap (e.g., <code>gap: row-gap column-gap</code>).</p>
</li>
</ol>
<pre><code class="lang-css"><span class="hljs-selector-class">.grid</span>{
    <span class="hljs-attribute">display</span>: grid;
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">1</span>fr <span class="hljs-number">2</span>fr <span class="hljs-number">1</span>fr;
    <span class="hljs-attribute">gap</span>: <span class="hljs-number">10px</span> <span class="hljs-number">100px</span>;
}
</code></pre>
<p>In the example above, the <code>gap</code> property (formerly <code>grid-gap</code>) is used to provide spacing between elements. The value <code>gap: 10px 100px</code> creates a 10px gap between rows and a 100px gap between columns.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726509451897/8e65f7d9-adce-43d1-9ddc-025600d76587.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-advanced-functions-and-features">Advanced functions and features:</h3>
<h4 id="heading-repeat-function">Repeat function:</h4>
<p>The <code>repeat()</code> function in CSS Grid Layout allows you to define a repeating pattern for rows or columns, making your code more concise and manageable.<br />For example, <code>grid-template-columns: repeat(3, 1fr)</code> This creates three columns, each taking up a size of <code>1fr</code>, similar to writing <code>grid-template-columns: 1fr 1fr 1fr;</code></p>
<h4 id="heading-minmax-function">Minmax function:</h4>
<p>The <code>minmax()</code> function allows you to set a minimum and maximum size for a track (row or column). It is useful when the track's size cannot accommodate its content, causing overflow.</p>
<p>Syntax: <code>minmax(min,max)</code></p>
<p>In this case, if the content overflows, the rows will resize to the minimum height specified (100px) and try to fit the content. If it still overflows, you may need to adjust the minimum height or use other methods to contain it.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.grid</span>{
    <span class="hljs-attribute">display</span>: grid;
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">3</span>,<span class="hljs-number">1</span>fr);
    <span class="hljs-attribute">grid-auto-rows</span>: <span class="hljs-built_in">minmax</span>(<span class="hljs-number">100px</span> ,auto);
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726545453873/41bd84dd-8d68-49a8-9c5a-deea55b9d69d.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-css"><span class="hljs-selector-class">.grid</span>{
    <span class="hljs-attribute">display</span>: grid;
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">3</span>,<span class="hljs-number">1</span>fr);
    <span class="hljs-attribute">grid-auto-rows</span>: <span class="hljs-built_in">minmax</span>(<span class="hljs-number">50px</span> ,auto);
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726545604398/7d8935d2-d8e1-40e5-b702-60f9d3ba0f6f.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-implicit-vs-explicit-grids">Implicit vs Explicit Grids</h3>
<h4 id="heading-explicit-grid">Explicit Grid:</h4>
<p>An explicit grid is defined by specifying properties such as <code>grid-template-columns</code> and <code>grid-template-rows</code>.</p>
<h4 id="heading-implicit-grid">Implicit Grid:</h4>
<p>Implicit grids are created automatically when items are placed outside the bounds of the explicit grid. CSS generates additional rows and columns to accommodate these items. You can control their size using <code>grid-auto-rows</code> and <code>grid-auto-columns</code>. By grid-auto-rows and grid-auto-columns properties in grid, we can give implicit grid with a certain size.<br />e.g. if we use <code>grid-auto-rows: 100px</code>, it will set the row-size as 100px for the content that will be auto placed by CSS and the same goes for <code>grid-auto-columns</code>.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.grid</span>{
    <span class="hljs-attribute">display</span>: grid;
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">3</span>,<span class="hljs-number">1</span>fr);
    <span class="hljs-attribute">grid-template-rows</span>: <span class="hljs-number">100px</span> <span class="hljs-number">100px</span> <span class="hljs-number">100px</span>;
    <span class="hljs-attribute">grid-auto-rows</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">gap</span>: <span class="hljs-number">2px</span>;
}
</code></pre>
<p>In this setup, the first three rows have a height of 100px, while subsequent rows have a height of 50px.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726547238311/eac6b135-1f95-4088-b6c0-55c77ee3eaf2.png" alt class="image--center mx-auto" /></p>
<p>CSS Properties for grid-auto-flow:</p>
<ul>
<li><p><code>grid-auto-flow</code>: controls how auto-placed items are inserted into the grid. By default, it is row.</p>
</li>
<li><p><code>grid-auto-columns</code>: controls the size of column track for auto-placed items.</p>
</li>
<li><p><code>grid-auto-rows</code>: controls the size of row track for auto-placed items  </p>
</li>
</ul>
<h3 id="heading-item-placement-properties">Item Placement Properties:</h3>
<p>These properties are used to place items on the grid. You can visualize a grid as a matrix where rows and columns are divided by lines. For a grid with three rows and three columns, you have four column lines and four row lines. You can use these lines to position items using properties like:</p>
<ul>
<li><p><code>grid-column-start</code> : Specifies the start line for a column.</p>
</li>
<li><p><code>grid-column-end</code> : Specifies the end line for a column.</p>
</li>
<li><p><code>grid-row-start</code> : Specifies the start line for a row.</p>
</li>
<li><p><code>grid-row-end</code> : Specifies the end line for a row.</p>
</li>
<li><p><code>grid-column</code> : A shorthand for <code>grid-column-start</code> and <code>grid-column-end</code>.</p>
</li>
<li><p><code>grid-row</code> : A shorthand for <code>grid-row-start</code> and <code>grid-row-end</code>.</p>
</li>
</ul>
<pre><code class="lang-html">      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"grid layout"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item1"</span>&gt;</span>ITEM 1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item2"</span>&gt;</span>ITEM 2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item3"</span>&gt;</span>ITEM 3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.grid</span>{
    <span class="hljs-attribute">display</span>: grid;
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">3</span>,<span class="hljs-number">1</span>fr);
    <span class="hljs-attribute">gap</span>: <span class="hljs-number">2px</span>;
}

<span class="hljs-selector-class">.item1</span>{
    <span class="hljs-attribute">grid-column</span>: <span class="hljs-number">1</span>/<span class="hljs-number">4</span>;
    <span class="hljs-attribute">grid-row</span>: <span class="hljs-number">1</span>/<span class="hljs-number">3</span>;
}
<span class="hljs-selector-class">.item2</span>{
    <span class="hljs-attribute">grid-column</span>: <span class="hljs-number">1</span>/<span class="hljs-number">3</span>;
}
</code></pre>
<p>In this example,<code>.item1</code>occupies two rows and three columns, while<code>.item2</code>occupies two columns.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726548322252/2d4a9049-0dac-428e-8d9c-b2951372c536.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-layout-management-with-grid-template-areas">Layout-management with grid-template-areas:</h3>
<p>The <code>grid-template-areas</code> property provides an alternative way to arrange items in your grid by assigning names to various elements of your layout.</p>
<p>The rules for <code>grid-template-areas</code>:</p>
<ul>
<li><p>Every cell of a grid needs to be filled.</p>
</li>
<li><p>Repeat names to span across multiple cells.</p>
</li>
<li><p>Use a period (<code>.</code>) to leave a cell empty.</p>
</li>
<li><p>Areas must be rectangular (cannot have L-shaped areas).</p>
</li>
<li><p>Areas cannot be repeated in different locations.  </p>
</li>
</ul>
<pre><code class="lang-html">      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"grid layout"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item1"</span>&gt;</span>ITEM 1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item2"</span>&gt;</span>ITEM 2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item3"</span>&gt;</span>ITEM 3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item4"</span>&gt;</span>ITEM 4<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.grid</span>{
    <span class="hljs-attribute">display</span>: grid;
    <span class="hljs-attribute">gap</span>: <span class="hljs-number">10px</span>;
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">3</span>,<span class="hljs-number">1</span>fr);
    <span class="hljs-attribute">grid-template-areas</span>:
        <span class="hljs-string">"i1 i1 i2"</span>
        <span class="hljs-string">"i1 i1 i2"</span>
        <span class="hljs-string">"i3 i3 i3"</span>
        <span class="hljs-string">"i4 i4 i4"</span>;
}

<span class="hljs-selector-class">.item1</span>{
    <span class="hljs-attribute">grid-area</span>: i1;
}
<span class="hljs-selector-class">.item2</span>{
    <span class="hljs-attribute">grid-area</span>: i2;
}

<span class="hljs-selector-class">.item3</span>{
    <span class="hljs-attribute">grid-area</span>: i3;
}

<span class="hljs-selector-class">.item4</span>{
    <span class="hljs-attribute">grid-area</span>: i4;
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1726550550476/2bdcb8d6-ec12-49ec-a742-1d5db6c76fad.png" alt class="image--center mx-auto" /></p>
<p>Syntax:</p>
<pre><code class="lang-plaintext">grid-template-areas: "a b";
grid-template-areas:
  "a b ."
  "a c d";
</code></pre>
<p><code>grid-area</code> is used to name the element of grid which we want to place in the grid layout(Here, I have used i1, i2, i3, i4). After from this, <code>grid-template-areas</code> supports some global values like <code>inherit</code>, <code>initial</code>, <code>revert</code>, <code>revert-layer</code>, <code>unset</code>.</p>
<h3 id="heading-alignment-properties">Alignment Properties:</h3>
<p>CSS Grid has two axes—block (vertical) and inline (horizontal). The alignment properties allow you to align elements along these axes:</p>
<ul>
<li><code>align-items</code> : aligns items on the block axis.</li>
</ul>
<ul>
<li><p><code>justify-items</code> : aligns items along the inline axis.</p>
</li>
<li><p><code>align-content</code>: aligns items along the block axis when there is extra space.</p>
</li>
<li><p><code>justify-content</code> : aligns items on the inline axis when there is extra space.</p>
</li>
<li><p><code>align-self</code>: Aligns an individual item on the block axis.</p>
</li>
<li><p><code>justify-self</code>: Aligns an individual item on the inline axis.</p>
</li>
</ul>
<h3 id="heading-conclusion">Conclusion:</h3>
<p>This blog covers essential concepts related to CSS Grid Layout. After reading this blog, you should have a solid understanding of all necessary properties required to design web pages using CSS Grid Layout.</p>
<h4 id="heading-points-to-remember">Points to remember:</h4>
<ul>
<li><p>It is possible to nest a grid within another grid, by using <code>grid-template-rows: subgrid</code> or <code>grid-template-columns: subgrid</code> for the nested grid.</p>
</li>
<li><p>Grid functionality is already included in the specification and is supported by most modern browsers.</p>
</li>
</ul>
<h3 id="heading-code">Code:</h3>
<p>You can use this code snippet to experiment with CSS Grid Layout:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"styles.css"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"grid layout"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> &gt;</span>ITEM 1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> &gt;</span>ITEM 2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> &gt;</span>ITEM 3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> &gt;</span>ITEM 4<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<pre><code class="lang-css">*{
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100vw</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">233</span>, <span class="hljs-number">230</span>, <span class="hljs-number">225</span>);
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">2rem</span>;
}

<span class="hljs-selector-class">.grid</span>{
    <span class="hljs-attribute">display</span>: grid;
    <span class="hljs-attribute">gap</span>: <span class="hljs-number">10px</span>;
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">3</span>,<span class="hljs-number">100px</span>);
    <span class="hljs-attribute">grid-template-rows</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">3</span>,<span class="hljs-number">1</span>fr);
    <span class="hljs-attribute">grid-auto-flow</span>: column;
}


<span class="hljs-selector-class">.layout</span>{
    <span class="hljs-attribute">width</span>: <span class="hljs-number">80vw</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">80vh</span>;
    <span class="hljs-attribute">background-color</span>:gray;
    <span class="hljs-attribute">margin</span>: auto ;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">1rem</span>;
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">20px</span>;
    <span class="hljs-attribute">-webkit-border-radius</span>: <span class="hljs-number">20px</span>;
    <span class="hljs-attribute">-moz-border-radius</span>: <span class="hljs-number">20px</span>;
    <span class="hljs-attribute">-ms-border-radius</span>: <span class="hljs-number">20px</span>;
    <span class="hljs-attribute">-o-border-radius</span>: <span class="hljs-number">20px</span>;
}

<span class="hljs-selector-class">.layout</span>&gt;<span class="hljs-selector-tag">div</span>{
    <span class="hljs-attribute">background-color</span>: beige;
    <span class="hljs-attribute">text-align</span>: center;
    <span class="hljs-attribute">box-sizing</span>: border-box;
}
</code></pre>
<p>Thank you for your time! Please let me know if there are any opportunities for improvement in this blog or any additions I can make in the comment section.</p>
]]></content:encoded></item></channel></rss>