我使用Bootstrap 3进行目录编制。当在平板电脑上显示时,由于产品尺寸较小(500x500),浏览器中的宽度为767像素,因此产品图像看上去很难看。我想将图像放在屏幕中央,但是由于某些原因,我不能。谁将帮助解决问题?

我使用Bootstrap 3进行目录编制。当在平板电脑上显示时,由于产品尺寸较小(500x500),浏览器中的宽度为767像素,因此产品图像看上去很难看。我想将图像放在屏幕中央,但是由于某些原因,我不能。谁将帮助解决问题?

Answers:
如果您使用的是Bootstrap v3.0.1或更高版本,则应改用此解决方案。它不会使用自定义CSS覆盖Bootstrap的样式,而是使用Bootstrap功能。
我的原始答案如下所示
这是一个非常容易的修复。因为.img-responsive已经从Bootstrap设置了display: block,所以可以margin: 0 auto用来使图像居中:
.product .img-responsive {
    margin: 0 auto;
}margin: 0 auto而不是更改.img-response 创建新类可能更好。
                    .center-blockTwitter Bootstrap 3(自v3.0.1以来)有一个类,因此请使用:
<img src="..." alt="..." class="img-responsive center-block" />class="img-responsive" style="margin: 0 auto;"为我class="img-responsive center-block"工作不(bootstrap 3,但一个月大的版本)
                    <a href="#"> </a>对中,则可点击区域将扩展为包含容器的整个宽度,该宽度可能比图像大很多。这可能会使单击他们认为是“空”空间的用户感到困惑。
                    仅将类添加center-block到映像,这也适用于Bootstrap 4:
<img src="..." alt="..." class="center-block" />注意:center-block即使在img-responsive使用时也可以使用
.text-center如果您使用的是Bootstrap 3,请仅使用class。
<div class="text-center">
    <img src="..." alt="..."/>
</div>注意:这不适用于img响应式
img-responsive
                    您仍然可以img-responsive使用此样式类,而不会影响其他图像。
您可以在该标签之前加上ID / div ID / class部分,以定义img嵌套该标签的顺序。此习惯img-responsive仅在该区域有效。
假设您有一个HTML区域定义为:
<section id="work"> 
    <div class="container">
        <div class="row">
            <img class="img-responsive" src="some_image.jpg">
        </div>
    </div>
</section>然后,您的CSS可以是:
section#work .img-responsive{
    margin: 0 auto;
}注意:此答案img-responsive与整个变更的潜在影响有关。当然,center-block是最简单的解决方案。
尝试使用此代码,因为bootstrap 4没有中心块类,因此它也适用于bootstrap 4的小图标,因此尝试此方法将很有帮助。您可以通过设置改变图像的位置.col-md-12来.col-md-8或者.col-md-4,它高达你。
<div class="container">
       <div class="row">
          <div class="col-md-12">
            <div class="text-xs-center text-lg-center">
           <img src="" class="img-thumbnail">
           </div>
          </div>
       </div>
  </div>您可以通过定义margin:0 auto来解决它
或者您也可以使用col-md-offset
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<style>
.img-responsive{
margin:0 auto;
}
</style>
<body>
<div class="container">
  <h2>Image</h2>
<div class="row">
<div class="col-md-12">
  <p>The .img-responsive class makes the image scale nicely to the parent element (resize the browser window to see the effect):</p>                  
  <img src="http://www.w3schools.com/bootstrap/cinqueterre.jpg" class="img-responsive" alt="Cinque Terre" width="304" height="236"> 
</div>
</div>
</div>
</body>
</html>仅使用标准类应用于所有Booostrap对象的更准确方法是不设置上下边界(因为图像可以从父级继承这些边界),所以我一直在使用:
.text-center .img-responsive {
    margin-left: auto;
    margin-right: auto;
}我也为此做了一个要点,因此,如果由于任何错误而将进行任何更改,更新版本将始终在此处:https : //gist.github.com/jdrda/09a38bf152dd6a8aff4151c58679cc66
:)