简体   繁体   English

如何在 HTML 和 CSS 中的任何 div 中居中图像?

[英]How can i center image in any div in HTML & CSS?

输出

How can i center image in any div in HTML & CSS ?如何在 HTML 和 CSS 中的任何 div 中居中图像?

I have a nav bar that uses images as links for other pages, the nav bar is multiple divs each has an image and anther div contains a words, how can I center this img in the div我有一个导航栏,它使用图像作为其他页面的链接,导航栏是多个 div,每个 div 都有一个图像,花药 div 包含一个单词,我如何将此 img 居中放置在 div 中

The CSS: CSS:

#Nav
{           
  height:  150px; 
  margin-top: 50px;
}
#Nav ul
{
  list-style-type: none;
  padding: 50px;
  padding-top: 0px;
  width: 100%;
}
#Nav li
{
 float: left;
 font-size: 12px; 
 font-weight: bold; 
 letter-spacing: 0; 
 margin: 30px;
}
#Nav a
{
 display: inline-block;
 padding: 5px;  
 width: 70px;
 height: 100px;
 color: black;
 text-decoration: none;
}
#Nav a:hover 
{
 border: 2px solid #ddd;
 border-radius: 4px;
 box-shadow: 0 0 2px 1px rgba (0, 140, 186, 0.5);
}
#img img 
{
 align-self: middle; 
 width: 50px;
 height: 50px;
}
#desc 
{
 margin-top: 15px;
 text-align: center;
}

The html: html:

<li> <a target="_blank" href="#"> 

          <div id="img"> <img src="C:/Users/hp1/Desktop/Website/Pictures/Accessories.jpg" alt="Accessories">

            <div id="desc"> Accessories </div> 

          </div> 

 </a> </li>

You need to change your CSS for image like below:-您需要更改图像的 CSS,如下所示:-

#img img 
{
 display: block;
 margin: 0 auto;
}

You can see Many Demos Here-您可以在这里看到许多演示-

Note that align-self is for elements inside a flexbox container which is not your case, you can try with text-align: center;请注意, align-self 用于 flexbox 容器内的元素,这不是您的情况,您可以尝试使用text-align: center; on img .img

Or if you wish to go full flexbox, set the img container to:或者,如果您希望使用完整的 flexbox,请将 img 容器设置为:

.containerClass {
    display: flex;
    flex-flow: column wrap;
    justify-content: center;
}

With this setup align-self on the img is not need since justify-content on it's container will do.有了这个设置, img上的align-self就不需要了,因为它的容器上的justify-content就可以了。

Flexbox to the rescue: Flexbox来拯救:

 .parent { width:200px; height:200px; border:2px solid #000; display: flex; /* Make it flex */ flex-direction: column; /* wrap children elements to columns */ align-items: center; /* Center children horizontally */ justify-content: center; /* Center children vertically */ }
 <div class="parent "> <img src="http://placehold.it/100x100/cf5"> <p>Green-ish</p> </div> <div class="parent "> <img src="http://placehold.it/100x100/5fc"> <p>Blue-ish</p> </div>

align-self: center; is valid, align-self: middle;有效, align-self: middle; is not valid.无效。

However, you can probably achieve what you're wanting with text-align: center;但是,您可能可以使用text-align: center;实现您想要的效果text-align: center; . . Despite it being an image, you can still center using text-align as images are, by default, inline elements just like text.尽管它是一个图像,您仍然可以使用 text-align 居中,因为默认情况下,图像和文本一样是内联元素。

#img img 
{
 align-self: center; 
 width: 50px;
 height: 50px;
}

OR或者

#img img 
{
 text-align: center; 
 width: 50px;
 height: 50px;
}

First you have to change the nature of div to table then make its align to center like this首先,您必须将 div 的性质更改为 table,然后像这样将其对齐到中心

 #img img 
{
display:table;
 align: center; 
 width: 50px;
 height: 50px;
}

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

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