Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left, center, and right align divs on bottom of same line

I have three divs that I would like to display on the same line. Each of the three has different widths and heights, and they are not straight text. I'd like to left-align one (all the way to the left), right-align another (all the way to the right), and center the third (in the middle of the containing div, the whole page in this case).

In addition, I'd like the three divs to be vertically aligned to the bottom of the containing div. The solution I have vertically aligns them to the top of the containing div.

What is the best way to handle this?

like image 955
jrdioko Avatar asked Feb 16 '11 00:02

jrdioko


People also ask

How do I align two divs on the same line?

To display multiple div tags in the same line, we can use the float property in CSS styles. The float property takes left, right,none(default value) and inherit as values. The value left indicates the div tag will be made to float on the left and right to float the div tag to the right.

How do you align right and left in the same line in CSS?

The style attribute can be used to align html text. Style attributes specify inline styling for an element. The HTML *p style attribute is used with the CSS property text-align for the center, left, and right alignments.

How do I align 3 divs horizontally?

Three or more different div can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side.

How do you center align 3 DIVS in one line?

Align 3 divs in one line left-center-right side by side in horizontal without having to define explicit sizes that is without using width. Left should be aligned most to the left edge, right to the right edge and center to the center(middle of left and right div as shown below.

How to align text to left and right in Word document?

In the Word file that you want to insert the text and align to left and right, and then, click Home, in the Paragraph group, click the Paragraph Settings icon, see screenshot: 2. In the Paragraph dialog box, select Left from the Alignment drop down, and then, click Tabs button, see screenshot: 3.

How to align div elements on the same line when resized?

- To make a responsive design, so the DIV elements to remain aligned on the same line when the browser's window is resized, use percentage values for the sizes of the DIVs, and margins. In the following example, the <div> elements have inline-block property.

How to center an element vertically in CSS?

Then we can add the clearfix hack to the containing element to fix this problem: There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding: I am vertically centered. To center both vertically and horizontally, use padding and text-align: center: I am vertically and horizontally centered.


2 Answers

By setting your container div to position:relative and the child divs to position:absolute you can absolute position the divs within the confines of the container.

This makes it easy, as you can use bottom:0px to align all vertically to the bottom of the container, and then use left/right styling to position along the horizontal axis.

I set up a working jsFiddle: http://jsfiddle.net/Damien_at_SF/KM7sQ/5/ and the code follows:

HTML:

<div id="container">
    <div id="left">left</div>
    <div id="center">center</div>
    <div id="right">right</div>    
</div>

CSS:

#container {
    position:relative;
    height:400px;
    width:100%;
    border:thick solid black;
}
#container div {
    background:grey;
    width:200px;
}
#left {
    position:absolute;
    left:0px;
    bottom:0px;
}
#center {
    position:absolute;
    left:50%;
    margin-left:-100px;
    bottom:0px;
}
#right {
    position:absolute;
    right:0px;
    bottom:0px;
}

Note: For the "center" div, the margin-left = 1/2 the width of the div :)

Hope that helps :)

like image 180
Damien-Wright Avatar answered Oct 26 '22 02:10

Damien-Wright


My technique is similar to @Damien-at-SF:

I tried to rigorously demonstrate all the requirements you asked for.

Live Demo

HTML:

<div id="container">

    <div id="left"></div>
    <div id="mid"></div>
    <div id="right"></div>

</div>

CSS:

#container {
    position: relative;
    height: 400px;
    width: 80%;
    min-width: 400px;
    margin: 0 auto;

    background: #ccc
}
#left, #right, #mid {
    position: absolute;
    bottom: 0;
}
#left {
    left: 0;
    width: 80px;
    height: 200px;

    background: red
}
#right {
    right: 0;
    width: 120px;
    height: 170px;

    background: blue
}

#mid {
    left:50%;

    margin-left: -80px;
    width: 160px;
    height: 300px;

    background: #f39
}
like image 34
thirtydot Avatar answered Oct 26 '22 00:10

thirtydot