Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to horizontally center multiple div's in a parent div

I am trying to create a control which contains multiple div's and all the div's are horizontally center align. as shown below.

enter image description here

I was able to able to float every div using float:left css property. which give something like this

enter image description here

but ever div's are aligh left to their parent.

I have one ulternative solition to use javascript to position every div but I want to use css. Is it possible to achive this using css.

Thanks in advance

like image 379
Faheem Alam Avatar asked Jan 06 '15 15:01

Faheem Alam


People also ask

How do I align multiple items horizontally in CSS?

To align div horizontally, one solution is to use css float property. But a better solution is to to use CSS display:inline-block on all divs which needs to be aligned horizontally and place them in some container div.


1 Answers

This may do what you are trying to:

HTML:

<div class="parent">
    <div class="child">Element 1</div>
    <div class="child">Element 2</div>
</div>

CSS:

.child {
    display: inline-block;
}

.parent {
    text-align: center;
}

and the fiddle.

like image 62
lante Avatar answered Oct 18 '22 03:10

lante