Pathly
Home
Questions
Jobs
Community
Categories
Login
Register
How to center a div in CSS?
Asked by
Beginner User
105
• 1 month ago • 424 views
Frontend Development
3
Login to vote
I've been trying different methods to center a div horizontally and vertically but nothing seems to work perfectly. What's the best modern approach using Flexbox or Grid?
Solved
2 Answers
5
You can also use CSS Grid:
```css
.container {
display: grid;
place-items: center;
height: 100vh;
}
```
Even simpler than Flexbox!
Answered by
John Doe
1470
• 1 month ago
Best Answer
Marked by question author
7
The modern way is using Flexbox! Here's how:
```css
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
```
This will center both horizontally and vertically. Works great in all modern browsers!
Answered by
Jane Smith
700
• 1 month ago
Please
login
to post an answer.
Question Stats
Asked:
Jan 21, 2026
Views:
424
Votes:
3
Answers:
2
Beginner User
Frontend Development
105 Rep
Intermediate
Member since Jan 2026
Close Question
Reason (optional)