简体   繁体   English

如何在较小的div中将大图像居中?

[英]How to center a large image within a smaller div?

Using Bootstrap 4, I am trying to add a image inside of a smaller div and want to center the image and hide the area that overflow the parent div. 使用Bootstrap 4,我试图在较小的div内添加图像,并希望将图像居中并隐藏溢出父div的区域。 How can I do this? 我怎样才能做到这一点?

Here is a snippet: 这是一个片段:

 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" /> <div class="jumbotron p-3 p-md-5 text-white rounded bg-dark"> <div class="col-md-6 px-0"> <img src="https://i.stack.imgur.com/COO8G.jpg"> <h1 class="display-4 font-italic">Sample</h1> <p class="lead my-3">Sample Text</p> <p class="lead mb-0"><a href="#" class="text-white font-weight-bold">Sample</a></p> </div> </div> 

Edit: The image was added as a child to the div col-md-6 px-0 in an effort to make it responsive. 编辑:将图像作为子级添加到div col-md-6 px-0中,以使其具有响应性。

Edit 2: I don't want to change the size of the image. 编辑2:我不想更改图像的大小。 I want to keep the size, center it vertically and horizontally within the parent div, and hide the parts that are flowing over. 我要保持大小,在父div内垂直和水平居中,并隐藏正在流动的部分。

使用网格系统类justify-content-center ,请在此处查看文档

You can use absolute positioning, I would suggest to add a class to the container for relative positioning 您可以使用绝对定位,建议您将一个类添加到容器中以进行相对定位

 img { position: absolute; top: 50%; left: 50%; transform: translateY(-50%) translateX(-50%); } .col-md-6.px-0 { position: relative; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <div class="jumbotron p-3 p-md-5 text-white rounded bg-dark"> <div class="col-md-6 px-0"> <img src="http://experienceperception.com/images/moe1700_2.jpg"> <h1 class="display-4 font-italic">Sample</h1> <p class="lead my-3">Sample Text</p> <p class="lead mb-0"><a href="#" class="text-white font-weight-bold">Sample</a></p> </div> </div> 

Best to add a class to the container 最好在容器中添加一个类

<div class="col-md-6 px-0 abs-center">

and then 接着

.abs-center {
  position: relative;
}

This centers the image horizontally and vertically on initial load, if you are trying to achieve something else, please update the question 这将使图像在初始加载时水平和垂直居中,如果您要实现其他目标,请更新问题

Try the following snippet. 尝试以下代码段。 I have given 100% width and height to the image. 我已经为图像提供了100%的宽度和高度。 Since it is inside a container, the image will be enlarged up to the container only. 由于它在容器内,因此图像只会放大到该容器。 And I saw in comments that you want the image to be zoom. 我在评论中看到您希望图像放大。 So I added a bootstrap modal as well. 因此,我还添加了一个引导模态。

 $(function() { $('.pop').on('click', function() { $('.imagepreview').attr('src', $(this).find('img').attr('src')); $('#imagemodal').modal('show'); }); }); 
 img { width:100%; height:100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <div class="jumbotron p-3 p-md-5 text-white rounded bg-dark"> <div class="col-md-6 px-0"> <a href="#" class="pop"> <img src="http://experienceperception.com/images/moe1700_2.jpg"></a> <h1 class="display-4 font-italic">Sample</h1> <p class="lead my-3">Sample Text</p> <p class="lead mb-0"><a href="#" class="text-white font-weight-bold">Sample</a></p> </div> </div> <!--Zoom image with a modal--> <div class="modal fade" id="imagemodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-body"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> <img src="" class="imagepreview" style="width: 100%;" > </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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