<?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: Okputu Emmanuel Okputu</title>
    <description>The latest articles on DEV Community by Okputu Emmanuel Okputu (@okputu-e).</description>
    <link>https://dev.to/okputu-e</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%2F446557%2F11334416-9d03-4278-9aca-34a6244f3bb4.jpeg</url>
      <title>DEV Community: Okputu Emmanuel Okputu</title>
      <link>https://dev.to/okputu-e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/okputu-e"/>
    <language>en</language>
    <item>
      <title>Collapsing Margins in CSS: A Beginner's Guide</title>
      <dc:creator>Okputu Emmanuel Okputu</dc:creator>
      <pubDate>Tue, 24 Sep 2024 10:03:00 +0000</pubDate>
      <link>https://dev.to/okputu-e/collapsing-margins-in-css-a-beginners-guide-41bn</link>
      <guid>https://dev.to/okputu-e/collapsing-margins-in-css-a-beginners-guide-41bn</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;There's nothing more frustrating than applying a style and having it not work as expected. Collapsing Margin is one phenomenon that causes unexpected styling outcomes. Thus, what is a collapsing margin and how does it affect margins in CSS?&lt;/p&gt;

&lt;h2&gt;
  
  
  Collapsing Margin
&lt;/h2&gt;

&lt;p&gt;Collapsing margins stem from the CSS property margin, which controls the spacing outside an element. As the name suggests, collapsing margins occur when the margins of adjacent elements combine or 'collapse' into one, rather than adding up. This typically happens between sibling elements or between a parent and child element. For instance, if two sibling elements have margins—one with a &lt;code&gt;20px&lt;/code&gt;bottom margin and the other with a &lt;code&gt;30px&lt;/code&gt; top margin—you might expect the total margin to be &lt;code&gt;50px&lt;/code&gt;. However, due to collapsing margins, only the larger margin of &lt;code&gt;30px&lt;/code&gt; will be applied, and the smaller margin of &lt;code&gt;20px&lt;/code&gt; will be collapsed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.element1 {
    margin-bottom: 20px;
}
.element2 {
    margin-top: 30px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Moreover, in cases where a parent element lacks padding or border, and its child element possesses a top margin, the margin may "collapse" beyond the parent, thereby impacting the parent's placement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.parent {
    margin-top: 0;
}
.child {
    margin-top: 20px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;20px&lt;/code&gt; top margin from the .child might collapse outside of the .parent, moving the entire parent down by &lt;code&gt;20px&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This can confuse noob developers (well I never knew about it until recently), as the resulting spacing may not match their expectations. &lt;/p&gt;

&lt;h2&gt;
  
  
  Ways to work around it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Add Padding or Border: adding a small amount of padding or a border to the parent element will prevent margin collapse.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.parent {
    padding-top: 1px; /* or border-top: 1px solid transparent; */
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Use of the overflow property: When the overflow property of the parent is set to anything other than visible (e.g., overflow: hidden;), it can prevent margin collapsing.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.parent {
    overflow: hidden;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Use Flexbox or Grid Layouts: These layout methods avoid margin collapsing issues by design. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Flexbox example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.parent {
    display: flex; /* Flexbox layout */
    flex-direction: column; /* Stack children vertically */
    border: 1px solid #000; /* Just for visibility */
}

.child1, .child2 {
    margin: 20px 0; /* Vertical margins that won't collapse */
    background-color: lightblue;
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Grid Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.parent {
   display: grid; /* Grid layout */
   grid-template-rows: auto auto; /* Define two rows */
   border: 1px solid #000; /* Just for visibility */
 }

.child1, .child2 {
     margin: 20px 0; /* Vertical margins */
     background-color: lightgreen;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Use Float or Inline-Block: Floating the child element or setting it to display: inline-block; will also prevent margins from collapsing. However, I rarely see float anymore but it is good to know in case of legacy code base.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Float example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.parent {
    border: 1px solid #000; /* Just for visibility */
 }

 .child1, .child2 {
     float: left; /* Prevents margin collapsing */
     width: 100%; /* Full width */
     margin: 20px 0; /* Vertical margins */
     background-color: lightcoral;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Inline block example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.parent {
   border: 1px solid #000; /* Just for visibility */
}

.child1, .child2 {
    display: inline-block; /* Prevents margin collapsing */
    width: 100%; /* Full width */
    margin: 20px 0; /* Vertical margins */
    background-color: lightyellow;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding margin collapsing can significantly contribute to consistent and expected spacing in your web layout, particularly when working with block-level elements. It's worth noting that inline elements such as &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;strong&amp;gt;&lt;/code&gt;, etc. are generally unaffected by margin collapsing due to their distinct behavior in generating vertical margins compared to block-level elements. Margin collapsing primarily poses challenges with block-level elements, such as &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;, and others, as they may have vertical margins that interact with each other.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_box_model/Mastering_margin_collapsing" rel="noopener noreferrer"&gt;Mdn&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/css/css_margin_collapse.asp" rel="noopener noreferrer"&gt;w3Schools&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>css</category>
    </item>
    <item>
      <title>General vs. Semantic Tags in HTML: Why Structure Matters</title>
      <dc:creator>Okputu Emmanuel Okputu</dc:creator>
      <pubDate>Sun, 22 Sep 2024 08:39:07 +0000</pubDate>
      <link>https://dev.to/okputu-e/general-vs-semantic-tags-in-html-why-structure-matters-3imn</link>
      <guid>https://dev.to/okputu-e/general-vs-semantic-tags-in-html-why-structure-matters-3imn</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;It’s common to begin learning HTML by relying heavily on general tags like &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; for structuring various parts of a webpage, such as sections, buttons, paragraphs, and containers. However, HTML serves as the skeleton of every front-end development project, much like the human body has visible, identifiable parts. To ensure that this structure is meaningful and easy to understand, both for humans and machines, it’s essential to use semantic HTML.&lt;/p&gt;

&lt;h2&gt;
  
  
  General vs Semantic Tags
&lt;/h2&gt;

&lt;p&gt;General tags are non-semantic tags that do not convey specific meaning about their content. In contrast, semantic tags are designed to provide clear, meaningful context about the information they enclose. Additionally, the distinction between general and semantic tags can be made based on their intended usage in web development, where semantic tags enhance accessibility and improve search engine optimization by conveying the structure and significance of the content.&lt;/p&gt;

&lt;p&gt;By definition, general tags include elements like &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;, which lack specific meaning regarding the content they enclose. In contrast, semantic tags—such as &lt;code&gt;&amp;lt;menu&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt;, and &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt;— are specifically designed to describe the role of the content they contain, providing both context and meaning.&lt;/p&gt;

&lt;h4&gt;
  
  
  Code Example (General Tags by Definition):
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt; &amp;lt;!-- non-semantic, should be nav --&amp;gt;
  &amp;lt;ul&amp;gt; &amp;lt;!-- non-semantic, should be menu --&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Home&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;About&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Contact&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Code Example (Semantic Tags by Definition):
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;nav&amp;gt; 
  &amp;lt;menu&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Home&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;About&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Contact&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;/menu&amp;gt;
&amp;lt;/nav&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By usage, general tags might include using &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;h2&amp;gt;&lt;/code&gt; without adhering to the correct heading hierarchy, omitting labels for form inputs, or using a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; to contain navigation links when a &lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt; tag would be more appropriate. Similarly, employing a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; for menu items, rather than utilizing the &lt;code&gt;&amp;lt;menu&amp;gt;&lt;/code&gt; tag, exemplifies the misuse of general tags. These practices can diminish the semantic value and accessibility of the markup.&lt;/p&gt;

&lt;h4&gt;
  
  
  Code Example (General Tags Misused by Usage):
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
  &amp;lt;h1&amp;gt;Main Heading&amp;lt;/h1&amp;gt;
  &amp;lt;h1&amp;gt;Subheading (incorrect)&amp;lt;/h1&amp;gt; &amp;lt;!-- Incorrect, should be h2 --&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;a href="#"&amp;gt;Home&amp;lt;/a&amp;gt;
    &amp;lt;a href="#"&amp;gt;About&amp;lt;/a&amp;gt;
    &amp;lt;a href="#"&amp;gt;Contact&amp;lt;/a&amp;gt;
  &amp;lt;/div&amp;gt; &amp;lt;!-- Incorrect, should use &amp;lt;nav&amp;gt; and &amp;lt;menu&amp;gt; instead --&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In contrast, semantic usage involves applying tags correctly for their intended purposes. For example, using the  tag to contain navigation links and ensuring that &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;h2&amp;gt;&lt;/code&gt;, and other heading tags follow a proper hierarchical structure exemplifies semantic usage. It also includes employing the &lt;code&gt;&amp;lt;menu&amp;gt;&lt;/code&gt; tag for list items intended for navigation or commands, rather than using a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt;. This proper application enhances the clarity and accessibility of the content.&lt;/p&gt;

&lt;h4&gt;
  
  
  Code Example (Semantic Tags Correct by Usage):
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;main&amp;gt;
  &amp;lt;h1&amp;gt;Main Heading&amp;lt;/h1&amp;gt;
  &amp;lt;h2&amp;gt;Subheading&amp;lt;/h2&amp;gt; &amp;lt;!-- Correct hierarchy --&amp;gt;
  &amp;lt;section&amp;gt;
    &amp;lt;p&amp;gt;Section content here...&amp;lt;/p&amp;gt;
  &amp;lt;/section&amp;gt;
&amp;lt;/main&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By definition, general tags such as &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; lack specific meaning, whereas semantic tags like &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt; describe the role of the content. By usage, general tags can be misapplied when proper conventions—such as heading hierarchy or navigation grouping—are ignored. Semantic usage involves applying the correct tags for their intended functions to enhance readability, accessibility, and SEO.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;p&gt;Below are some resources that I am currently using to break into the tech world:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://v2.scrimba.com/the-frontend-developer-career-path-c0j/~0xid" rel="noopener noreferrer"&gt;Scrimba&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/2022/responsive-web-design/" rel="noopener noreferrer"&gt;FreeCodeCamp&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>html</category>
      <category>seo</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
