3

Look at this fiddle

HTML:

<div id="outer">
    <div id="inner">
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
            <li>9</li>
            <li>10</li>
        </ul>
    </div>
</div>
<button onClick="toggleOuterHeight();">toggle outer div height</button>

JS:

toggleOuterHeight = function() {
    if ($('#outer').css('height') == '400px') {
        $('#outer').css('height','200px');
    } else {
        $('#outer').css('height','400px');
    }
}

CSS:

#outer {
    background-color: #1abc9c;
    height: 400px;
    position: relative;
    overflow: auto;
}

#inner {
    background-color: #16a085;
    height: 250px;
    width: 250px;
    position: absolute;
    margin: auto;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

#inner ul {
    margin: 0;
}

#inner ul li {
    line-height: 25px;
}

I have a div (inner) vertically aligned inside another div (outer)... It works like a charm!

If the outer div is smaller than the inner div scrolling must occur. You can see this clicking on button on the bottom of the example.

There is the problem: scrolling hides the top of the inner div. You cannot scroll to show the first lines!!!

I changed the css to center the div using translate, no luck!

Any tips?

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
zedmartins
  • 115
  • 1
  • 10

3 Answers3

1

You could avoid using absolute positioning for the vertical alignment which removes the element from normal flow, and use this approach1 instead:

EXAMPLE HERE

#outer { text-align: center; }

#outer:after {
    content: "";
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}

#inner {
    background-color: #16a085;
    height: 250px;
    width: 250px;
    display: inline-block;
    vertical-align: middle;
}

1. You could refer to "Vertical Alignment" section of that answer in order to achieve both horizontal and vertical alignment of a box.

Community
  • 1
  • 1
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
0

Try this:

CSS

#inner {
    background-color: #16a085;
    position: relative;
    height: 250px;
    width: 250px;
    margin: auto;
    top: 75px;
    left: 0;
    right: 0;
    bottom: 0;
}

JS

toggleOuterHeight = function() {
    if ($('#outer').css('height') == '400px') {
        $('#outer').css('height','200px');
        $('#inner').css('top','0');
    } else {
        $('#outer').css('height','400px');
        $('#inner').css('top','75px');
    }
}

Link to fiddle

gkrls
  • 2,618
  • 2
  • 15
  • 29
0

You could add a class and as you know the height use negative margins:

http://jsfiddle.net/vea1cxh1/3/

JS:

toggleOuterHeight = function() {
    if ($('#outer').css('height') == '400px') {
        $('#outer').css('height','200px').addClass('scrolly');
    } else {
        $('#outer').css('height','400px').removeClass('scrolly');
    }
}

CSS:

#inner {
    background-color: #16a085;
    height: 250px;
    width: 250px;
    position: absolute;
    margin: auto;
    margin-top: -125px;
    top: 50%;
    left: 0;
    right: 0;
    /* bottom: 0; */
}

#inner ul {
    margin: 0;
}
.scrolly #inner {
    margin-top: 0;
    top: 0;
}
alexrogers
  • 1,514
  • 1
  • 16
  • 27