Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center two divs in the middle of a wrapper

Tags:

css

I have this CSS problem:

<div id="wrap">
    <div id="left">I am div 1</div>
    <div id="right">I am div 2</div>
</div>

I am trying to make "I am div 1 I am div 2" center inside the wrap div. Not sure how to explain it. The design is only in percent, liquid design and I'm stuck.

Any ideas?

like image 922
webmasters Avatar asked Apr 29 '11 15:04

webmasters


People also ask

How do you center a div wrapper?

Use text-align: center for the container, display:inline-block(-moz-inline-box for Firefox 2) for the wrapper div, and display:inline for the content div to horizontally center content that has an undefined width across browsers.

How do I center two divs side by side?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

How do I align two divs vertically centered?

To align two <div> elements vertically in Bootstrap 3, you can try using the CSS Flexible Box Layout. In the example below, we display the needed row as a flex container box with the CSS display property and then, align the flex-items (columns) vertically with the align-items property.

How do I center a nested div?

To move the inner div container to the centre of the parent div we have to use the margin property of style attribute. We can adjust the space around any HTML element by this margin property just by providing desired values to it.


2 Answers

If I understand your question correctly, this should work:

#wrap {
    background: #e7e7e7;
    padding: 5%;
    text-align: center;
    width: 80%;  
}

#left, #right {
     background: #ccc;
     display: inline-block;    
     padding: 2%;   
}

View Example

This will center two div blocks within the wrap, side by side.


EDIT: 2015 Flexbox Solution

Flexbox is much more widely supported now and is most likely a better solution for this situation. There are some quirks that come along with the above inline-block method, such as horizontal spacing and vertical alignment issues. Here is the flexbox solution:

#wrap {
    background: #e7e7e7;
    display: flex;
    justify-content: center;
    padding: 5%; 
    width: 80%;  
}

#left, #right {
    background: #ccc;
    padding: 2%;   
}

View Example

Be sure to check Can I Use to confirm that flexbox is supported in the browsers you are supporting.

2019 Edit: Commenter MC9000 brought up that my example is not responsive in percents, as the original question mentioned. I've updated my examples to show that this works with percentage based sizing just like it does with pixel based sizing.

like image 70
jackrugile Avatar answered Oct 21 '22 16:10

jackrugile


text-align:center; would center this for you:

#wrap{
  width:50%;
  border: 1px solid #000;
}

#left, #right{
  text-align: center;
}

Demo: http://jsfiddle.net/DbNs6/1/

like image 37
kjl Avatar answered Oct 21 '22 16:10

kjl