Equivalent to <center> tag in CSS? [duplicate]

人走茶凉 提交于 2019-12-01 03:12:34
Mike Crawford

text-align should work for other kinds of elements. Does this work?

.center { margin: auto; text-align: center; }

Edit Three Years Later :-D

"margin: auto;" also makes the top and bottom margins "auto". That might not be what you want. Alternatively you could have something like:

.center { margin-left: auto;
          margin-right: auto;
          margin-top: 3px;
          margin-bottom: 3px;
          text-align: center; }

This particular example will center the text horizontally while hardwiring the upper and lower margins to 3 pixels.

One can also say something like "margin: 3px auto 3px auto;" but I prefer spelling out the directions explicitly, as I can never quite remember what the order of the parameters are, if I put them all on the one "margin:" setting.

There is a difference

    <center>
      <span> works </span>
    </center>

    <span style="margin: auto; text-align: center;"> And this is broken</span>

    <div style="margin: auto; text-align: center;"> but this works works</div>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!