Dynamically change the height of a div element based on content

I am working on a Facebook-like toolbar for my website.

There is a part of the toolbar where the user can click to see which of his favorite members are online.

I am trying to figure out how to get the div element that appears to grow based on the content that places the AJAX call.

For example, when a user clicks "Favorites Online (4)", I show a pop-up div element with a fixed height and "Loading ...". When the content is loading, I would like to increase the height of the div element based on what content was returned.

I can do this by calculating the height of each element * the number of elements, but not very elegant at all.

Is there a way to do this using JavaScript or CSS? (note: using jQuery as well).

Thanks.

JavaScript:

    function favoritesOnlineClick()
{
    $('#favoritesOnlinePopUp').toggle();
    $('#onlineStatusPopUp').hide(); 
    if ($('#favoritesOnlinePopUp').css('display') == 'block') { loadFavoritesOnlineListing(); } 
}

CSS and HTML:

    #toolbar
{
    background:url('/_assets/img/toolbar.gif') repeat-x;
    height:25px;
    position:fixed;
    bottom:0px;
    width:100%;
    left:0px;
    z-index:100;
    font-size:0.8em;
}
#toolbar #popUpTitleBar
{
    background:#606060;
    height:18px;
    border-bottom:1px solid #000000;
}
#toolbar #popUpTitle
{
    float:left;
    padding-left:4px;
}
#toolbar #popUpAction
{
    float:right;
    padding-right:4px;
}
#toolbar #popUpAction a
{
    color:#f0f0f0;
    font-weight:bold;
    text-decoration:none;
}
#toolbar #popUpLoading
{
    padding-top:6px;
}
#toolbar #favoritesOnline
{
    float:left;
    height:21px;
    width:160px;
    padding-top:4px;
    border-right:1px solid #606060;
    text-align:center;
}
#toolbar #favoritesOnline .favoritesOnlineIcon
{
    padding-right:5px;
}
#toolbar #favoritesOnlinePopUp
{
    display:block;
    border:1px solid #000000;
    width:191px;
    background:#2b2b2b;
    float:left;
    position:absolute;
    left:-1px;
    top:-501px; /*auto;*/
    height:500px;/*auto;*/
    overflow:auto;
}
#toolbar #favoritesOnlineListing
{
    font-size:12px;
}
<div id="toolbar">
    <div id="favoritesOnline" style=" <?php if ($onlinestatus == -1) { echo "display:none;"; } ?> ">
       <img class="favoritesOnlineIcon" src="/_assets/img/icons/favorite-small.gif" /><a href="javascript:favoritesOnlineClick();">Favorites Online (<span id="favoritesOnlineCount"><?php echo $favonlinecount; ?></span>)</a>
        <div id="favoritesOnlinePopUp">
            <div id="popUpTitleBar">
                <div id="popUpTitle">Favorites Online</div>
                <div id="popUpAction"><a href="javascript:closeFavoritesOnline();">x</a></div>
            </div>     
            <div id="favoritesOnlineListing">
             <!-- Favorites online content goes here -->
            </div>
        </div>
    </div>
</div>
+3
source share
3 answers

Perhaps you can remove the height property (make sure it is not set in CSS) and let the DIV expand in height by itself.

+4
source

Make this a float and do not use a cleanup element after it.

+2
source

CSS: 25px , . , , :

:

 #toolbar #popUpAction  { /*some css */ }

 #toolbar #popUpAction a  { /*some css */ }

:

 #popUpAction  { /*some css */ }

 #popUpAction a  { /*some css */ }
+1

Source: https://habr.com/ru/post/1704270/


All Articles