4 ways to center a component/div in CSS

Flexbox
Grid
Margin auto on a flex item
Position

/* Using Flexbox */
.parent {
display: flex;
justify-content: center;
align-items: center;
}

/* Grid */
.parent {
display: grid;
justify-content: center;
align-items: center;
}

/* Gr…


This content originally appeared on DEV Community and was authored by DEV Community

  • Flexbox
  • Grid
  • Margin auto on a flex item
  • Position
/* Using Flexbox */
.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Grid */
.parent {
  display: grid;
  justify-content: center;
  align-items: center;
}

/* Grid */
.parent {
  display: grid;
  place-items: center;
}

/* margin auto on a flex items */

.parent {
  display: flex;
}
.child {
  margin: auto;
}

/* Using Position */
.parent {
  position: relative;
}

.child {
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(-50%, -50%); /* OR right: 0; bottom: 0; */
}


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-08T14:38:16+00:00) 4 ways to center a component/div in CSS. Retrieved from https://www.scien.cx/2022/03/08/4-ways-to-center-a-component-div-in-css/

MLA
" » 4 ways to center a component/div in CSS." DEV Community | Sciencx - Tuesday March 8, 2022, https://www.scien.cx/2022/03/08/4-ways-to-center-a-component-div-in-css/
HARVARD
DEV Community | Sciencx Tuesday March 8, 2022 » 4 ways to center a component/div in CSS., viewed ,<https://www.scien.cx/2022/03/08/4-ways-to-center-a-component-div-in-css/>
VANCOUVER
DEV Community | Sciencx - » 4 ways to center a component/div in CSS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/08/4-ways-to-center-a-component-div-in-css/
CHICAGO
" » 4 ways to center a component/div in CSS." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/08/4-ways-to-center-a-component-div-in-css/
IEEE
" » 4 ways to center a component/div in CSS." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/08/4-ways-to-center-a-component-div-in-css/. [Accessed: ]
rf:citation
» 4 ways to center a component/div in CSS | DEV Community | Sciencx | https://www.scien.cx/2022/03/08/4-ways-to-center-a-component-div-in-css/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.