2

I'm trying to align my img in center, but not happens with this code:

<div id="foto" style="position:relative; left:50%;">
    <img src="Imgs/pa1.PNG" width="160" height="240" alt="foto1" />
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272

6 Answers6

3

Use text-align:

<div id="foto" style="text-align: center;">
   <img src="Imgs/pa1.PNG" width="160" height="240" alt="foto1">
</div>
Simon Staton
  • 4,345
  • 4
  • 27
  • 49
2

Dont use inline style. See this demo If you give outer div text-align:center; then all your text in your div will align in center. So its not a good practice.

<div id="foto">
    <img src="http://placehold.it/250x250" width="160" height="240" alt="foto1">
</div>

CSS

#foto {
    width: 100%;
}
#foto img {
    display: block;
    margin: 0 auto;
}
Vikas Ghodke
  • 6,602
  • 5
  • 27
  • 38
1

Try below css.

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

or

#foto {
  text-align: center;
}
Praveen
  • 55,303
  • 33
  • 133
  • 164
0

CSS

#foto
{
    margin-left:auto;
    margin-right:auto;
    width:1000px;//give width accordingly 

}

HTML

<div id="foto" >
   <img src="Imgs/pa1.PNG" width="160" height="240" alt="foto1">
</div>
Sasidharan
  • 3,676
  • 3
  • 19
  • 37
0

With an image I feel you can simply apply the css rule "text-align: center;"

But if thats not working for you could try taking out the position rules and giving the "foto" div a width and an auto margin like so:

style="width:960px; margin: 0 auto;"

I am also not sure if you are aware of style sheets but seperating your CSS from your HTML is also good practice.

0

Make CSS as

div {
  text-align: center;
}

Here is JSBin

Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57