HTML and CSS solution

Solution retrospective
I found centering the container horizontally and vertically difficulty, and making it also responsive to mobile version.
how its viewing in mobile version, its not centering.
Is there a good method of centering a div both vertically and horizontally both in desktop and mobile?
Please log in to post a comment
Log in with GitHubCommunity feedback
- @Hassiai
Replace <div class="div-totalContainer"> with the main tag and <p class="content-TopCopy"> with <h1> to fix the accessibility issues. click here for more on web-accessibility and semantic html
To center .div-container on the page, add min-height:100vh; display: flex; align-items: center: justify-content: center; or min-height:100vh; display: grid place-items: center to the body.
To center .div-container on the page using flexbox: body{ min-height: 100vh; display: flex; align-items: center; justify-content: center; }
To center .div-container on the page using grid: body{ min-height: 100vh; display: grid; place-items: center; }
There is no need to style .div-totalContainer, add text-align: center .div-container. Give .div-container a padding value for all the sides and increase the width value. e.g:
width: 320px, padding: 15px
.Give h1 and p the same font-size of 15px which is 0.9375em and the same margin-left, margin-right and margin-top values. Give p a margin bottom value.
Use relative units like rem or em as unit for the padding, margin, width values and preferably rem for the font-size values, instead of using px which is an absolute unit. For more on CSS units Click here
Hope am helpful.
Well done for completing this challenge. HAPPY CODING
- @sandro21-glitch
Hi godforyou
here is a common method to center a div both vertically and horizontally in CSS:
.center-div { display: flex; align-items: center; justify-content: center; height: 100vh; }
The display flex makes the div a flex container and align-items: center centers the div vertically, and justify-content: center centers the div horizontally. height: 100vh sets the height of the div to be 100% of the viewport height.
Note:
If you want to center the div only horizontally, you can use display: flex and justify-content: center.
If you want to center the div only vertically, you can use display: flex and align-items: center.
here are some helpful resources to learn about the display: flex property in CSS:
CSS-Tricks: A Comprehensive Guide to Flexbox
Mozilla Developer Network (MDN): Flexbox:
Happy Coding
Join our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord