<?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: Nwulu Uchechukwu Prince</title>
    <description>The latest articles on DEV Community by Nwulu Uchechukwu Prince (@uchedotphp).</description>
    <link>https://dev.to/uchedotphp</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%2F465627%2F086c6069-4e07-4cb8-9b69-bbd676bf1329.jpeg</url>
      <title>DEV Community: Nwulu Uchechukwu Prince</title>
      <link>https://dev.to/uchedotphp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/uchedotphp"/>
    <language>en</language>
    <item>
      <title>🧱 Ensuring Full-Height Layouts: Why Your First Child Isn’t Stretching Like You Think</title>
      <dc:creator>Nwulu Uchechukwu Prince</dc:creator>
      <pubDate>Sat, 24 May 2025 07:08:16 +0000</pubDate>
      <link>https://dev.to/uchedotphp/ensuring-full-height-layouts-why-your-first-child-isnt-stretching-like-you-think-16ha</link>
      <guid>https://dev.to/uchedotphp/ensuring-full-height-layouts-why-your-first-child-isnt-stretching-like-you-think-16ha</guid>
      <description>&lt;p&gt;When building landing pages or hero sections, a common expectation is that the first &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; inside the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; should stretch to the full height of the viewport. Simple, right? Just slap on &lt;code&gt;height: 100%&lt;/code&gt; and move on.&lt;br&gt;
But many frontend developers quickly realize... it doesn’t work.&lt;/p&gt;

&lt;p&gt;In this post, I’ll walk you through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why this layout bug happens&lt;/li&gt;
&lt;li&gt;The correct way to fix it&lt;/li&gt;
&lt;li&gt;Real-world use cases&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Bonus tip: Why &lt;code&gt;:root { height: 100% }&lt;/code&gt; might help when &lt;code&gt;html { height: 100% }&lt;/code&gt; doesn’t.
&lt;/h2&gt;
&lt;h2&gt;
  
  
  🎯 The Common Setup
&lt;/h2&gt;

&lt;p&gt;A developer might start with this HTML/CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
  &amp;lt;div class="main-content"&amp;gt;Hello, World!&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.main-content {
  height: 100%;
  background: lightblue;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Expected&lt;/u&gt;&lt;/strong&gt;: The blue background fills the screen&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Actual&lt;/u&gt;&lt;/strong&gt;: The content takes only as much height as its children — definitely not 100% of the viewport.&lt;/p&gt;
&lt;h2&gt;
  
  
  ❌ Why It Doesn’t Work
&lt;/h2&gt;

&lt;p&gt;Because &lt;code&gt;height: 100%&lt;/code&gt; on .main-content means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Be 100% as tall as my parent"&lt;br&gt;
But if neither html nor body has an explicit height, &lt;code&gt;.main-content&lt;/code&gt; has no reference point to calculate that 100%.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  ✅ The Correct Solution
&lt;/h2&gt;

&lt;p&gt;Set the height of html and body to 100%:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;html,
body {
  height: 100%;
  margin: 0;
}

body &amp;gt; div:first-child {
  min-height: 100%;
  background: lightblue;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;💡 min-height allows the content to grow if necessary but still ensures full viewport coverage.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🆚 min-height: 100% vs 100vh
&lt;/h2&gt;

&lt;p&gt;Both are valid, but behave slightly differently:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;100vh is fixed to the viewport height (useful for hero sections).&lt;/li&gt;
&lt;li&gt;min-height: 100% is relative to the parent’s height (better for layouts that might grow with content.
Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.main-content {
  min-height: 100vh; /* or use 100% with proper html/body setup */
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ⚠️ Bonus: When &lt;code&gt;:root { height: 100% }&lt;/code&gt; Works But html &lt;code&gt;{ height: 100% }&lt;/code&gt; Doesn’t
&lt;/h2&gt;

&lt;p&gt;In component-based frameworks (like Vue, Nuxt, or React), styles might be scoped, meaning selectors like &lt;code&gt;html&lt;/code&gt; or &lt;code&gt;body&lt;/code&gt; don’t apply unless explicitly defined in global CSS.&lt;/p&gt;

&lt;p&gt;Why &lt;code&gt;:root&lt;/code&gt; sometimes works better:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It selects the html element but has slightly higher specificity&lt;/li&gt;
&lt;li&gt;It’s less likely to be overridden by resets or scoped styles
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Use &lt;code&gt;:root&lt;/code&gt; as a fallback or if you’re unsure about where your global styles are applied in your app structure.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🧠 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Layout bugs like this can be frustrating, especially when the fix seems “obvious.” But understanding inheritance, box models, and scope helps demystify these quirks.&lt;/p&gt;

&lt;p&gt;Next time your section isn’t filling the screen, ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Did I set height: 100% on both html and body?&lt;/li&gt;
&lt;li&gt;Is my layout in a scoped or modular system like Vue?&lt;/li&gt;
&lt;li&gt;Should I use :root for more reliable global styling?&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>html</category>
      <category>css</category>
    </item>
  </channel>
</rss>
