Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically Center Div Content [duplicate]

Tags:

html

css

Possible Duplicate:
center div vertically in a % height div?

Hey all, i have a page with some content in it. I need to center the whole thing so that in large resolutions, the content is always in the middle of the page. Two things. first is, if the resolution is small, i want at the very least the content to stop when it gets to the top of the page. I tried doing top:50% height: 680 margin-top:-340px. The problem is that in a smaller resoultion the content goes flying off the page. How do i do this? The nav is in an absolute div at the top, this too has to be centered vertically. Thanks for your help all!

er

like image 300
Damien Avatar asked Apr 21 '11 15:04

Damien


People also ask

How do I align the contents of a div center vertically?

You can do this by setting the display property to “flex.” Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I center a div vertically in a 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 center a parent in a div?

right are positioned absolutely with top: 50% and use margin-top: -0.8em to get vertical centering (use one-half of line-height value). Use the left and right offsets (or padding) to adjust the child elements horizontal position as needed.

How do you center align a div inside another div using transform?

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. Now here comes the role of this property in adjusting the inner div.


2 Answers

It's possible to vertically center a <div> within another <div> using only css and without explicitly specifying a height or a negative top margin. The one major drawback of the following technique is that { display: table; } is NOT supported in Internet Explorer < 9.

If this is your markup:

<div class="wrapper">
    <div class="content">...</div>
</div>

The CSS to get the inner <div> to be centered is:

.wrapper { display: table; }
.wrapper .content { display: table-cell; vertical-align: middle; }

This works by essentially emulating the naitive layout behavior of <table> elements.

like image 166
Lev Avatar answered Sep 27 '22 20:09

Lev


try something like this:

<figure class='logo'>
<span></span>
<img class='photo'/>

and css:

.logo {
display: block
text-align: center;
vertical-align: middle;
border: 4px solid #dddddd;
padding: 4px;
height: 74px;
width: 74px; }

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

.logo .photo {
height: auto;
width: auto;
max-width: 100%;
max-height: 100%; }  
like image 23
Obi-wan Avatar answered Sep 27 '22 18:09

Obi-wan