Skip to main content

How Can I Vertically Center a DIV Element in the Body of HTML?

Published: Asked By 1 Answers Answered

Learn how to vertically center a DIV element in the body of HTML using CSS techniques like Flexbox, Grid, and position properties. Simple examples included.

Share:

1 Answer

A
Amarjith AG Community Contributor Answered on

To vertically center a `div` in the body of HTML, you can use either Flexbox or CSS Grid:

*Flexbox:*
css
body {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0;
}


*CSS Grid:*
css
body {
    display: grid;
    place-items: center;
    height: 100vh;
    margin: 0;
}

Your Answer

Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certifications

Earn certifications to enhance your resume.