In this tutorial will learn how to center div,span or any element horizontally using css3 in just few lines of code.
For align center we are using transform translate css property. see below image, you can see Main grey div in which one green div is align Horizontally centered.
So, lets start with css,
-moz-transform: translateX(-50%); /* For mozilla support */
-webkit-transform: translateX(-50%); /* For safari support */
-ms-transform: translateX(-50%); /* For IE support */
-o-transform: translateX(-50%); /* For opera support */
By applying position relative , left and transform we can easily align horizontally centered.
See full code with HTML & CSS
Hope this tutorial is helpful for you.
Meet Mukul, a passionate visionary and a dedicated 3D printing enthusiast. With an insatiable curiosity for technology and a flair for creativity, Mukul has discovered a world where innovation knows no bounds. Armed with a deep understanding of 3D printing and its endless possibilities, he has become a true pioneer in the field, constantly pushing the boundaries of what can be achieved with this remarkable technology.
Can’t you just use “margin: auto;” alone for the same purpose?
Yes we can do, by applying “margin : 0px auto;”.
but in some cases margin doesn’t work.
This one is just different approach for align horizontally center..
Good to see a different approach! Better to use “margin : 0px auto;”. But when you are using pop ups, this approach will be useful!
Yes right Joby Tom.