Centering a div is the most impossible thing
Flexbox is a modern way to center content both vertically and horizontally:
.container { display: flex; justify-content: center; align-items: center; height: 100vh; }
Centered Content
CSS Grid can also center content:
.container { display: grid; place-items: center; height: 100vh; }
Centered Content
You can center a div using absolute positioning:
.container { position: relative; height: 100vh; } .centered-div { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
Centered Content
For simple horizontal centering, use margin: auto:
.centered-div { width: 50%; margin: 0 auto; }
Centered Content
For inline or inline-block elements:
.container { text-align: center; line-height: 100vh; } .centered-div { display: inline-block; vertical-align: middle; line-height: normal; }
Centered Content
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3