4

I have an inline image in a paragraph. I want to float that image in the middle of the paragraph. I tried to center it with...

<center>

...but I saw it was depreciated in HTML5.
I tried to use float it with...

<img style="float: middle;">

...but it turns out that that doesn't even exist.

Is there a short way to do this, preferably inside the img tag, like with the style attribute?
Here is my HTML so far...

<p>
    <img id="resize" width="70%" src="../logo.png" alt="Alt" style="CENTER THIS IMAGE... SOMEHOW...">
</p>
  • Orion closed this question but didn't bother to link to the original answer. So, here it is: https://stackoverflow.com/questions/28336888/html-img-align-middle-not-working – Gabriel Sep 15 '17 at 10:24

4 Answers4

18

You can do it inline using style="display:block; margin-left: auto; margin-right: auto;"

or add it to a css class

    .myimage {
        display:block;
        margin-left: auto;
        margin-right: auto;
    }

Updated Based on CBore comment

rlcrews
  • 3,482
  • 19
  • 66
  • 116
1

You could just add text-align: center; to the <p> tag

Like <p style="text-align:center">

Josh M
  • 982
  • 1
  • 7
  • 19
0

If you know the size you want it to be you can put a margin on left and right to equal 100. for instance

 <!DOCTYPE>
<html>
<head>
</head>
<style>
#resize{
margin-left:15%; 
margin-right:15%;
}
</style>
<body>

<div>
<p>
    <img id="resize" width="70%" src="http://codingkids.businesscatalyst.com/logo.png" alt="Alt" >
</p>
</div>
</body>
</html>
Devin Prejean
  • 626
  • 6
  • 12
  • Cleaner method, and the method to use if you are not statically setting a width is to do like CBroe said and put display:block in CSS for the img. I guess I'm just to used to using bootstrap for all my centering needs – Devin Prejean Jan 20 '16 at 19:18
0

Well, it's inside a 100% width paragraph. I'm not sure what framework you're working in, but, can you just add the css property (text-align:center;) to that paragraph tag?