1 Answers
Ways to center an element horizontally and vertically in CSS
There are several ways to center an element both horizontally and vertically using CSS:
- Using Flexbox: Set the parent container's display property to
flex
and usejustify-content: center;
andalign-items: center;
on the parent container. - Using Grid: Set the parent container's display property to
grid
and useplace-items: center;
on the parent container. - Absolute Positioning: Set the element to be centered using
position: absolute;
, and then usetop: 50%;
,left: 50%;
,transform: translate(-50%, -50%);
to center it. - CSS Table Display: Set the parent container's display property to
table;
and usedisplay: table-cell;
andvertical-align: middle;
on the child element. - Using CSS Grid with Auto Margins: Set the parent container's display property to
grid
and usemargin: auto;
on the centered element.
Please login or Register to submit your answer