CSS Centralize DIV

3 Lines CSS Centralize div

<div class="parent"> <div class="child"></div> </div>
Code language: HTML, XML (xml)
.parent { width: 100%; height: 250px; border: 1px solid grey; display: flex; align-items: center; justify-content: center; } .child { width: 50px; height: 50px; background: brown; }
Code language: CSS (css)

2 Lines CSS Centralize div

<div class="parent"> <div class="child"></div> </div>
Code language: HTML, XML (xml)
.parent { width: 100%; height: 250px; border: 1px solid grey; display: grid; place-items: center; } .child { width: 50px; height: 50px; background: lightgreen; }
Code language: CSS (css)

Using CSS Position

<div class="parent"> <div class="child"></div> </div>
Code language: HTML, XML (xml)
.parent { width: 100%; height: 250px; border: 1px solid grey; position: relative; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 50px; height: 50px; background: orange; }
Code language: CSS (css)

Hope this help you 🙂

Leave a Reply