Design comparison
Solution retrospective
All feedback is welcome
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 */ }
In this query, styles inside the block will apply when the screen width is 768 pixels or less, allowing you to customize your layout for mobile devices.
0@ppatel6Posted about 1 year ago@SadeeshaJayaweera Thank you for letting me know about the other modern ways to center vertically and horizontally. Now onwards I will apply these new rules to my other solutions
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