How do I resize a div automatically to the size of its contents when the contents of the div have changed?

后端 未结 3 713
庸人自扰
庸人自扰 2021-01-06 02:58

At the moment, I have this DIV with a registration form centered over the page. The contents of the DIV come from an ascx-page. This is done nicely. Now, if the user tries t

3条回答
  •  时光说笑
    2021-01-06 03:37

    Jquery:

    $(document).ready(function() {
    
    var originalFontSize = 12;
    
    var sectionWidth = $('#sidebar').width();
    
    
    
    $('#sidebar span').each(function(){
    
        var spanWidth = $(this).width();
    
        var newFontSize = (sectionWidth/spanWidth) * originalFontSize;
    
        $(this).css({"font-size" : newFontSize, "line-height" : newFontSize/1.2 + "px"});
    
    });
    
    });
    
    
    

    div:

    preview http://jbdes.net/dev/js/fontsize.html

提交回复
热议问题