Jason
Jason

Reputation: 13986

Center Div Tags in another Div Tag

I'm trying to get a parent div tag to hold n children div tags such that they are all on the same line, yet grouped together in the center. For example:

enter image description here

Here the children are blue, and the parent is red.

Here are the things I've tried:

Upvotes: 1

Views: 470

Answers (4)

Stefan
Stefan

Reputation: 1106

For IE6 and IE7 compatibility you might have to add zoom:1; and *display:inline; to your child CSS

jsFiddle

.parent {width:100%;border:1px solid red;text-align:center;}
.child {width:15%;display:inline-block;border:1px solid blue;}

Upvotes: 4

Paul Roub
Paul Roub

Reputation: 36458

<style>
.container {
  width: 100%;
  padding: 0;
  text-align: center;
  border: 1px solid red;
}

.inner {
  display: inline-block;
  margin: 0 5px;
  border: 1px solid blue;
}
</style>

<div class="container">
  <div class="inner">
    one
  </div>
  <div class="inner">
    two
  </div>
  <div class="inner">
    three
  </div>
</div>

Upvotes: 2

dfsq
dfsq

Reputation: 193291

Try to use display: inline-block;:

.child {
    display: inline-block;
    ...
}

http://jsfiddle.net/mupuR/

Upvotes: 0

Elliot Bonneville
Elliot Bonneville

Reputation: 53341

Stick the blue divs in a container div. Find their widths (margin and padding included) and give the container div that width. Then set the container div's margin to 0 auto, stick it in the red div and you should be fine.

Upvotes: 0

Related Questions