简体   繁体   English

为什么我的图片不在div的中心

[英]why my image not in center of div

Why my image not in the center(Horizontally, Vertically) of div. 为什么我的图片不在div的中心(水平,垂直)。

<div>
   <img id = "loader-img" class="centered" alt src="al.gif">
</div>

and my css is: 而我的CSS是:

.centered {
    display: block;
    margin-left: 150px;
    margin-right: 150px;
    margin-bottom: 100px;
    margin-top: 100px;
    height: 200px;
    width: 200px;
   }

Here is how my image looks like in div: 这是我的图像在div中的样子:

在此处输入图片说明

Try This: 尝试这个:

 .frame { height: 125px; width: 160px; border: 1px solid red; white-space: nowrap; text-align: center; margin: 1em 0; } .helper { display: inline-block; height: 100%; vertical-align: middle; } img { vertical-align: middle; max-height: 25px; max-width: 160px; } 
 <div class=frame> <span class="helper"></span> <img src="http://www.afew-store.com/js/vigoshop/ajaxcart/ajax-loader.gif" height=250 /> </div> 

Demo Here 在这里演示

Update: As you say that above is the image which you are using then please note that given image itself is not good. 更新:正如您所说的,以上是您正在使用的图像,请注意,给定的图像本身不是很好。 As it has white background so its difficult to notice the difference. 由于它具有白色背景,因此很难注意到其差异。

I am drawing border around image and posting here again: 我在图像周围绘制边框并再次在此处发布:

在此处输入图片说明

Try this: 尝试这个:

HTML HTML

<div>
   <img src="al.gif">
</div>

CSS CSS

div {
  display: -webkit-flex;
  display: flex;
}

img {
  margin: auto;
}

And also, your id attribute contains space symbols. 而且,您的id属性包含空格符号。 You should remove them: 您应该删除它们:

<img id="loader-img" class="centered" src="al.gif">

This works for me: 这对我有用:

CSS: CSS:

#logo{
  max-width: 100%;
  height: auto;
  width: auto;

HTML: HTML:

<div>
   <img id="logo" src= {{image.get('logo').url()}}>
</div>

As requested, this is my solution: 根据要求,这是我的解决方案:

html HTML

<div>
    <img src='http://placehold.it/100x50' />
</div>

css: CSS:

div {
    display: flex;
    background: pink;
    height: 200px;
}

img {
    margin:auto;
}

jsfiddle.net/lharby/48o5zd7g (thanks to Zakhar Day). jsfiddle.net/lharby/48o5zd7g (感谢Zakhar Day)。

There are 3 answers in this thread that all work. 这个线程有3个答案,所有的答案都起作用。

There could be something else in your css which is overriding the css rules you are trying to apply? 您的CSS中可能还有其他内容要覆盖您尝试应用的CSS规则? I would strip out the .centered class properties and start with something really simple. 我将删除.centered类属性,并从一些非常简单的内容开始。

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

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