I used basics of CSS like box model to solve this challenge
Design comparison
Solution retrospective
- I tried to get the div container that is holding the whole content to center by winging the margin-left number, how could I had done better.
- I had a hard time it responsive for mobile view, I had to google it, Did I get close to doing correctly?
Community feedback
- @SadeeshaJayaweeraPosted about 1 year ago
Congratulations on completing the challenge & all the very Best ✌️
It sounds like you were trying to center a
<div>
container that holds your content by adjusting themargin-left
property. While this approach can work in some cases, it may not be the most responsive and flexible way to achieve centering, especially for mobile views. There are more modern and responsive techniques you can use for centering content. Here's a better approach:-
Flexbox Centering: One of the most popular and responsive methods for centering content is using flexbox. To center a
<div>
horizontally and vertically, you can apply the following CSS to the container:.container { display: flex; justify-content: center; /* Center horizontally */ align-items: center; /* Center vertically */ }
This will work well for various screen sizes, including mobile, without having to set specific margin values.
-
CSS Grid Centering: CSS Grid is another powerful tool for layout control. You can use it to center content as well. Here's an example:
.container { display: grid; place-items: center; }
This will center the content both horizontally and vertically within the container.
-
Margin: 0 auto for Block-Level Elements: If you want to center a block-level element (like a
<div>
) horizontally, you can also use themargin: 0 auto;
technique. This method is useful when you want to center a block horizontally within its parent container..container { margin: 0 auto; /* Center horizontally within parent */ }
To center vertically as well, you can combine this with flexbox or grid as mentioned above.
For responsiveness, it's also essential to consider using media queries to adjust your layout for different screen sizes. Media queries allow you to apply different CSS styles depending on the screen width, ensuring your content looks good on both desktop and mobile devices.
Here's a basic example of a media query for mobile responsiveness:
@media (max-width: 768px) { /* Your mobile-specific CSS styles go here */ }
Marked as helpful1 -
- @NectarHubPosted about 1 year ago
It will center horizontally and also some give margin-top
.container{ margin : 0 auto; }0
Please log in to post a comment
Log in with GitHubJoin 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