<?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: Hima varshini Yaleru</title>
    <description>The latest articles on DEV Community by Hima varshini Yaleru (@hima_varshiniyaleru_6c9d).</description>
    <link>https://dev.to/hima_varshiniyaleru_6c9d</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4090533%2F00b7760f-a76c-4461-b934-1ead62f4d211.jpg</url>
      <title>DEV Community: Hima varshini Yaleru</title>
      <link>https://dev.to/hima_varshiniyaleru_6c9d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hima_varshiniyaleru_6c9d"/>
    <language>en</language>
    <item>
      <title>How to Center a Div Horizontally and Vertically using CSS Flexbox</title>
      <dc:creator>Hima varshini Yaleru</dc:creator>
      <pubDate>Sun, 23 Aug 2026 07:25:19 +0000</pubDate>
      <link>https://dev.to/hima_varshiniyaleru_6c9d/how-to-center-a-div-horizontally-and-vertically-using-css-flexbox-3adn</link>
      <guid>https://dev.to/hima_varshiniyaleru_6c9d/how-to-center-a-div-horizontally-and-vertically-using-css-flexbox-3adn</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Aligning and centering design elements inside a web page used to require complex CSS hacks, precise pixel margins, or absolute positioning formulas. With the arrival of the CSS Flexbox Layout, centering child elements has become robust, responsive, and requires only three fundamental rules on the parent container.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Core Principle
&lt;/h3&gt;

&lt;p&gt;To properly align a 'div' using Flexbox, you must activate the layout engine on the parent element (the container holding the item) rather than writing layout instructions on the child element itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key CSS Properties Required
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;display: flex;&lt;/strong&gt; — This initializes the flex formatting context for all direct children.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;justify-content: center;&lt;/strong&gt; — This aligns child items precisely in the center along the horizontal main axis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;align-items: center;&lt;/strong&gt; — This aligns child items perfectly in the center along the vertical cross axis.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Practical Implementation Code
&lt;/h3&gt;

&lt;p&gt;Below is a clean, modern HTML5 and CSS structure you can use to center any element. You can copy and test this directly inside your local VS Code setup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Centering Elements with CSS Flexbox&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;.flex-parent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100vh&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* Fills full viewport height */&lt;/span&gt;
            &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#f7f9fa&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nc"&gt;.flex-child&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#007bff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#ffffff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Arial&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;6px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;4px&lt;/span&gt; &lt;span class="m"&gt;6px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-parent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Perfectly Centered Content!&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Walkthrough
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The container &lt;code&gt;.flex-parent&lt;/code&gt; acts as the flex wrapper. Setting its height to &lt;code&gt;100vh&lt;/code&gt; forces the container to occupy the absolute height of the user's viewport screen, making the vertical alignment visible.&lt;/li&gt;
&lt;li&gt;Once &lt;code&gt;display: flex&lt;/code&gt; is parsed by the browser, &lt;code&gt;justify-content&lt;/code&gt; shifts the blue inner card to the horizontal middle, while &lt;code&gt;align-items&lt;/code&gt; drops it down to the vertical midpoint cleanly.&lt;/li&gt;
&lt;/ul&gt;

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