How can I horizontally center a <div> within another <div> using CSS?
<div id="outer"> <div id="inner">Foo foo</div> </div>
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.
With
flexboxit is very easy to style the div horizontally and vertically centered.To align the div vertically centered, use the property
align-items: center.Other Solutions
You can apply this CSS to the inner
<div>:Of course, you don’t have to set the
widthto50%. Any width less than the containing<div>will work. Themargin: 0 autois what does the actual centering.If you are targeting Internet Explorer 8 (and later), it might be better to have this instead:
It will make the inner element center horizontally and it works without setting a specific
width.Working example here: