How to center the elements with CSS ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to center the elements with CSS ?

Share the different ways to center the elements in html5 and CSS3. let's see you will do it best by using easy technique. :)

1st Nov 2016, 6:01 PM
sahil gupta
sahil gupta - avatar
3 Answers
+ 1
hmm smart.. that's pretty easy but i think text-align property only works for x-axis. what about y-axis?? anyway I appreciate your answer .thanx :)
1st Nov 2016, 6:11 PM
sahil gupta
sahil gupta - avatar
+ 1
ahh.. wait a minute.we are talking about css and html right ? what i mean bt y-axis is .if we have an div with height of 100px(for example ) and we want to place an h1 tag at exactly 50px from top? solution: h1{ margin-top: 50px ; } But what if we have an div element who's height is keep on changing on a document by certain actions . then what ? solution : h1{ position : absolute; top:50%; transform : translateY(-50%); } this is how it can be done :) if you like our answer follow us,it will just take a second. ;)
1st Nov 2016, 6:31 PM
sahil gupta
sahil gupta - avatar
+ 1
Different ways... I know four ways to do it • <center> (deprecated) • text-align: center (recommended if you want to center text) • margin:auto • display:flex; align-items:center • position: absolute; left:50%; right: 50% (not recommended)
1st Nov 2016, 6:35 PM
Unai Mengual
Unai Mengual - avatar