Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to center an html element within its containing element?

Tags:

html

css

center

Let's say you have the following chunk of code:

<div id="container">
  <someelement>This is any element.</someelement>
</div>

What's the best CSS I could use to horizontally center "someelement" within its containing div?

like image 977
stalepretzel Avatar asked Dec 27 '08 22:12

stalepretzel


1 Answers

JaredPar has the right idea, but here's a cleaner way to do what you're looking for ;)

#container {
    text-align: center;
}

#container someelement {
    margin: 0 auto;
    text-align: left;
}
like image 154
ojrac Avatar answered Sep 28 '22 23:09

ojrac