【问题标题】:How do I center a column of images vertically, responsively for mobile devices我如何垂直居中一列图像,以响应移动设备
【发布时间】:2017-05-02 06:01:03
【问题描述】:

我正在编写一个显示 9 张具有特定标签的最新照片的应用程序。我通过将每张图片的尺寸设置为 240 像素宽和 200 像素高来解决不同图像尺寸的问题。

我的问题是如何将它们垂直居中以使它们位于屏幕宽度的中间?我希望它们具有响应性,因此无论显示它们的设备的宽度如何,它们始终位于中间。

HTML:-

<div id="results" class="row"></div> 

到目前为止,我已经尝试了几乎所有可以在互联网上找到的东西。我应该标记,有 9 张照片正在显示,所以用户必须滚动。这意味着每个解决方案都包含 position: absolute;不会的。

正在通过 Flickr API 挑选照片。之后,我 .append() 将它们添加到 div#results 中:-

$("#results").append('<div class="miniaturka col-xs-12 col-sm-4 col-md-4 col-lg-4"><a href="'+myresult_size.url+'" target="_blank"><img src="'+myresult_size.source+'" ></a></div>');

引导网格无法使它们居中。

【问题讨论】:

    标签: jquery html css twitter-bootstrap


    【解决方案1】:

    试试这个

    &lt;div id="results" class="row text-center"&gt;&lt;/div&gt; 

    或使用

    #results{
      margin: 0 auto;
      }

    【讨论】:

    • 抱歉,您的建议均无效。图片保持在屏幕左侧的位置
    【解决方案2】:

    对于 4 宽的单元格,您需要使用偏移类 col-sm-offset-4。 当您定义col-sm-4 时,所有用于更宽视口的类都将继承此定义,我的意思是col-sm-4 等于col-sm-4 col-md-4 col-lg-4。您还需要为比其子图像更宽的单元格应用 text-center 类。

    所以结果是:

    $("#results").append('<div class="miniaturka col-xs-12 col-sm-4  col-sm-offset-4 text-center">
        <a href="'+myresult_size.url+'" target="_blank">
           <img src="'+myresult_size.source+'" >
         </a>
        </div>');
    

    这里是一个例子 sn-p:

    img {
      display: block;
      margin: 0 auto;
      max-width: 240px;
      max-height: 200px;
      height: auto;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="row">
      <div class="miniaturka col-xs-12 col-sm-4 col-sm-offset-4 text-center">
        <a href="#" target="_blank">
          <img src="http://placekitten.com/g/200/300">
        </a>
        <a href="#" target="_blank">
          <img src="http://placekitten.com/g/300/300">
        </a>
        <a href="#" target="_blank">
          <img src="http://placekitten.com/g/400/300">
        </a>
        <a href="#" target="_blank">
          <img src="http://placekitten.com/g/200/300">
        </a>
        <a href="#" target="_blank">
          <img src="http://placekitten.com/g/200/300">
        </a>
        <a href="#" target="_blank">
          <img src="http://placekitten.com/g/200/300">
        </a>
      </div>
    </div>

    【讨论】:

    • 对不起,没有任何改变。图片保持与交换前相同的位置。
    • img 添加了 CSS 规则。看看改过的sn-p
    • 现在图像溢出边框,边框本身粘在屏幕左侧
    • 你添加了css样式吗?您在问题中没有给出任何内容。看看jsfiddle:jsfiddle.net/xet1osha
    • 您似乎没有在页面中粘贴指向 bootstrap.css 的链接。 &lt;link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/&gt;
    【解决方案3】:

    Text-align center on parent 将所有内联的子元素居中。

    .miniaturka {
      width: 100vw;
      text-align: center;
    }
    <div id="results" class="row">
      <div class="miniaturka col-xs-12 col-sm-4 col-md-4 col-lg-4">
        <a href="#" target="_blank">
          <img src="http://placehold.it/350x150">
        </a>
      </div>
      <div class="miniaturka col-xs-12 col-sm-4 col-md-4 col-lg-4">
        <a href="#" target="_blank">
          <img src="http://placehold.it/350x150">
        </a>
      </div>
    </div>

    【讨论】:

    • width: 100vw;col-sm-4 宽度有什么关系? col-sm-4 类表示左侧和右侧存在相同宽度的单元格。如果它们是空的,那么既不声明 width: 100vw; 也不声明 col-sm-4 是没有意义的。
    猜你喜欢
    • 2013-03-04
    • 2015-02-13
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2015-09-06
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多