Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I vertically align two floated divs?

Tags:

html

css

I have two divs inside a container div. One need to float left the other float right. They also both need to be vertically centered inside their parent. How can I achieve this?

<div id='parent'>
    <div id='left-box' class='child'>Some text</div>
    <div id='right-box' class='child'>Details</div>    
</div>

If no float is applied to either they vertically align to the middle with this css

.child{ display:inline-block; vertical-align:middle; }

However adding #right-box{ float: right; } causes the children to lose their vertical alignment. What am I doing wrong?

Thanks guys

like image 962
JackMahoney Avatar asked May 12 '12 04:05

JackMahoney


People also ask

How do I vertically align two divs?

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 arrange DIVS vertically?

Use the position property with the “relative” value for the parent element to place it relative to its normal position. Use the position property with the “absolute” value for the child element to place it relative to its positioned parent element. Add the height, margin-top, top, border, and width properties.

How do you vertically align an object in CSS?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.

What is vertical-align inherit?

The vertical-align property in CSS is used to specify the vertical alignment of the table-box or inline element. Syntax: vertical-align: baseline|length|sub|super|top|text-top|middle|bottom |text-bottom|initial|inherit; Note: This property is mostly used to align images to it's corresponding text.


2 Answers

here is the online demo of the solution you needed

enter image description here

it was made with this html:

<div id='parent'>
    <div id='left-box' class='child'>Some text</div>
    <div id='right-box' class='child'>Details</div>    
</div>

and this css:

#parent {
    position: relative;

    /* decoration */
    width: 500px;
    height: 200px;
    background-color: #ddd;
}

.child {
    position: absolute;
    top: 50%;
    height: 70px;
    /* if text is one-line, line-height equal to height set text to the middle */
    line-height: 70px;
    /* margin-top is negative 1/2 of height */
    margin-top: -35px;

    /* decoration */
    width: 200px;
    text-align: center;
    background-color: #dfd;
}​

#left-box { left: 0; }
#right-box { right: 0; }
like image 99
Vladimir Starkov Avatar answered Oct 23 '22 03:10

Vladimir Starkov


You can try the display:table and display:table-cell styles.
Check this site out for more details http://www.quirksmode.org/css/display.html


NB: if you want the parent div height to be a percent (like 100%), then it will be relative to the height of it's container. If the container is the body, then you will have to set the body and html's height as well, like to 100%.

Here's an example of what the code might look like:

<div id='parent'>
    <div id='left-box'>Some text</div>
    <div id='right-box'>Details</div>    
</div>​

<style>
body,html{
    height:100%;   
}
#parent{
    border:1px solid red;
    display:table;
    height:100%; 
    width:100%;        
}
#left-box{ 
    background-color:#eee;
    display: table-cell;
    vertical-align:middle;
    padding:3px;
    width:50%;
}
#right-box{ 
    background-color:#dddddd;
    display: table-cell;
    vertical-align:middle;
    padding:3px;
    width:50%;
}
​</style>
like image 20
D.B. Avatar answered Oct 23 '22 04:10

D.B.