Maison  >  Questions et réponses  >  le corps du texte

img如何居中?

在div中写入 text-align:center 里面的img可以居中

<div style="width:600px;heigth:800px;border:2px solid red;text-align:center">
<img src="timg.jpeg" style="width:200px;height:200px;">
</div>

我想在img中设置属性,这样设置不行

<div style="width:600px;heigth:800px;border:2px solid red">
<img src="timg.jpeg" style="width:200px;height:200px;display:inline-block;text-align:center">
</div>

请问,有无办法在img中设置属性,不在div中设置text-align:center  ,使得这个img居中?

测试过了,不可以使用;margin:0 auto

<div style="width:600px;heigth:800px;border:2px solid red">
<img src="https://s25.postimg.org/8nw251b8f/timg.jpg" style="width:200px;height:200px;display:inline-block;margin:0 auto">
</div>
小白小白2788 Il y a quelques jours4163

répondre à tous(6)je répondrai

  • 枫铖

    枫铖2017-11-30 16:52:38

    Enveloppez un div et centrez la marge du div :0 auto ;

    répondre
    0
  • phpcn_u37517

    phpcn_u375172017-11-22 18:01:10

    Centrez-le

    répondre
    0
  • 数据分析师

    数据分析师2017-10-01 00:35:53

    Comment centrer img ? - Questions et réponses sur le site Web PHP chinois - Comment centrer img ? - Questions et réponses sur le site Web PHP chinois

    Jetez un œil autour de vous et apprenez.

    répondre
    0
  • 阿神

    阿神2017-03-01 09:50:16

     text-align:center 这个属性作用的对象的是父层下面的子元素的居中对齐,
    如果你用text-align:center 让img直接居中的话,因为没有参考的对象,他无法使用这个属性来决定怎么居中,位置该怎么放
    你可以使用margin 来实现居中 

    répondre
    1
  • 怪我咯

    怪我咯2017-03-01 09:50:13

     img中控制样式的话, 就用position。

    <div style="width:600px;heigth:800px;border:2px solid red;">
    <img src="timg.jpeg" style="width:200px;height:200px;position:relative;left:50%;margin-left:-100px;">
    </div>
    就是将图片left设置为50%。这个时候,图片左侧是在中间的。然后设置margin-left:-100px; 这样,就把图片向左移动了50%的自身宽。这个时候就左右居中了。 


    répondre
    0
  • 迷茫

    迷茫2017-03-01 09:48:11

    img的样式应该为width:200px;height:200px;display:block;margin:0 auto;
    img是个特殊的内联元素。可以设置长宽,跟button类似。margin:0 auto居中只能于作用块级元素。故img设置了display:block;margin:0 auto即可居中

    répondre
    1
  • Annulerrépondre