Center div horizontally and vertically

Joined
Sep 20, 2023
Messages
1
Reaction score
0
How can I center a div in the middle of the website so that it is centered horizontally & vertically?
 
Joined
Aug 22, 2023
Messages
57
Reaction score
14
Assign particular margin characteristics!
Code:
<style>
Assign margin:auto to assigned div name.
</style>
 
Joined
Jul 4, 2023
Messages
480
Reaction score
60
For
HTML:
<body>
    <div>Hello World</div>
</body>

[ on-line ]
CSS:
div {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: transform(-50%, -50%);
}

[ on-line ]
CSS:
body {
    display: grid;
    place-items: center;
}

[ on-line ]
CSS:
body {
    display: flex;
    justify-content: center;
    align-items: center;
}

[ on-line ]
CSS:
body {
    display: flex;    
    align-items: center;
}
div {
    /*margin: 0 auto;*/
    margin-inline: auto;
}

[ on-line ]
CSS:
body {
    display: table;
    width: 100%;
    height: 100%;
}
div {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}

[ on-line ]
CSS:
div {
    text-align: center;
    width: 100%;
    height: 100%;
    font-size: 1.5rem;
    line-height: 100vh;
}

[ on-line ]
CSS:
body {
    /*display: flex;*/
    display: grid;
}
div {
    margin: auto;
}
 
Last edited:
Joined
Nov 13, 2020
Messages
302
Reaction score
38
Of all the great responses that VB gave you, the one I have used 100% of the time is
body {
display: flex;
justify-content: center;
align-items: center;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top