【问题标题】:Center then left align image grid in Bootstrap fluid width在 Bootstrap 流体宽度中居中然后左对齐图像网格
【发布时间】:2013-12-28 18:47:22
【问题描述】:

我正在使用具有以下要求的 Bootstrap 3.0:

  1. 每个缩略图图像都有固定的已知宽度,在这种情况下假设为 200 像素。 可以有多列(如果屏幕很大,根据需要可以有5-6列)。
  2. 整个页面必须是流体宽度
  3. 容器(在本例中为.center)必须位于页面的中心
  4. 然后在容器内(本例中为.grid),缩略图本身必须左对齐。例如,如果页面每行适合 3 张图片,但最后一行只有 2 张图片,则这 2 张图片必须左对齐。
  5. 我可以更改 HTML 结构但没有 JS。页面和整个容器的宽度未知。

示例 1:本例中的列数为 4。图片(蓝色)在页面中心的灰色容器(黄色)内左对齐。

示例 2:当屏幕宽度较小时,图像会向下折叠(因此本例中为 3 列)。但是图像容器仍然居中,所有图像都左对齐。

我当前的代码只左对齐,但.center 类实际上没有任何效果。不知何故,我需要 .center 类(红色边框)具有内部图像的宽度(不是全宽),然后将其自身居中。

HTML:

<div class='col-md-12'>
  <div class='center'>
    <div class='grid'>
      <div class='thumbnail'>
        <img src='http://placehold.it/200x200' />
        <div class='caption'>
          <h3>Donec id elit non mi porta gravida at eget metus</h3>
          <p>Fusce dapibus tellus ac cursus commodo</p>
        </div>
      </div>
      <div class='thumbnail'>
        <img src='http://placehold.it/200x200' />
        <div class='caption'>
          <h3>Donec id elit non mi porta gravida at eget metus</h3>
          <p>Fusce dapibus tellus ac cursus commodo</p>
        </div>
      </div>
      <div class='thumbnail'>
        <img src='http://placehold.it/200x200' />
        <div class='caption'>
          <h3>Donec id elit non mi porta gravida at eget metus</h3>
          <p>Fusce dapibus tellus ac cursus commodo</p>
        </div>
      </div>
      <div class='thumbnail'>
        <img src='http://placehold.it/200x200' />
        <div class='caption'>
          <h3>Donec id elit non mi porta gravida at eget metus</h3>
          <p>Fusce dapibus tellus ac cursus commodo</p>
        </div>
      </div>
      <div class='thumbnail'>
        <img src='http://placehold.it/200x200' />
        <div class='caption'>
          <h3>Donec id elit non mi porta gravida at eget metus</h3>
          <p>Fusce dapibus tellus ac cursus commodo</p>
        </div>
      </div>
      <div class='thumbnail'>
        <img src='http://placehold.it/200x200' />
        <div class='caption'>
          <h3>Donec id elit non mi porta gravida at eget metus</h3>
          <p>Fusce dapibus tellus ac cursus commodo</p>
        </div>
      </div>
      <div class='thumbnail'>
        <img src='http://placehold.it/200x200' />
        <div class='caption'>
          <h3>Donec id elit non mi porta gravida at eget metus</h3>
          <p>Fusce dapibus tellus ac cursus commodo</p>
        </div>
      </div>
    </div>
  </div>
</div>

CSS:

.col-md-12 {
    background: yellow;
}

.center {
    margin-left: auto;
    margin-right: auto;
    border: 1px solid red;
}

.grid {
    text-align: left;
}

.thumbnail {
    display: inline-block;
    width: 215px;
    max-width: none;
}

链接: http://jsfiddle.net/qhoc/Rp838/

有什么帮助吗?

更新 1:

按照 Matt 的建议“将整个东西包装在一个 .container 中”。仍然没有工作,因为您看到红色边框不是页面中心。

http://jsfiddle.net/qhoc/Rp838/1/

【问题讨论】:

  • 去掉 .center div。将整个东西包裹在一个 .container 中,它将是一个居中的布局。如果您不希望它全角,请在容器中添加一些左右边距
  • 请看我的更新1。没用。
  • 我在看你的小提琴,看起来 .center 类元素已经居中了?
  • @Lloyd 请看我的截图

标签: html css twitter-bootstrap stylesheet


【解决方案1】:

在经历了一上午的事情后终于解决了。 这是它的jsfiddle。

http://jsfiddle.net/Rp838/9/

                    .menu {
              display: block;
              height: 100%;
              max-width: 1000px;
              margin-left: auto;
              margin-right: auto;
              text-align: center;
              /*flex-direction: row;
                flex-wrap: wrap;
                justify-content: space-around;*/
            }

            .menuItem {
              display: inline-block;
              position: relative;
              margin: 0 5px 20px 5px;
              max-width: 320px;
            }

            .menuItem img {
              max-width: 100%;
            }

            .menuItem h2 {
              position: absolute;
              bottom: 0;
              left: 0;
              width: 100%;
              margin: 0;
              text-align: center;
              color: white;
              font: bold 1.5em/2em Helvetica, Sans-Serif;
              background: rgb(0, 0, 0);
              /* fallback color */

              background: rgba(0, 0, 0, 0.7);
            }

            .menuItem h3 {
              position: absolute;
              bottom: 0;
              left: 0;
              width: 100%;
              margin: 0;
              text-align: center;
              color: white;
              font: bold 1em/1.5em Helvetica, Sans-Serif;
              background: rgb(0, 0, 0);
              /* fallback color */

              background: rgba(0, 0, 0, 0.7);
            }

            .menu li {
              list-style: none;
            }

            .menu a {
              font-weight: normal;
              text-decoration: none;
            }

            .menu a:hover {
              text-decoration: underline;
            }

            .menu a:active {
              text-decoration: underline;
            }






                <div class="menu">
                <div class="menuItem">
                  <a href="history.html"><img src="https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97200&w=200&h=200" alt="History" />
                    <h2>History</h2>
                  </a>
                </div>
                <div class="menuItem">
                  <a href="photographs.html"><img src="https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97200&w=200&h=200" alt="Photographs" />
                    <h2>Photographs</h2>
                  </a>
                </div>
                <div class="menuItem">
                  <a href="videos.html"><img src="https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97200&w=200&h=200" alt="Videos" />
                    <h2>Videos</h2>
                  </a>
                </div>
                <div class="menuItem">
                  <a href="documents.html"><img src="https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97200&w=200&h=200" alt="Documents" />
                    <h2>Documents</h2>
                  </a>
                </div>
                <div class="menuItem">
                  <a href="maps.html"><img src="https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97200&w=200&h=200" alt="Maps" />
                    <h2>Maps</h2>
                  </a>
                </div>
                <div class="menuItem">
                  <a href="biographies.html"><img src="https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97200&w=200&h=200" alt="Biographies" />
                    <h2>Biographies</h2>
                  </a>
                </div>
              </div>

【讨论】:

    【解决方案2】:

    好的,我们开始吧,您需要重新构建标记以遵循引导标准

    CSS

    .container {
        background: yellow;
    }
    
    .thumbnail {
        width: 200px;
        display: inline-block;
    }
    

    HTML

    <div class="container">
        <div class="thumbnail">
            <img src="http://placekitten.com/200/200" width="200" height="200" alt="" />
            <div class="caption">
            <h3>Thumbnail label</h3>
            <p>blah blah blah blah blah</p>
        </div>
        </div>
        <div class="thumbnail">
            <img src="http://placekitten.com/200/200" width="200" height="200" alt="" />
            <div class="caption">
            <h3>Thumbnail label</h3>
            <p>blah blah blah blah blah</p>
        </div>
        </div>
        <div class="thumbnail">
            <img src="http://placekitten.com/200/200" width="200" height="200" alt="" />
            <div class="caption">
            <h3>Thumbnail label</h3>
            <p>blah blah blah blah blah</p>
        </div>
        </div>
        <div class="thumbnail">
            <img src="http://placekitten.com/200/200" width="200" height="200" alt="" />
            <div class="caption">
            <h3>Thumbnail label</h3>
            <p>blah blah blah blah blah</p>
        </div>
        </div>
        <div class="thumbnail">
            <img src="http://placekitten.com/200/200" width="200" height="200" alt="" />
            <div class="caption">
            <h3>Thumbnail label</h3>
            <p>blah blah blah blah blah</p>
        </div>
        </div>
        <div class="thumbnail">
            <img src="http://placekitten.com/200/200" width="200" height="200" alt="" />
            <div class="caption">
            <h3>Thumbnail label</h3>
            <p>blah blah blah blah blah</p>
        </div>
        </div>
    </div>
    

    如果您想查看它的实际效果,请查看fiddle。我使图像具有响应性,因此请确保将窗口拉得足够远。在较小的视口上,图像将拉伸以适应。

    【讨论】:

    • 只更新上面的要求(1)。基本上我不想限制为 X 列数。我忘了提几件事。我没有在 Bootstrap 3 中使用 .container.row 的原因是因为我希望它是流动的宽度。从 v3 开始,这两个类不是必需的。然后每个拇指将只是inline-block 并保持彼此相邻并在达到宽度时向下滚动。
    • 好吧,如果你删除 .container 和 .row 那么它应该像上面那样工作。您的原始标记中仍然存在错误。不知道你为什么不想使用 .container。它本质上与您使用 .grid 所做的相同。似乎过于复杂。如果您不想使用 .row 在您的用例中有意义
    • 实际上不得不改变一些其他的事情。上面的代码现在应该适合你了。小提琴也更新了
    • 新代码使用width:100%,但在我的情况下,图像具有固定宽度(无响应)。想象一下,整个图像网格必须位于屏幕的中心,但在网格内,它们是左对齐的。
    • 是的,应该可以。 .container 将布局在浏览器中居中。如果您想要更多的左右边距,只需将其添加到 .container 即可。里面的内容是左对齐的。将其放入实际页面进行测试。小提琴可能有点误导。我删除了响应式图像的东西。每个图像和标题的宽度将由图像宽度设置
    猜你喜欢
    • 2014-10-18
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2011-10-20
    • 2019-12-14
    相关资源
    最近更新 更多