<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Asmaa Hamdy</title>
    <description>The latest articles on DEV Community by Asmaa Hamdy (@asmaahamdym).</description>
    <link>https://dev.to/asmaahamdym</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1433957%2F32e11fb0-04f9-4ea2-b318-19ec36174b89.jpeg</url>
      <title>DEV Community: Asmaa Hamdy</title>
      <link>https://dev.to/asmaahamdym</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asmaahamdym"/>
    <language>en</language>
    <item>
      <title>Navigating the Web: A Guide to the DOM</title>
      <dc:creator>Asmaa Hamdy</dc:creator>
      <pubDate>Tue, 07 May 2024 21:10:03 +0000</pubDate>
      <link>https://dev.to/asmaahamdym/navigating-the-web-a-guide-to-the-dom-10j2</link>
      <guid>https://dev.to/asmaahamdym/navigating-the-web-a-guide-to-the-dom-10j2</guid>
      <description>&lt;h2&gt;
  
  
  What is the DOM?
&lt;/h2&gt;

&lt;p&gt;If you have been learning HTML and CSS then moving to JavaScript, you might have heard about the DOM(Document Object Model). Maybe with a vague understanding of what it means.&lt;br&gt;
Today we will break down what the DOM actually is.&lt;/p&gt;

&lt;p&gt;All the website on the internet rely on three key components HTML, CSS and JavaScript. These need to work together to make the magic happen. HTML lays the basic backbone and CSS make things look pretty and JavaScript achieve interactivity.&lt;br&gt;
JavaScript can add more HTML to a page without the need to reload it and it can detect when an event happens on the page like a mouse click or a key press. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh6hzoai5oo30r4x4coez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh6hzoai5oo30r4x4coez.png" alt="javascript image" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But how exactly does JavaScript have access to HTML elements? It's the DOM!&lt;br&gt;
The DOM is a tree-like representation of the web page that gets loaded into the browser. If we are to change something in HTML, the DOM changes as well. The browser takes care of that synchronization.&lt;/p&gt;

&lt;p&gt;For everything inside the HTML file, there is a different representation for it as an object or node; that way, a programming language like JavaScript can interact with the page.&lt;/p&gt;

&lt;p&gt;The DOM is a web API that's part of the browser and is used to build websites.&lt;br&gt;
.&lt;br&gt;
The top object of the DOM holds the entire page is the &lt;strong&gt;Document&lt;/strong&gt; object which nests inside the rest of the objects representing the page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Traversing the DOM:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Document.querySelector(selectors)&lt;/strong&gt;: This method will return an element object of the &lt;strong&gt;first&lt;/strong&gt; element in the document that matches the specified selector.
Here's an example:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxilw9pvdwxye89dnyv3p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxilw9pvdwxye89dnyv3p.png" alt="querySelector example" width="800" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;as you can see, I used the element selector to get the h1 element.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Document.querySelectorAll()&lt;/strong&gt;: This method returns a NodeList of all elements in the document that match the specified CSS selector. Here it will return a collection of two p element nodes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F23d4izw38ifnreljz2c0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F23d4izw38ifnreljz2c0.png" alt="querySelectorAll example" width="800" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Document.createElement()&lt;/strong&gt;: This method creates a new HTML element with the specified tag name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Element.innerHTML&lt;/strong&gt;: This property sets or returns the HTML content inside the selected element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Element.setAttribute()&lt;/strong&gt;: This method sets the value of a specified attribute on the current element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Element.getAttribute()&lt;/strong&gt;: This method returns the value of a specified attribute on the current element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;EventTarget.addEventListener()&lt;/strong&gt;: This method attaches an event listener to the specified event target (e.g., a DOM element).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;HTMLElement.style&lt;/strong&gt;: This property represents the inline style of an HTML element and can be used to get or set CSS properties.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Node.appendChild()&lt;/strong&gt;: This method adds a new child node to the end of the list of children of a specified parent node.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;window.onload&lt;/strong&gt;: This event is fired when the initial HTML document has finished loading.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;window.scrollTo()&lt;/strong&gt;: This method scrolls the window to a particular set of coordinates in the document.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and many more others...&lt;/p&gt;

&lt;p&gt;In summary, the DOM is a crucial part of how JavaScript interacts with and manipulates web pages. It provides a tree-like representation of the HTML structure, allowing developers to access and modify individual elements programmatically.&lt;br&gt;
The DOM is a fundamental concept in front-end web development, as it forms the bridge between the static HTML/CSS structure and the dynamic, interactive functionality powered by JavaScript. Mastering DOM manipulation is an essential skill for any aspiring web developer, as it unlocks the ability to create engaging and user-friendly web experiences. With the knowledge provided in this article, you now have a solid foundation to continue exploring and leveraging the power of the Document Object Model in your future projects.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>CSS Specificity: Why Your Div Isn't Blue? 👩‍💻</title>
      <dc:creator>Asmaa Hamdy</dc:creator>
      <pubDate>Thu, 18 Apr 2024 21:58:15 +0000</pubDate>
      <link>https://dev.to/asmaahamdym/css-specificity-why-your-div-isnt-blue-llj</link>
      <guid>https://dev.to/asmaahamdym/css-specificity-why-your-div-isnt-blue-llj</guid>
      <description>&lt;p&gt;Being someone who leans more towards logic and coming from desktop development, CSS felt (and sometimes still feels) like a mysterious beast!&lt;/p&gt;

