Bootstrap模态框图片背景

5

我想问一下,在我的模态框中是否可以设置背景图片?我试过搜索,但我只看到与模态框有关的背景内容是指页面本身。我想要的是在模态框上放置一个带有图像背景的模态框。或者改变它的颜色?谢谢。我不太擅长CSS。谢谢。


也许这个链接可以帮助你:https://dev59.com/0XXYa4cB1Zd3GeqP-9Yy#39426586 - Elnaz
4个回答

8
假设您想在模态框的主体中添加图像,您可以使用以下代码:
.modal-body {
   background-image: url(path/to/image);
}

或者,如果您希望将其放在模态框的后面,则可以使用以下代码:
.modal-backdrop {
   background-image: url(path/to/image);
}

模态框主体只将图像作为背景放置在主体上(如果您愿意,可以放置在模态框的中间)。背景覆盖了模态框弹出窗口后面的内容。是否有一种方法可以用图像替换整个模态框背景(使页眉/页脚也位于单个图像下方)? - n00b

1

是的,只需使用 background-image: url(你的图片路径);

$('#myModal').modal() 
.modal-body {
    background-image: url('http://myfunnymemes.com/wp-content/uploads/2015/04/Doge-Meme-Lion-In-All-Its-Majestic-Glory.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    height: 400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css">

<!-- Modal -->
<div class="modal fade" 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-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        test
      </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>


1
我已经通过下面的代码得到了所需的结果,可能对某些人有帮助:
在正文中只显示适合的图像:
.modal-body {
  background-size: cover;
  background-image: url("./bg_image.png");
}

在完整的模态框中显示适合的图像(包括标题和页脚):
.modal-content {
  background-size: cover;
  background-image: url("./bg_image.png");
}

编辑:我使用的模态框是从这里直接复制的标准模态框。


1

我假设您正在使用Bootstrap。

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>

你需要编辑你的。
<div class="modal-content">
    ...
</div>

至:

<div class="modal-content modal-popout-bg">

css:

.modal .modal-popout-bg {
    background-image: url("http://somepic.com/somepic.jpg");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接