search

Home  >  Q&A  >  body text

前端 - 如何让img在div居中,img比div还长

如果下面代码

<p style="height: 100px; overflow: scroll" >
    <img src="img" style="height: 200px; />
</p>

请问如何让img总是居中,尤其是img的height比p 的height还大,image还是居中。
因为p的overflow是scroll的所以图片的上部和下部看不见,但是 p里面显示的还是图片的中部。请从css角度思考。

谢谢

阿神阿神2825 days ago1127

reply all(5)I'll reply

  • 黄舟

    黄舟2017-04-17 11:07:24

    Your code always looks awkward. Take a closer look at the colon ":" in the first line. Is it a Chinese colon? There is also a quotation mark missing in the style of the second line? That's why the third line is highlighted. It looks like there are problems...

    Show picture:

    <p style="height: 100px; overflow: hidden;  width:100px" >
        <img src="img" style="height: 200px; width:200px; margin-top: -100px; margin-left: -100px;" />
    </p>
    

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 11:07:24

    If img is longer than p
    1. Set img width as percentage
    2. Set text-align of p: center

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 11:07:24

    If you have problems with centering, you can try flex box It’s so simple.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 11:07:24

    Negative comments mixed styles are written in the tags 嘤嘤嘤

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 11:07:24

    Hahahaha your answer is so funny!

    reply
    0
  • Cancelreply