&lt;p&gt;Early on, I struggled to understand why my styles weren't always working. Then I discovered the concept of CSS specificity. It explains how browsers resolve conflicts when multiple rules target the same element. Basically, the browser has a set of criteria to determine which style rule takes precedence!&lt;/p&gt;

&lt;p&gt;To demonstrate here are five different divs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7r07zsrkwxusa81x9by4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7r07zsrkwxusa81x9by4.png" alt="Image description" width="800" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have targeted all of them with element selector and gave some background color and bottom margin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
  width: 100%;
  height: 100px;
  margin-bottom: 2px;
  background: blue;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How specificity work?
&lt;/h2&gt;

&lt;p&gt;I see CSS specificity and place value in math both determine which rule takes precedence. In place value, the leftmost digit holds the most weight, similar to how an ID selector in CSS has a higher specificity than a class selector. Just like how a number with a 3 in the hundreds place is greater than a number with a 3 in the tens place, a more specific CSS rule will override a more general one.&lt;br&gt;
so let take this number: &lt;em&gt;00000&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficbwnwq6u4g5zay19h19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficbwnwq6u4g5zay19h19.png" alt="Image description" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As Shown in the image above if we have an element or a pseudo-element in the selector, we get the value of 1 in the units place: &lt;em&gt;00001&lt;/em&gt; and the more we add we add them to the units value so for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div a {
  width: 100%;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we have &lt;em&gt;div&lt;/em&gt; and &lt;em&gt;a&lt;/em&gt; and both of them are elements so we get score of 2: &lt;em&gt;00002&lt;/em&gt;. and the pseudo-element has the same weight of 1 so this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div::after {
  width: 100%;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will also count as 2.&lt;/p&gt;

&lt;h2&gt;
  
  
  The tens place Value:
&lt;/h2&gt;

&lt;p&gt;The tens place we have the class and pseudo-class selectors.&lt;br&gt;
so if I did this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.element {
  background: red;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because this is a class selector the value becomes 10: &lt;em&gt;00010&lt;/em&gt;&lt;br&gt;
so it takes precedence over the element selector so we see all divs with the class element are now red.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbsumew0lx8ham5xvcqkv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbsumew0lx8ham5xvcqkv.png" alt="Image description" width="800" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and if we changed our minds and do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.element {
  background: red;
}
.element {
  background: green;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because both selectors have the same specificity, the last one will be applied as you see here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhe70iscaf0g4fxv8qftr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhe70iscaf0g4fxv8qftr.png" alt="Image description" width="800" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we want to first one(Red) to be applied we shall increase it's specificity for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div.element {
  background: red;
}
.element {
  background: green;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding the div will increase the value to 11 so the first 4 divs will go back to red.&lt;/p&gt;

&lt;h2&gt;
  
  
  Another example with data attribute:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyunxmloum8pyvesfg787.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyunxmloum8pyvesfg787.png" alt="Image description" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last selector has the same specificity value as the one in line 7 but because it's last it will be applied over the element with the data attribute of name.&lt;/p&gt;

&lt;p&gt;And the more classes we add to the selector, the more specificity value we're adding&lt;br&gt;
For example the selector on line 7 will have the value of 21 so it would take precedence:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ve956c32q1h8hi7rh27.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ve956c32q1h8hi7rh27.png" alt="Image description" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The hundreds place Value:
&lt;/h2&gt;

&lt;p&gt;This is reserved for id selector, it has the value of 100 &lt;em&gt;00100&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxk332gn8l5jt8lce8som.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxk332gn8l5jt8lce8som.png" alt="Image description" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we added classes selector to it or pseudo-class selector that would add to the tens place.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Thousands place:
&lt;/h2&gt;

&lt;p&gt;This is for the inline style tag. It takes the specifitcty value of &lt;em&gt;01000&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo13uqo4vbn5ze4dci1fq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo13uqo4vbn5ze4dci1fq.png" alt="Image description" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The !important exception:
&lt;/h2&gt;

&lt;p&gt;Having the &lt;em&gt;!important&lt;/em&gt; flag, the important declaration is applied no matter the specificity. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flv1emtpad3y44w8dl4cw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flv1emtpad3y44w8dl4cw.png" alt="Image description" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using !important to override specificity is considered a bad practice and should be avoided for this purpose. Understanding and effectively using specificity and the cascade can remove any need for the !important flag.&lt;/p&gt;

&lt;p&gt;In a nutshell, the more specific the selector is the more it will have precedence over other conflicting selectors. &lt;/p&gt;

&lt;p&gt;Here are some resources for CSS specificity weight calculations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href="https://codepen.io/Asmaa-Hamdy-/pen/yLrRRog" rel="noopener noreferrer"&gt;CodePen&lt;/a&gt; demonstrated above&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://specificity.keegan.st/" rel="noopener noreferrer"&gt;Specificity Calculator&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://css-tricks.com/specifics-on-css-specificity/" rel="noopener noreferrer"&gt;CSS tricks&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Specifishity 😂:
&lt;a href="https://specifishity.com/" rel="noopener noreferrer"&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm7fpn8p5gpw1xzwtqxwb.png" alt="Specifishity " width="610" height="780"&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>learning</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
