[英]How can I change the default width of a Twitter Bootstrap modal box?
我尝试了以下方法:
<div class="modal hide fade modal-admin" id="testModal" style="display: none;">
<div class="modal-header">
<a data-dismiss="modal" class="close">×</a>
<h3 id='dialog-heading'></h3>
</div>
<div class="modal-body">
<div id="dialog-data"></div>
</div>
<div class="modal-footer">
<a data-dismiss="modal" class="btn" >Close</a>
<a class="btn btn-primary" id="btnSaveChanges">Save changes</a>
</div>
</div>
这个Javascript:
$('.modal-admin').css('width', '750px');
$('.modal-admin').css('margin', '100px auto 100px auto');
$('.modal-admin').modal('show')
结果不是我的预期。 模态左上方位于屏幕中央。
谁能帮我。 有没有人试过这个。 我认为这不是一件不寻常的事情。
更新:
在bootstrap 3
您需要更改模态对话框。 因此,在这种情况下,您可以在modal-dialog
所在的位置添加类modal-admin
。
原始答案(Bootstrap <3)
是否有某种原因你试图用JS / jQuery改变它?
您可以使用CSS轻松完成,这意味着您无需在文档中进行样式设置。 在您自己的自定义CSS文件中,添加:
body .modal {
/* new custom width */
width: 560px;
/* must be half of the width, minus scrollbar on the left (30px) */
margin-left: -280px;
}
在你的情况下:
body .modal-admin {
/* new custom width */
width: 750px;
/* must be half of the width, minus scrollbar on the left (30px) */
margin-left: -375px;
}
我将body放在选择器之前的原因是它比默认值具有更高的优先级。 这样您就可以将其添加到自定义CSS文件中,而无需担心更新Bootstrap。
如果你想让它只用CSS响应,请使用:
.modal.large {
width: 80%; /* respsonsive width */
margin-left:-40%; /* width/2) */
}
注1:我使用.large
类,你也可以在普通的.modal
上执行此.modal
注2:在Bootstrap 3中,可能不再需要负边距 (未经个人确认)
/*Bootstrap 3*/
.modal.large {
width: 80%;
}
注3:在Bootstrap 3和4中,有一个modal-lg
类。 所以这可能就足够了,但是如果你想让它响应,你仍然需要我为Bootstrap 3提供的修复。
在某些应用程序中使用fixed
模态,在这种情况下,您可以尝试width:80%; left:10%;
width:80%; left:10%;
(公式:左= 100 - 宽度/ 2)
如果您正在使用Bootstrap 3,则需要更改模态对话框div,而不是像接受的答案中所示的模态div。 此外,为了保持Bootstrap 3的响应特性,使用媒体查询编写覆盖CSS非常重要,因此模式在较小的设备上将是全宽的。
看到这个JSFiddle来试验不同的宽度。
HTML
<div class="modal fade">
<div class="modal-dialog custom-class">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 class="modal-header-text">Text</h3>
</div>
<div class="modal-body">
This is some text.
</div>
<div class="modal-footer">
This is some text.
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
CSS
@media screen and (min-width: 768px) {
.custom-class {
width: 70%; /* either % (e.g. 60%) or px (400px) */
}
}
如果你想要一些不破坏相对设计的东西,试试这个:
body .modal {
width: 90%; /* desired relative width */
left: 5%; /* (100%-width)/2 */
/* place center */
margin-left:auto;
margin-right:auto;
}
正如@Marco Johannesen所说,选择器之前的“主体”使得它比默认值具有更高的优先级。
在Bootstrap 3+中,更改模式对话框大小的最合适方法是使用size属性。 下面是一个例子,注意modal-sm
modal-dialog
类中的modal-sm
,表示一个小模态。 它可以包含小的值sm
,中的md
和大的lg
。
<div class="modal fade" id="ww_vergeten" tabindex="-1" role="dialog" aria-labelledby="modal_title" aria-hidden="true">
<div class="modal-dialog modal-sm"> <!-- property to determine size -->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="modal_title">Some modal</h4>
</div>
<div class="modal-body">
<!-- modal content -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="some_button" data-loading-text="Loading...">Send</button>
</div>
</div>
</div>
</div>
对于Bootstrap 3
这里是如何做到的。
在HTML标记中添加modal-wide
样式(根据Bootstrap 3文档中的示例进行调整 )
<div class="modal fade modal-wide" id="myModal" 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">×</button>
<h4 id="myModalLabel" class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
并添加以下CSS
.modal-wide .modal-dialog {
width: 80%; /* or whatever you wish */
}
没有必要在Bootstrap 3
覆盖margin-left
以使其立即居中。
在Bootstrap 3中,您只需要这个
<style>
.modal .modal-dialog { width: 80%; }
</style>
以上大多数答案对我没有用!!!
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> <style> #myModal1 .modal-dialog { width: 80%; } #myModal2 .modal-dialog { width: 50%; } </style> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal1"> 80% </button> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal2"> 50% </button> <center> <!-- Modal --> <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span> </button> <h4 class="modal-title" id="myModalLabel">Modal title</h4> </div> <div class="modal-body"> custom width : 80% </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> <!-- Modal --> <div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span> </button> <h4 class="modal-title" id="myModalLabel">Modal title</h4> </div> <div class="modal-body"> custom width : 50% </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> </center>
解决方案来自此页面
$('#feedback-modal').modal({
backdrop: true,
keyboard: true
}).css({
width: 'auto',
'margin-left': function () {
return -($(this).width() / 2);
}
});
在Bootstrap的第3版中,有一种简单的方法可以使模态更大。 只需在modal-dialog旁边添加类modal-lg即可。
<!-- My Modal Popup -->
<div id="MyWidePopup" class="modal fade" role="dialog">
<div class="modal-dialog modal-lg"> <---------------------RIGHT HERE!!
<!-- Modal content-->
<div class="modal-content">
Bootstrap 3:为了维护小型和小型设备的响应功能,我做了以下工作:
@media (min-width: 768px) {
.modal-dialog-wide
{ width: 750px;/* your width */ }
}
这就是我所做的,在我的custom.css中我添加了这些行,就是这样。
.modal-lg {
width: 600px!important;
margin: 0 auto;
}
显然,您可以更改宽度的大小。
如果Bootstrap> 3,只需为您的模态添加样式。
<div class="modal-dialog" style="width:80%;">
<div class="modal-content">
</div>
</div>
我发现这个解决方案对我来说效果更好。 你可以用这个:
$('.modal').css({
'width': function () {
return ($(document).width() * .9) + 'px';
},
'margin-left': function () {
return -($(this).width() / 2);
}
});
或者这取决于您的要求:
$('.modal').css({
width: 'auto',
'margin-left': function () {
return -($(this).width() / 2);
}
});
请参阅我发现的帖子: https : //github.com/twitter/bootstrap/issues/675
FYI Bootstrap 3自动处理左侧属性。 因此,只需添加此CSS将更改宽度并使其保持居中:
.modal .modal-dialog { width: 800px; }
保持响应式设计,将宽度设置为您想要的宽度。
.modal-content {
margin-left: auto;
margin-right: auto;
max-width: 360px;
}
把事情简单化。
改变类model-dialog
可以达到预期的效果。 这些小技巧对我有用。 希望它能帮助您解决这个问题。
.modal-dialog {
width: 70%;
}
我使用CSS和jQuery的组合,以及此页面上的提示,使用Bootstrap 3创建流体宽度和高度。
首先,一些CSS用于处理内容区域的宽度和可选滚动条
.modal.modal-wide .modal-dialog {
width: 90%;
}
.modal-wide .modal-body {
overflow-y: auto;
}
然后一些jQuery根据需要调整内容区域的高度
$(".modal-wide").on("show.bs.modal", function() {
var height = $(window).height() - 200;
$(this).find(".modal-body").css("max-height", height);
});
完整的文章和代码http://scottpdawson.com/development/creating-a-variable-width-modal-dialog-using-bootstrap-3/
使用Bootstrap 3,所需要的只是通过CSS给模态对话框的百分比值
#alertModal .modal-dialog{
width: 20%;
}
在带有CSS的Bootstrap 3中 ,您可以使用:
body .modal-dialog {
/* percentage of page width */
width: 40%;
}
这可以确保您不会破坏页面的设计,因为我们使用的是.modal-dialog
而不是.modal
,它甚至会应用于着色。
尝试类似下面的内容(请参阅此处的实时jsfiddle示例: http : //jsfiddle.net/periklis/hEThw/1/ )
<a class="btn" onclick = "$('#myModal').modal('show');$('#myModal').css('width', '100px').css('margin-left','auto').css('margin-right','auto');" ref="#myModal" >Launch Modal</a>
<div class="modal" id="myModal" style = "display:none">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<a href="#" class="btn">Close</a>
<a href="#" class="btn btn-primary">Save changes</a>
</div>
</div>
我发现使用已经内置到引导程序中的列和其他响应元素可以获得更多控制,而不是使用百分比来使模式响应。
要使模态响应/任意数量的列的大小:
1)使用.container类在模态对话框div周围添加一个额外的div -
<div class="container">
<div class="modal-dialog">
</div>
</div>
2)添加一点CSS以使模态全宽 -
.modal-dialog {
width: 100% }
3)如果你有其他模态,或者加一个额外的课程 -
<div class="container">
<div class="modal-dialog modal-responsive">
</div>
</div>
.modal-responsive.modal-dialog {
width: 100% }
4)如果你想要各种大小的模态,添加一行/列 -
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="modal-dialog modal-responsive">
...
</div>
</div>
</div>
</div>
在您的css文件中添加以下内容
.popover{
width:auto !important;
max-width:445px !important;
min-width:200px !important;
}
使用下面的脚本:
.modal {
--widthOfModal: 98%;
width: var(--widthOfModal) !important;
margin-left: calc(calc(var(--widthOfModal) / 2) * (-1)) !important;
height: 92%;
overflow-y: scroll;
}
演示 :
使用,达到了预期的结果,
.modal-dialog {
width: 41% !important;
}
我为Bootstrap 4.1解决了这个问题
混合以前发布的解决方案
.modal-lg { max-width: 90% !important; /* desired relative width */ margin-left:auto !important; margin-right:auto !important; }
我用这种方式,这对我来说是完美的
$("#my-modal")
.modal("toggle")
.css({'width': '80%', 'margin-left':'auto', 'margin-right':'auto', 'left':'10%'});
Bootstrap 2的基于较少的解决方案(无js):
.modal-width(@modalWidth) {
width: @modalWidth;
margin-left: -@modalWidth/2;
@media (max-width: @modalWidth) {
position: fixed;
top: 20px;
left: 20px;
right: 20px;
width: auto;
margin: 0;
&.fade { top: -100px; }
&.fade.in { top: 20px; }
}
}
然后,无论您想要指定模态宽度,都可以:
#myModal {
.modal-width(700px);
}
我使用SCSS和完全响应模式:
.modal-dialog.large {
@media (min-width: $screen-sm-min) { width:500px; }
@media (min-width: $screen-md-min) { width:700px; }
@media (min-width: $screen-lg-min) { width:850px; }
}
you can use any prefix or postfix name for modal. but you need to make sure that's should use everywhere with same prefix/postfix name.
body .modal-nk {
/* new custom width */
width: 560px;
/* must be half of the width, minus scrollbar on the left (30px) */
margin-left: -280px;
}
要么
body .nk-modal {
/* new custom width */
width: 560px;
/* must be half of the width, minus scrollbar on the left (30px) */
margin-left: -280px;
}
下面是通过javascript从屏幕设置模态弹出宽度和左侧位置的代码。
$('#openModalPopupId')。css({“width”:“80%”,“left”:“30%”});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.