简体   繁体   English

如何在html中并排对齐图像?

[英]How to align images side by side in html?

I have three images with text and headers I want them at the same row side by side. 我有三个带有文本和标题的图像,我希望它们并排在同一行。 I'm trying to use float left for that but after hundreds of attempts, I failed. 我试图为此使用左移,但经过数百次尝试后,我失败了。

<section class="boxes">

   <div class="box">
     <img src="img/brush.png">
     <h2>graphic desigen</h2>
     <p>Curabitur nec ultrices ex.  
   </dive>

   <div class="box">
     <img src="img/html.png">
     <h2>html 5 marckup</h2>
     <p>Curabitur nec ultrices ex.  
   </dive>     

   <div class="box">
     <img src="img/css.png">
     <h2>graphic desigen</h2>
     <p>Curabitur nec ultrices ex. 
   </div>          

</section>

The issue you have is not the images - its the divs - these are block level elements - so in order to align them in a horizontal row - you need to make them display: inline-block with CSS 您遇到的问题不是图像-它的div-这些是块级元素-因此,要使它们在水平行中对齐-您需要使其display: inline-block使用CSS进行display: inline-block

 .box { display: inline-block; max-width: 33%; text-align: center } h2 { font-size: 18px; } p { font-size: 14px; } 
 <section class="boxes"> <div class="box"> <img src="https://i.pinimg.com/originals/e5/3c/fb/e53cfb282846313a69daf9755bfaf339.jpg" width="150"> <h2>graphic desigen</h2> <p>Curabitur nec ultrices ex. </div> <div class="box"> <img src="https://i.pinimg.com/originals/e5/3c/fb/e53cfb282846313a69daf9755bfaf339.jpg" width="150"> <h2>html5 markup</h2> <p>Curabitur nec ultrices ex. </div> <div class="box"> <img src="https://i.pinimg.com/originals/e5/3c/fb/e53cfb282846313a69daf9755bfaf339.jpg" width="150"> <h2>graphic desigen</h2> <p>Curabitur nec ultrices ex. </div> </section> 

Although this works - the newer way of showing images and associated headings / captions - is the HTML 5 figure element. 尽管这样做有效-显示图像和相关标题/标题的新方法-是HTML 5图形元素。 This houses the image element and the figcaption element together 这将图像元素和figcaption元素合并在一起

 .box { display: inline-block; text-align:center; max-width: 32%; } figcaption { width: 100px; display: block; } h2 { font-size: 12px; } p { font-size: 14px; } 
 <section class="boxes"> <div class="box"> <figure> <img src="https://i.pinimg.com/originals/e5/3c/fb/e53cfb282846313a69daf9755bfaf339.jpg" width="100" /> <figcaption> <h2>graphic desigen</h2> <p>Curabitur nec ultrices ex.</p> </figcaption> </figure> </div> <div class="box"> <figure> <img src="https://i.pinimg.com/originals/e5/3c/fb/e53cfb282846313a69daf9755bfaf339.jpg" width="100" /> <figcaption> <h2>html 5 marckup</h2> <p>Curabitur nec ultrices ex.</p> </figcaption> </figure> </div> <div class="box"> <figure> <img src="https://i.pinimg.com/originals/e5/3c/fb/e53cfb282846313a69daf9755bfaf339.jpg" width="100" /> <figcaption> <h2>graphic desigen</h2> <p>Curabitur nec ultrices ex.</p> </figcaption> </figure> </div> </section> 

You can also use CSS flex to achieve similar UI. 您还可以使用CSS flex来实现类似的UI。

 .boxes { display: flex; } .box { padding: 1em; text-align: center; } h2 { font-size: 18px; } p { font-size: 14px; } 
 <section class="boxes"> <div class="box"> <img src="https://i.pinimg.com/originals/e5/3c/fb/e53cfb282846313a69daf9755bfaf339.jpg" width="150"> <h2>graphic desigen</h2> <p>Curabitur nec ultrices ex. </div> <div class="box"> <img src="https://i.pinimg.com/originals/e5/3c/fb/e53cfb282846313a69daf9755bfaf339.jpg" width="150"> <h2>html5 markup</h2> <p>Curabitur nec ultrices ex. </div> <div class="box"> <img src="https://i.pinimg.com/originals/e5/3c/fb/e53cfb282846313a69daf9755bfaf339.jpg" width="150"> <h2>graphic desigen</h2> <p>Curabitur nec ultrices ex. </div> </section> 

I hate to add yet another version, but even more I hate non-responsive design with absolute "px" units. 我讨厌添加另一个版本,但是我更讨厌带有绝对“ px”单位的无响应设计。

The following example will nicely fill any page width, and will adjust to resizing: 以下示例将很好地填充任何页面宽度,并将调整为调整大小:

 * { box-sizing: border-box; border-collapse: collapse; margin: 0; padding: 0; } figure, aside { border-style: solid; border-width: .05em; margin-left: 2%; page-break-inside: avoid; } figure img, aside img { width: 100%; height: auto; } figcaption { text-align: center; } figcaption>div { font-size: 110%; font-weight: bold; } .boxes figure { display: inline-block; width: 30%; } 
 <section class="boxes"> <figure> <img alt="cat1" src="https://i.stack.imgur.com/yTEcI.jpg" /> <figcaption> <div>graphic design</div> Curabitur nec ultrices ex. </figcaption> </figure><figure> <img alt="cat2" src="https://i.stack.imgur.com/yTEcI.jpg" /> <figcaption> <div>html 5 markup</div> Curabitur nec ultrices ex. </figcaption> </figure><figure> <img alt="cat3" src="https://i.stack.imgur.com/yTEcI.jpg" /> <figcaption> <div>graphic design</div> Curabitur nec ultrices ex. </figcaption> </figure> </section> 

Note that using <figure> and <figcaption> will improve search engine results. 请注意,使用<figure><figcaption>将改善搜索引擎结果。 These tags tell the search engine that the caption belongs with the image. 这些标签告诉搜索引擎标题是图片的所属。 But with generic <div> tags, the search engine can only guess. 但是使用通用<div>标签,搜索引擎只能猜测。

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

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