简体   繁体   English

用CSS对齐图像

[英]Aligning an image with CSS

I am trying to center an image using CSS and Im pretty sure I am using the correct selector in CSS because I have resized the image, but I cant get it to align properly. 我正在尝试使用CSS居中放置图像,并且非常确定我在CSS中使用了正确的选择器,因为我已经调整了图像的大小,但是我无法使其正确对齐。

My HTML: 我的HTML:

</br>
<div class="copyright">
  <em>TWO GREEN THUMBS 2013© ALL RIGHTS RESERVED</em>
</div>
</br>
<div class="sponsors">
  <a href="https://craneflight.org">
    <img src="https://greenthumbsfarm.com/wp-content/uploads/2017/07/CRANE-FLIGHT-COMP-2-1.jpg" alt="crane-flight-logo" class="thumb" />
  </a>
</div>

Here's what i tried for CSS: 这是我为CSS尝试的内容:

.sponsors>img {
  display: inline-block;
  vertical-align: middle;
}    

 .sponsors>img { display: inline-block; vertical-align: middle; } img.thumb { width: 100px; height: 150px; } 
 </br> <div class="copyright"> <em>TWO GREEN THUMBS 2013© ALL RIGHTS RESERVED</em> </div> </br> <div class="sponsors"> <a href="https://craneflight.org"> <img src="https://greenthumbsfarm.com/wp-content/uploads/2017/07/CRANE-FLIGHT-COMP-2-1.jpg" alt="crane-flight-logo" class="thumb" /> </a> </div> 

and

.sponsors>img.align-center {
  display: block;
  margin: 0px auto;
}

 .sponsors>img.align-center { display: block; margin: 0px auto; } img.thumb { width: 100px; height: 150px; } 
 </br> <div class="copyright"> <em>TWO GREEN THUMBS 2013© ALL RIGHTS RESERVED</em> </div> </br> <div class="sponsors"> <a href="https://craneflight.org"> <img src="https://greenthumbsfarm.com/wp-content/uploads/2017/07/CRANE-FLIGHT-COMP-2-1.jpg" alt="crane-flight-logo" class="thumb align-center" /> </a> </div> 

I am trying to get the image to align at the bottom of the page below where it says "TWO GREEN THUMBS 2013© ALL RIGHTS RESERVED", but I just can't figure it out. 我正在尝试使图像在页面底部对齐,该页面的底部显示“两个绿色缩略图2013©保留所有权利”,但我无法弄清楚。 I have looked through multiple similar questions and none of the other solutions seem to be working in this case. 我浏览了多个类似的问题,在这种情况下,其他解决方案似乎都无效。 Could it be I am not using the selector properly? 可能是我没有正确使用选择器吗?

EDIT: I have the image resized with the "thumb" class selector because there will be several images and they will all need to be the same size. 编辑:我有“拇指”类选择器调整图像的大小,因为将有几个图像,它们都将需要相同的大小。 Here's the code: 这是代码:

img.thumb {
    width: 100px;
    height: 150px;
}

Try to aligning it with the div: 尝试使其与div对齐:

<div class="sponsors" align="center">
<a href="https://craneflight.org">
<img src="https://greenthumbsfarm.com/wp-content/uploads/2017/07/CRANE-FLIGHT-COMP-2-1.jpg" alt="crane-flight-logo" class="thumb"/>
</a>
</div>

You can try to apply the style to the container DIV. 您可以尝试将样式应用于容器DIV。

.sponsors {
    text-align: center;

}

I added a container around both elements and made that an inline-block (so it would be only the width of its contents and stay left) . 我在这两个元素周围添加了一个容器,并使其成为一个内联块(因此,它仅是其内容的宽度并保持在左侧)。 Then I centered the image inside that container by applying text-align-center to the .sponsors DIV . 然后,通过对.sponsors DIV应用text-align-center ,将图像居中放置在该容器中。 I also centered the top line to be center aligned with the image using text-align: center . 我还使用text-align: center将顶线居中与图像居中对齐。

BTW: You had a wrong CSS selector for the image. 顺便说一句:您为图片使用了错误的CSS选择器。 There is an a tag between the div and the image, so .sponsors>img won't have any effect. div和图像之间有a标签,因此.sponsors>img不会起作用。 Either .sponsors>a>img or just .sponsors img .sponsors>a>img或只是.sponsors img

 .container1 { display: inline-block; } .copyright { text-align: center; } .sponsors { text-align: center; } .sponsors img { width: 100px; height: 150px; } 
 <div class="container1"> <div class="copyright"> <em>TWO GREEN THUMBS 2013© ALL RIGHTS RESERVED</em> </div> <div class="sponsors"> <a href="https://craneflight.org"> <img src="https://greenthumbsfarm.com/wp-content/uploads/2017/07/CRANE-FLIGHT-COMP-2-1.jpg" alt="crane-flight-logo" class="thumb" /> </a> </div> </div> 

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

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