Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically align <div> contained in <td>

Tags:

html

css

I have something like this:

<tr>
  <td>
    <div class="blue">...</div>
    <div class="yellow">...</div>
  </td>
  <td>
    <div class="blue">...</div>
    <div class="yellow">...</div>
  </td>
</tr>

Here's a example of my current HTML: http://jsfiddle.net/DcRmu/2/

Inside a <tr>, all <td>s have the same height. I want the yellow <div>s inside those <td>s to align vertically along the bottom of <td>; and the blue <div>s to align vertically along the top of <td>. I tried to set vertical-align to bottom and it didn't work.

Thanks!

like image 618
garthcn Avatar asked Sep 26 '11 05:09

garthcn


People also ask

How do I vertically align a div?

For this to work, you need to have a parent container with the display: table; property, and inside that container you will have the number of columns you want to have centered, with the display: table-cell; (and vertical-align: middle; ) property.

How do I vertically center a div inside another div?

Vertically centering div items inside another div Just set the container to display:table and then the inner items to display:table-cell . Set a height on the container, and then set vertical-align:middle on the inner items.

How do you vertically align an element in CSS?

To get them centered along a line, you'd use vertical-align: middle; . Although note that it centers the text according to its tallest ascender and deepest descender. Each element lines up according to the line you've set, which doesn't change from element to element.


1 Answers

vertical-align:bottom; should work

Example: http://jsfiddle.net/jasongennaro/DcRmu/

EDIT

As per the new fiddle

You just need to place vertical-align:bottom; on the td not on the div

I updated your fiddle: http://jsfiddle.net/jasongennaro/DcRmu/7/

EDIT 2

I reread the question again and I saw the change

I want the yellow <div>s inside those <td>s to align vertically along the bottom of <td>; and the blue <div>s to align vertically along the top of <td>

To do this, you need to

  1. set the vertical-align to top on the td
  2. float the divs
  3. give the bottom div a margin equal to the height of the cell minus the sum of the div heights. In this case, 200px - (50px + 50px) = 100px.

New CSS

tr td{
    width:200px;
    height:200px;
    background:red;
    vertical-align:top;
}

div.blue{
    width:50px;
    height:50px;
    background:blue;
    float:left;
}
div.yellow{
    width:50px;
    height:50px;
    background:yellow;
    float:left;
    clear:both;
    margin-top:100px;
}

Working example: http://jsfiddle.net/jasongennaro/DcRmu/9/

like image 93
Jason Gennaro Avatar answered Oct 05 '22 13:10

Jason Gennaro