Close Menu

    Subscribe to Updates

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

    What's Hot

    How to create Snake Volume Slider using HTML CSS and JS

    22 March 2026

    How to create Stick Hero Game using HTML CSS and JS

    19 March 2026

    How to make Netflix Login page using HTML & CSS

    18 March 2026
    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

    JavaScript

    How to create Snake Volume Slider using HTML CSS and JS

    22 March 2026
    JavaScript

    How to create Stick Hero Game using HTML CSS and JS

    19 March 2026
    JavaScript

    How to create Animated Add to Cart Button using HTML CSS and JS

    15 March 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Trending Post

    Master Frontend in 100 Days Ebook

    2 March 202432K Views

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

    11 January 202431K Views

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

    14 February 202424K Views

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

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

    How to create Valentine’s Day Book using HTML CSS & JavaScript

    13 February 2025

    Is it Good to be a Full Stack Developer?

    24 January 2024

    How to create Animated 404 Page not found using HTML and CSS

    29 July 2025

    How to make Squid Game Prize Counter using HTML CSS & JavaScript

    2 April 2024
    Latest Post

    How to create Snake Volume Slider using HTML CSS and JS

    22 March 2026

    How to create Stick Hero Game using HTML CSS and JS

    19 March 2026

    How to make Netflix Login page using HTML & CSS

    18 March 2026

    How to create Animated Add to Cart Button using HTML CSS and JS

    15 March 2026
    Facebook X (Twitter) Instagram YouTube
    • About Us
    • Privacy Policy
    • Return and Refund Policy
    • Terms and Conditions
    • Contact Us
    • Buy me a coffee
    © 2026 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