0

This is my simple code:

<h1 style="text-align:center;">Title TEST</h1>
<img class="center" src="MyImage.jpg">
<footer>
  <p style="text-align:center;">Footer TEST</p>
</footer>

Text is centered correctly, the image not.. Why? I can't understand

j08691
  • 204,283
  • 31
  • 260
  • 272

1 Answers1

1

In your current attempt, class="center" would have no meaning unless you have a class called .center defined in your CSS. It doesn't look like you have a stylesheet, though.

Here are two ways to center an image horizontally:

  1. Use text-align: center on a parent element:

<div style="text-align: center;">
  <h1>Title TEST</h1>
  <img src="https://via.placeholder.com/350x150" />
  <footer>
    <p>Footer TEST</p>
  </footer>
</div>
  1. Center the image element itself by making it block-level and giving it a margin-left and margin-right of 0:

<h1 style="text-align: center;">Title TEST</h1>
<img style="display: block; margin: 0 auto;" src="https://via.placeholder.com/350x150" />
<footer>
  <p style="text-align: center;">Footer TEST</p>
</footer>
Jon Uleis
  • 17,693
  • 2
  • 33
  • 42
  • @user3518663 Glad it helped, and welcome to Stack Overflow. Please feel free to click the checkmark icon next to the most helpful answer to close the question. – Jon Uleis Sep 13 '18 at 17:06