简体   繁体   English

将图像与引导程序 4 中的列并排对齐

[英]Aligning Images side-by-side with columns in bootstrap 4

I am using Bootstrap 4, and trying to align my columns so that I have 2 images next to each-other on medium or larger viewports, but they keep aligning themselves one on top of the other.我正在使用 Bootstrap 4,并尝试对齐我的列,以便在中等或较大的视口上有 2 个彼此相邻的图像,但它们会保持一个在另一个顶部对齐。

<div class="row">
                        <div class="col-6">
                            <img class="img-fluid"
                                src="img/1.PNG">
                        </div>
                        <div class="col col-5 d-none d-md-block">
                            <img class="img-fluid"
                                src="img/2.PNG">
                        </div>

                    </div>

Image of code and example代码和示例的图像

That should work just fine if you take all the extra bits from the Row classes and just have class="row" Your column widths are both set to 5 though, 6 would be ideal for centering.如果您从 Row 类中取出所有额外的位并且只设置class="row"您的列宽都设置为5 ,那么这应该可以正常工作,但6是居中的理想选择。

To assist with sleeker code, on the img classes try removing it all except for img-fluid为了帮助编写更流畅的代码,在img类上尝试将其全部删除,除了img-fluid

Your column classes are wrong.你的列类是错误的。 col fills all available space, and col-5 takes 5 columns on all screens. col填充所有可用空间, col-5在所有屏幕上占据 5 列。 And in this case your col-5 is overriding your col anyway.在这种情况下,您的col-5无论如何都会覆盖您的col What you actually need to do:你实际需要做的:

  • Use col-md-6 on both the images in place of col col-5 .在两个图像上使用col-md-6代替col col-5 Your images will align themselves side by side from medium and up.您的图像将从中向上并排对齐。

Also, you do NOT need to add d-none d-md-block to the parent and all the child classes.此外,您不需要将d-none d-md-block到父类和所有子类。 If you don't want to show a particular div in less than md, using it on the parent is enough.如果您不想在小于 md 的时间内显示特定的 div,则在父级上使用它就足够了。

For demonstration purposes I went ahead and used a dummy image that I could get to work.出于演示目的,我继续使用了一个我可以开始工作的虚拟图像。 Your structure should be as follows.您的结构应如下所示。

 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous"> <div class="row"> <div class="col-6 col-lg-6 col-sm-6"> <img src="https://dummyimage.com/600x400/000/fff"> </div> <div class="col-6 col-lg-6 col-sm-6"> <img src="https://dummyimage.com/600x400/000/fff"> </div> </div>

Using the different Large, Medium, and Small bootstrap classes allows for your row to adjust on different screen sizes, while maintaining that row structure.使用不同的大、中和小引导类允许您的行在不同的屏幕尺寸上进行调整,同时保持该行结构。 This link here is also very helpful for learning the system.这里的这个链接对学习系统也很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM