How can we align a div element to center?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

How can we align a div element to center??

https://code.sololearn.com/WrZJZI6mOPyv/?ref=app How can I align this rectangle to center ??

11th Dec 2017, 1:01 PM
Arushi Singhania
Arushi Singhania - avatar
9 Answers
+ 12
11th Dec 2017, 1:36 PM
TwinkleMist
TwinkleMist - avatar
+ 9
If you want to centred it top no need those <br>
11th Dec 2017, 1:41 PM
TwinkleMist
TwinkleMist - avatar
+ 5
you can simply add <center> tag. <center> <div></div> </center> it will align div element to the center.
11th Dec 2017, 1:38 PM
Chamin Jayasooriya
Chamin Jayasooriya - avatar
+ 3
//I prefer css for this margin:0 auto; for inline elements display:block required
11th Jan 2019, 9:42 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 2
add these...to the div margin:0 auto; margin-top:30vh;
11th Dec 2017, 1:52 PM
Niush
Niush - avatar
+ 2
Thanks
12th Dec 2017, 10:08 AM
Arushi Singhania
Arushi Singhania - avatar
+ 2
thanks
13th Dec 2017, 3:00 AM
Arushi Singhania
Arushi Singhania - avatar
+ 1
well listen this, every div is block level element, that means that every block level element starts on a new line. You can set margin-left and margin-right to auto on div and it will center it, but width have to be 100% and box-sizing: border-box; so margin will affect div and not add margin to div width (div will be "example" 98% width and margins 1% :) If you want something to verticaly align in div, you have to set div to be inline-block , bcs vertical-align works only on inline elements. Hope this helps and sorry on bad language
12th Dec 2017, 10:31 PM
Дејан Борковић
Дејан Борковић - avatar
+ 1
Also note please, many answers recommend using the <center> tag. This tag has been deprecated in HTML 5 and may no longer work in new browser versions so please don't follow this advice.
13th Jan 2019, 9:35 AM
James Filby
James Filby - avatar