简体   繁体   中英

Bootstrap Popup with Dynamic Width as per content

I have created bootstrap modal, and appending some html through AJAX...

I am not able to show perfect width as per content. I have tried:

width: 'auto'; //it looks as in Screenshot
width: 100%;   //it takes whole page width

It looks like:

图片1

 var PopupHelper = { Show: function () { $("#popup_content").empty(); var qAjax = new AjaxHelper("/Services/Service.asmx/GetCurrentPopupContent"); qAjax.OnSuccess = function (data) { $("#popup_content").html(data); $('#popup-Model').css({ 'width' : 'auto','overflow': 'auto' }); $("#popup-Model").modal(); } qAjax.Init(); } } 
 <!--Popup Window --> <div class="modal fade" id="popup-Model" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-top:-10px !important">&times;</button> </div> <div class="modal-body" id="popup_content"> </div> </div> <!-- /.modal-content --> </div> <!-- /.modal-dialog --> </div> <!--Popup Window --> 

How to make modal width, as per the content inside modal-body ?

NOTE: modal HTML shown in image can vary

Thanks!!!

add row and col-* class in your popup to handle content

<!--Popup Window -->
<div class="modal fade" id="popup-Model" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-top:-10px !important">&times;</button>
      </div>
      <div class="modal-body">
        <div class="col-xs-12" id="popup_content"></div>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
<!--Popup Window -->

Since you're already using javascript to render this modal, it will be easier to just set the modal-dialog width to the same width as your content. It's hard to do this only with CSS because modal-dialog have fixed width (varying per screen size).

$("#popup_content").html(data);
$("#popup-Model").modal();
setTimeout(() => {     
  //change div:first-child to whatever your first child is 
  var width = $("#popup_content div:first-child").width() + 30; //Padding
  $(".modal .modal-dialog").css({ 'width' : width+'px' });
}, 0); //Maybe you'll need a longer timeout because of css transitions

working example

Worked in OP case as:

$("#popup_content").empty();
var qAjax = new AjaxHelper("/Services/Service.asmx/GetCurrentPopupContent");
qAjax.OnSuccess = function (data) {
$("#popup_content").html('<div class="test-div">' + data + '</div>');
$("#popup-Model").modal();
setTimeout(function () {
    //change div:first-child to whatever your first child is 
    var width = $("#popup_content div:first-child").width() + 50; //padding
    $(".modal-dialog").css({ 'width': width + 'px' });
    $('#popup-Model').css({ 'overflow': 'auto' });
}, 500);

try to use min-width and position:relative style to "model-body" class. I hope it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM