【问题标题】:Bootstrap 4 image grid alignmentBootstrap 4 图像网格对齐
【发布时间】:2019-12-14 02:55:26
【问题描述】:

所以我正在使用引导程序 4 创建一个图像网格。所有网格都包含在“展示”类中。第一行有 2 张风景照片,第二行有 3 张肖像照片。问题是,我希望纵向照片与横向照片对齐。1 个在左侧,1 个在中心,1 个在右侧。我希望它们具有相同的边距,这样看起来不错。

我尝试自行搜索和设置边距。我尝试使用“row-sm-4”之类的东西。

<div class = "showcase">
              <!-- First row with 2 landscape wallpapers -->
              <div class = "row" id = "land">
                <div class = "col">
                      <img src="img/land1.jpg" class="rounded float-left" alt = "Landscape wallpaper" width = "500" height = "350">
                      <img src="img/land2.jpg" class="rounded float-right" alt = "Landscape wallpaper" width = "500" height = "350">
              </div>
            </div>
              <!-- Second row with 3 portrait wallpapers-->
              <div class = "row" id = "port">
                <div class = "col-bg-4">
                  <img src="img/p1.jpeg" class="rounded float-left" id = "firstp" alt = "Portrait wallpaper" width = "200" height = "350">
                  <img src="img/p2.jpg" class="float-center" id = "secondp" alt = "Portrait wallpaper" width = "200" height = "350"> 
                  <img src="img/p3.jpg" class="rounded float-right" alt = "Portrait wallpaper" width = "200" height = "350">  
              </div>
            </div>
          </div>

它应该以相同的左右边距显示所有照片。

【问题讨论】:

    标签: html css twitter-bootstrap image bootstrap-4


    【解决方案1】:

    如果我完全理解您的意思,那么我认为您需要做的是将图像放入正确数量的列中。例如:

    您的 2 张风景图片需要分成两列,跨度为 6。

    您的 4 张肖像图像需要分成 3 列,跨度为 4。

    这应该看起来像这样:

               <div class = "showcase">
                  <!-- First row with 2 landscape wallpapers -->
                  <div class = "row" id = "land">
                    <div class = "col-6">
                          <img src="img/land1.jpg" class="rounded" alt = "Landscape wallpaper" width = "500" height = "350">
                    </div>
                    <div class="col-6">
                          <img src="img/land2.jpg" class="rounded" alt = "Landscape wallpaper" width = "500" height = "350"> 
                    </div>
                  </div>
                </div>
                  <!-- Second row with 3 portrait wallpapers-->
                  <div class = "row" id = "port">
                      <div class="col-4">                  
                          <img src="img/p1.jpeg" class="rounded" id = "firstp" alt = "Portrait wallpaper" width = "200" height = "350">
                      </div>
                      <div class="col-4 text-center">                  
                          <img src="img/p2.jpg" class="" id = "secondp" alt = "Portrait wallpaper" width = "200" height = "350"> 
                      </div>
    
                      <div class="col-4">
                           <img src="img/p3.jpg" class="rounded" alt = "Portrait wallpaper" width = "200" height = "350">
                      </div>  
                  </div>
              </div>
    

    告诉我进展如何!你也不需要floats

    【讨论】:

    • 你理解的没错。问题是右边,它起作用了。但是从中间照片到左边照片的距离大于从中间照片到右边照片的距离,这使得它不能修复左侧。
    • 在这种情况下,将 text-center 添加到要居中的 div 中。那应该可以为您解决问题!
    • 哇,它起作用了。起初,它什么也没做,因为我没有使用 col-4 布局。我使用了 css。然后我复制了你的代码,这使它成为我的方式上面说了。当我说文本对齐中心时,它没有做任何事情,但我只需要删除css。谢谢!试图解决这个问题好几个小时。
    • 很高兴能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 2019-01-17
    • 2015-06-14
    • 2018-04-14
    • 1970-01-01
    • 2021-12-10
    • 2022-06-23
    • 2014-12-25
    相关资源
    最近更新 更多