简体   繁体   中英

why my image not in center of div

Why my image not in the center(Horizontally, Vertically) of div.

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

and my css is:

.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:

在此处输入图片说明

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

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

CSS

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

img {
  margin: auto;
}

And also, your id attribute contains space symbols. You should remove them:

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

This works for me:

CSS:

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

HTML:

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

As requested, this is my solution:

html

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

css:

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

img {
    margin:auto;
}

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

There are 3 answers in this thread that all work.

There could be something else in your css which is overriding the css rules you are trying to apply? I would strip out the .centered class properties and start with something really simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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