Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    How to make Social media icons hover effect using HTML & CSS

    14 May 2025

    How to make Awesome Radar Animation using HTML & CSS

    8 May 2025

    How to create Cross Road Game using HTML CSS and JS

    2 May 2025
    Facebook X (Twitter) Instagram YouTube Telegram Threads
    Coding StellaCoding Stella
    • Home
    • Blog
    • HTML & CSS
      • Login Form
    • JavaScript
    • Hire us!
    Coding StellaCoding Stella
    Home - Blog - 5 Simple Ways to Center a Div in CSS
    Blog

    5 Simple Ways to Center a Div in CSS

    Coding StellaBy Coding Stella24 January 2024Updated:25 January 2024No Comments3 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Email WhatsApp Copy Link

    How to Center a Div in CSS ? Let’s explore some straightforward techniques for centering a div without wasting much time.

    1. Flexbox Magic:

    Flexbox is a layout model in CSS that makes it easy to design complex layouts. In this method, the parent container becomes a flex container, and the align-items: center and justify-content: center properties ensure both horizontal and vertical centering.

       .container {
         display: flex;
         align-items: center;
         justify-content: center;
         height: 100vh;
       }
    
       div {
         width: 50%;
         height: 50%;
       }

    2. Grid Goodness:

    CSS Grid is a two-dimensional layout system. The parent container is set as a grid container, and the place-items: center property centers the div both horizontally and vertically.

       .container {
         display: grid;
         place-items: center;
         height: 100vh;
       }
    
       div {
         width: 50%;
         height: 50%;
       }

    3. Absolute Simplicity:

    This method utilizes absolute positioning and the transform property to center the div. The parent container is set to position: relative, and the child div is absolutely positioned at 50% from the top and left, then translated back by 50% of its own size.

       .container {
         position: relative;
         height: 100vh;
       }
    
       div {
         position: absolute;
         top: 50%;
         left: 50%;
         transform: translate(-50%, -50%);
         width: 50%;
         height: 50%;
       }

    4. Table-cell Trickery:

    By mimicking the behavior of a table, this method utilizes display: table for the container and display: table-cell for the div. The vertical-align: middle ensures vertical centering, and text-align: center handles horizontal centering.

       .container {
         display: table;
         height: 100vh;
         width: 100%;
       }
    
       div {
         display: table-cell;
         vertical-align: middle;
         text-align: center;
         width: 50%;
         height: 50%;
       }

    5. Line-height Charm:

    Leveraging the line-height property, this method centers the div vertically by setting the line height of the container to the full viewport height. The div, set as an inline-block, aligns vertically in the middle.

       .container {
         height: 100vh;
         width: 100%;
         text-align: center;
       }
    
       div {
         display: inline-block;
         line-height: 100vh;
         vertical-align: middle;
         width: 50%;
         height: 50%;
       }

    Conclusion:

    To sum it up, there are different ways to center a div both horizontally and vertically in CSS. You can use properties like display: flex and display: grid, or go for position: absolute with transform: translate. Another option is to use display: table and display: table-cell, or simply adjust the line-height.

    Each method has its own pros and cons, and the best choice depends on your project’s needs. Regardless of the method, centering a div in CSS can make your webpage look better and be more user-friendly.

    Centering Centering Elements Centering Techniques Code Snippets CSS Centering CSS Flexbox CSS Tricks Design Tips Div Centering Front-End Development HTML and CSS Responsive Design UI Design Web Design Web Development Web Layout
    Share. Copy Link Twitter Facebook LinkedIn Email WhatsApp
    Previous ArticleIs it Good to be a Full Stack Developer?
    Next Article Top 5 Source Code Editors in 2024
    Coding Stella
    • Website

    Related Posts

    HTML & CSS

    How to make Awesome Search Bar 2 using HTML & CSS

    4 April 2025
    Blog

    50 Projects in 50 Days – HTML/CSS and JavaScript

    23 February 2025
    Blog

    Frontend Developer Roadmap 2025: The Complete Guide

    31 January 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Trending Post

    Master Frontend in 100 Days Ebook

    2 March 202419K Views

    How to make Modern Login Form using HTML & CSS | Glassmorphism

    11 January 202416K Views

    How to make I love you Animation in HTML CSS & JavaScript

    14 February 202414K Views

    How to make Valentine’s Day Card using HTML & CSS

    13 February 202412K Views
    Follow Us
    • Instagram
    • Facebook
    • YouTube
    • Twitter
    ads
    Featured Post

    How to make Animated Bouncy Clock in HTML CSS & JavaScript

    24 March 2025

    How to create New Year Animation using HTML CSS & JavaScript

    29 December 2024

    How to make Magic Mouse Effect using HTML CSS & JavaScript

    24 April 2024

    How to make Glassmorphism Login Form with Various Theme in HTML CSS & JavaScript

    12 January 2024
    Latest Post

    How to make Social media icons hover effect using HTML & CSS

    14 May 2025

    How to make Awesome Radar Animation using HTML & CSS

    8 May 2025

    How to create Cross Road Game using HTML CSS and JS

    2 May 2025

    How to create 3D Animated Bee website using HTML CSS and JS

    28 April 2025
    Facebook X (Twitter) Instagram YouTube
    • About Us
    • Privacy Policy
    • Return and Refund Policy
    • Terms and Conditions
    • Contact Us
    • Buy me a coffee
    © 2025 Coding Stella. Made with 💙 by @coding.stella

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Looks like you're using an ad blocker. We rely on advertising to help fund our site.
    Okay! I understood