Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning a div in the centre but with dynamic height?

Tags:

html

css

I want to position a div in the middle of the page. The solution I found on the internet assumes that the div will be of static size. I need the div to be in the middle, if the content is the right size, but if it is over the size of the div, it should become bigger, and eventually allow scrolling without changing the width.

PS: I don't need support for IE, just XULRunner (Firefox) and Webkit based browsers.

Edit: The whole page must be scrollable, not just the content div. And I need to preserve all the line breaks.

How I want to do italt text

like image 200
Kristina Brooks Avatar asked Dec 22 '10 15:12

Kristina Brooks


People also ask

Can you float a div in the center?

CSS div float centerYou can set float div center by using margin property. you does not need to use float property but you can use margin property and set left or right margin value auto by this way.

How do you make div width and height auto adjust to content?

Syntax: height: length|percentage|auto|initial|inherit; Property Values: height: auto; It is used to set height property to its default value.


1 Answers

Here you go:

<style>
    .container{
        border: 1px solid Red; 
        width: 300px; 
        height: 500px;
        display:table-cell;
        vertical-align:middle;
    }
    .content{
        border: 1px solid Blue; 
        width: 100px; 
        height:auto;
        min-height: 100px;
        max-height:200px;
        margin-left:auto;
        margin-right:auto;
        overflow:auto;
    }
</style>
<div class="container">
    <div class="content">
        add content here
    </div>
</div>

How it looks like:

alt text

Test it.

like image 110
Alin Purcaru Avatar answered Oct 17 '22 11:10

Alin Purcaru