Close Menu

    Subscribe to Updates

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

    What's Hot

    How to make a New 9 Dot Navigation Menu in HTML CSS & JavaScript

    30 April 2026

    How to make Valentine Letter Animation using HTML CSS & JavaScript

    25 April 2026

    How to make Animated Caveman 404 Page using HTML and CSS

    22 April 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 make Valentine Letter Animation using HTML CSS & JavaScript

    25 April 2026
    JavaScript

    How to make Interactive Launch Order Button using HTML CSS & JavaScript

    17 April 2026
    JavaScript

    How to make Samsung S26 Ultra Privacy Display using HTML CSS & JavaScript

    12 April 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 make Animated Gaming Website GSAP in HTML CSS & JS

    20 December 2025

    How to make Animated Chair Product Page using HTML & CSS

    30 September 2025

    How to make Ball Leaping Loader using HTML & CSS

    21 February 2024

    Top 14 Game-Changing Websites Every Developer Should Know About

    27 January 2024
    Latest Post

    How to make a New 9 Dot Navigation Menu in HTML CSS & JavaScript

    30 April 2026

    How to make Valentine Letter Animation using HTML CSS & JavaScript

    25 April 2026

    How to make Animated Caveman 404 Page using HTML and CSS

    22 April 2026

    How to make Bicycle Loading Animation using HTML and CSS

    19 April 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