当前位置: 首页 > 面试题库 >

使图像在Bootstrap中居中

东郭宏朗
2023-03-14
问题内容

我正在使用Bootstrap 3.0创建网站。我是新手。我想要的是,当浏览器尺寸太小时,我想在div中心放置图像,我有此代码。

<div class="col-lg-10 ccol-lg-offset-1 col-md-12 col-sm-12 ">
    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
        <img src="images/2.png" class="img-responsive" />
        <p class="text-center"><a href="javascript:void(0);">Taj Group</a></p>
    </div>
    <div class="col-lg-2  col-md-2 col-sm-2 col-xs-12">
        <img src="images/2.png" class="img-responsive" />
        <p class="text-center"><a href="javascript:void(0);">Taj Group</a></p>
    </div>
    <div class="col-lg-2  col-md-2 col-sm-2 col-xs-12">
        <img src="images/2.png" class="img-responsive" />
        <p class="text-center"><a href="javascript:void(0);">Taj Group</a></p>
    </div>
    <div class="col-lg-2  col-md-2 col-sm-2 col-xs-12">
        <img src="images/2.png" class="img-responsive" />
        <p class="text-center"><a href="javascript:void(0);">Taj Group</a></p>
    </div>
    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
        <img src="images/2.png" class="img-responsive" />
        <p class="text-center"><a href="javascript:void(0);">Taj Group</a></p>
    </div>
    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
        <img src="images/2.png" class="img-responsive" />
        <p class="text-center"><a href="javascript:void(0);">Taj Group</a></p>
    </div>

问题答案:

更新2018

Bootstrap 2.x

您可以创建一个新的CSS类,例如:

.img-center {margin:0 auto;}

然后,将其添加到每个IMG:

 <img src="images/2.png" class="img-responsive img-center">

或者,.img-responsive如果要将所有图像居中,则只需覆盖即可。

 .img-responsive {margin:0 auto;}

演示: http
//bootply.com/86123

Bootstrap 3.x

编辑 -随着Bootstrap 3.0.1的发布,center-block现在可以使用该类,而无需任何其他CSS。

 <img src="images/2.png" class="img-responsive center-block">

引导程序4

在Bootstrap 4中,mx- auto该类(自动x轴边距)可用于将图像居中display:block。但是,img是display:inline默认设置,因此text- center可以在父级上使用。

<div class="container">
    <div class="row">
        <div class="col-12">
            <img class="mx-auto d-block" src="//placehold.it/200">  
        </div>
    </div>
    <div class="row">
        <div class="col-12 text-center">
            <img src="//placehold.it/200">  
        </div>
    </div>
</div>

Bootsrap 4-中心图像演示



 类似资料:
  • 自从转到Bootstrap 3以来,我一直在努力使一些图像居中。以前,我会在整个coll元素上使用以分页为中心的类,但现在不起作用了。 此外,我尝试创建类img中心来使用margin:0 auto(根据此处的答案),但这也不起作用,我不确定为什么。如果有人能给我指出正确的方向,那就太好了。

  • 问题内容: 我已经将图像内的边框设置为none。我现在想将该图像放在其包含div的中心。我曾尝试使用,但没有用。 我敢肯定,我忽略了一些愚蠢的事情,但是我想得到stackoverflow社区的帮助,所以这并不需要我花一个小时盯着屏幕来弄清楚。非常感谢。 这是CSS … 问题答案: 尝试将图片的属性设置为:

  • 问题内容: 这可能是一个愚蠢的问题,但是由于居中对齐图像的常规方法不起作用,所以我想问一下。如何在容器div内居中对齐(水平)图像? 这是HTML和CSS。我还包括了缩略图其他元素的CSS。它以降序运行,因此最高的元素是所有内容的容器,最低的元素是所有内容的内部。 好的,我添加了不带PHP的标记,因此应该更容易理解。这两种解决方案似乎都无法在实践中起作用。顶部和底部的文本不能居中,并且图像应在其容

  • 问题内容: 我想居中背景图像。没有使用div,这是CSS样式: 上面的CSS会全部平铺并将其居中,但是看不到一半的图像,只是向上移动了一点。我想做的就是将图像居中。我可以在21英寸的屏幕上观看图像吗? 问题答案: background-image: url(path-to-file/img.jpg); background-repeat:no-repeat; background-position

  • 我有一个图标img,并试图将其居中在一个卡片div中,但不知何故不起作用。我已经尝试了和和两者都没有帮助。我在下面附上了我的代码。感谢任何帮助。提前道谢。 null null

  • 问题内容: 是否可以将div中的背景图像居中放置?我已经尝试了几乎所有内容-但没有成功: 这可能吗? 问题答案: