0

I have a div with width:100%; since I want to fill out the parent container.
I also want to have the contents of the div in its center. I can not find a horizontal-align:center attribute for the divs. How can I configure the div so that its contents are in the center?
I.e if the div is this big:

------------------------------------------------------------------------------------------------------------  
                    Enclosing tags here  
                    in the center  
------------------------------------------------------------------------------------------------------------        
Jim
  • 18,826
  • 34
  • 135
  • 254

8 Answers8

2

try this:

div {
    margin:0 auto;
}
George Brighton
  • 5,131
  • 9
  • 27
  • 36
Dr Robotnik
  • 352
  • 1
  • 4
  • 14
1

Try this for fixed width

div.class_name {
width: XXpx;
margin: 0 auto;
text-align: center;
}

Dynamic Height

<div class="container">
 <div class="inside">some content</div>
</div>


.container
{
 text-align: center;
}
.inside
{
 display:inline-block;
}

In HTML

<div style="text-align: center;"> </div>

That all you need for the CSS.

Sridhar R
  • 20,190
  • 6
  • 38
  • 35
  • I want to make sure that I fill out the parent container.Turns out I can not use `width:100%` – Jim Nov 11 '13 at 09:51
0

Read about margin: auto trick: http://bluerobot.com/web/css/center1.html

0

You could try setting:

margin: auto

I'm not sure if it helps in this case.

El Mac
  • 3,256
  • 7
  • 38
  • 58
0
div.class {
margin:0 auto;
width: 800px /*as desired*/ ;
}
Richa
  • 4,240
  • 5
  • 33
  • 50
0

You must assign width to div that you will want locate to center then right and left margin should get 0 value

{
 width: 100px;
 margin : 0 auto;
}
Hamid Bahmanabady
  • 665
  • 1
  • 8
  • 20
0

This might be what you looking for: padding:0 20%; using a % helps auto adjust the padding depending on how wide the parent div is. You can change 20% to your preferred number.

.div {
width:100%;
padding: 0 20%;
}
Simo Mafuxwana
  • 3,702
  • 6
  • 41
  • 59
0

You should try text-align:center; to make content of div in center.

Lusy
  • 1
  • 1