bootstrap模态框为什么不居中

  1. 使用bootstrap模态框的时候, 发现, 不能设置居中, 如果需要的话, 比较多的方法都是改源码CSS, 那么, 这样做全部的模态框都是居中;

  2. 为什么会出现这个现象, 是在交互上模态框不推荐使用, 还是有更好的方案?

  3. 感觉一个模态框出来填写表单,不换新页面挺好的, 所有产生了这个疑问, 难道还有更优雅的交互设计?

居中的方法已经有很多了, 这里探讨一下为什么没有设置剧中, 有了解行情的朋友吗?

阅读 12.3k
5 个回答

1.模态框不能居中,你指的是水平居中还是垂直居中,bootstrap的模态框是水平居中的,没有垂直居中。

2.为什么不是垂直居中?

模态框的高度不是固定的,是用里面的内容撑的。你能做到当模态框里面的内容比你的屏幕高度还多的时候垂直居中吗?bootstrap的模态框垂直方向是用 margin: 30px auto 固定的。

3.如何做到垂直居中?

改的只是css效果,覆盖样式就行了。给模态框定死一个高度,再用css写垂直居中的样式就行了。

直接用bootstrap提供的模板就是居中显示啊

<div class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</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 -->

1.不需要改源码,只需要覆盖样式就行了
2.居中不能自己写吗

小白不懂为什么这么多人依赖bootstrap.
可以用一个class来搞定,固定高度的元素垂直居中:

selector{
    height:100px;
    top:50%;
    margin-top:-50px;
    position: absolute;
}

不定高度的元素用js控制margin-top :-(height/2)就好了。
不管用什么框架,掌握原理是基本的。

bootstrap没有垂直居中,只是设计问题。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题