Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically align div inside another div without display:table-cell

Ok this is the div structure.

  <div class="DivParent"> 
  <a href="#">

  <div class="DivWhichNeedToBeVerticallyAligned"></div>

  </a>    
  </div>

DivParent has fixed width and height values but DivWhichNeedToBeVerticallyAligned does not have fixed height values.

If you make DivParent display:table-cell; you can vertically align DivWhichNeedToBeVerticallyAligned but i don't want to use that feature since it causes some mess.

A href tag link should be same size with the divParent i mean whole divparent has to be clickable. like display:block.

So are there any CSS way of vertically aligning or lightweight jquery solution would also help.

Thank you.

Here jsfiddle : http://jsfiddle.net/XHK2Z/

*

like image 772
MonsterMMORPG Avatar asked Sep 16 '11 14:09

MonsterMMORPG


People also ask

How do I vertically align 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 I vertically align a div content?

To center a div vertically on a page, you can use the CSS position property, top property, and transform property. Start by setting the position of the div to absolute so that it's taken out of the normal document flow. Then set the top property to 50%.

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.


1 Answers

You can use an extra helper to achieve vertical alignment in a block with fixed height.

Look at this: http://jsfiddle.net/kizu/7Fewx/

There you must have a helper near a block you want to align with:

.DivHelper {
    display: inline-block;
    vertical-align: middle;
    height:100%;
}

And add display: inline-block; vertical-align: middle; to .DivWhichNeedToBeVerticallyAligned

like image 111
kizu Avatar answered Oct 13 '22 20:10

kizu