What does auto do in margin: 0 auto;?
I can’t seem to understand what auto does. I know it sometimes has the effect of centring objects.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you have specified a
widthon the object that you have appliedmargin: 0 autoto, the object will sit centrally within its parent container.Specifying
autoas the second parameter basically tells the browser to automatically determine the left and right margins itself, which it does by setting them equally. It guarantees that the left and right margins will be set to the same size. The first parameter 0 indicates that the top and bottom margins will both be set to 0.Therefore, to give you an example, if the parent is 100px and the child is 50px, then the
autoproperty will determine that there’s 50px of free space to share betweenmargin-leftandmargin-right:Which would give:
Have a look at this jsFiddle. You do not have to specify the parent width, only the width of the child object.