tags:

views:

49

answers:

4

I'm trying to center an image in php. I'm currently using this line of code

echo '<img src="newimage.jpg" width="110" height="120" class="centre">';

However, this seems to have no effect. I've also tried using something like this,

img.center {

 display: block;

 margin-left: auto;

 margin-right: auto;

}
<img src="newimage.jpg" alt="Suni" class="center" /> 

but this merely gives me a syntax error, how do I go about fixing this? Thanks

A: 

This looks like a css question

Try margin:0 auto in your css

Rigobert Song
You also need `display: block` for auto margins to work.
nikc
+1 @Rigobert Song , that's exactly what i was going to write.
Webbisshh
He already has that in his CSS.
DisgruntledGoat
He updated the post goat
Rigobert Song
A: 
echo '<div class="center"><img src="newimage.jpg" width="110" height="120"></div>';

and use

div.center {

instead of

img.center {

Your php syntax is correct, is the syntax error coming from php?

Ross
I tried that, its still gave me the same error,it said:Parse error: syntax error, unexpected '{'
zorgo
it is coming from the CSS line of code
zorgo
that is a php error, not css. maybe you need to echo the css, echo "div.center { ... }" or make sure the css is not in the PHP.
Ross
A: 

You have a PHP error in your code, unrelated to HTML/CSS.

Are you sure you're putting all the pieces of code in the correct places? i.e. CSS should go in an external stylesheet or between <style type="text/css"> and </style> in your HTML.

It's generally better to put chunks of HTML code outside of the PHP sections too. Something like this:

<?php
//some php code
?>

<style type="text/css">
img.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
}
</style>

<img src="newimage.jpg" alt="Suni" class="center" />

<?php //continue your PHP code here...
DisgruntledGoat
A: 

This is a working example of a centred image. See what you think :-)

<html>
<head>
<title>Test</title>

<style type="text/css">
.myImage
{
    margin: auto;
    display: block; 
}
</style>

</head>
<body>
    <img src="http://sstatic.net/so/img/logo.png" alt="My Image" class="myImage"/>
</body>
</html>
Jamie Chapman