简体   繁体   English

如何使用 HTML 和 CSS 并排对齐图像?

[英]How to align images side by side using HTML and CSS?

I am trying to place three images on my page with text description underneath and three more images underneath those with text description underneath those images.我想在我的页面上放置三张图片,下面是文字说明,另外三张图片放在那些图片下面有文字说明的图片下面。

I need to create this using only HTML and CSS but not using Flex.我只需要使用 HTML 和 CSS 而不是使用 Flex 创建它。

I've managed to get all 6 images going down the left of my page but no matter what I try - float, display, grid etc, I can not get a simple result.我已经设法让所有 6 张图像都在我的页面左侧,但无论我尝试什么 - 浮动、显示、网格等,我都无法得到一个简单的结果。

Any suggestions?有什么建议? Thanks谢谢

You can do this with grid like you mentioned.你可以像你提到的那样使用grid来做到这一点。 you would used the grid-template-column property to define how you want your columns to be look.您将使用grid-template-column属性来定义您希望列的外观。

Here I used 3 columns ( 1fr per column) to show an image and some text under it.在这里,我使用了 3 列(每列1fr )来显示图像和其下的一些文本。

There are other ways to do this but grid is great for this kind of thing.还有其他方法可以做到这一点,但grid非常适合这种事情。 You can then use a media query on .grid to display: block for mobile if you wanted them to stack然后,您可以在.grid上使用媒体查询来display: block如果您希望它们堆叠,则display: block移动

 .grid { display: grid; grid-template-columns: 1fr 1fr 1fr; } .child { padding: 10px; } img { width: 200px; height: auto; }
 <div class="grid"> <div class="child"> <img src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=1.00xw:0.669xh;0,0.190xh&resize=640:*" alt="a dog" /> <p>some text</p> </div> <div class="child"> <img src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=1.00xw:0.669xh;0,0.190xh&resize=640:*" alt="a dog" /> <p>some text</p> </div> <div class="child"> <img src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=1.00xw:0.669xh;0,0.190xh&resize=640:*" alt="a dog" /> <p>some text</p> </div> </div>

You can try using setting the page up in a 'row' :您可以尝试使用在“行”中设置页面:

<div class="row">
  <div class="column">
    <img src="img_snow.jpg" alt="Snow" style="width:100%">
  </div>
  <div class="column">
    <img src="img_forest.jpg" alt="Forest" style="width:100%">
  </div>
  <div class="column">
    <img src="img_mountains.jpg" alt="Mountains" style="width:100%">
  </div>
</div>

<style>
   .column {
     float: left;
     width: 33.33%;
     padding: 5px;
   }
</style>

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

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