How can I center something if I don't know ahead of time what the width is?

后端 未结 8 1895
生来不讨喜
生来不讨喜 2021-01-06 08:02

I am trying to center a paragraph tag with some text in it within a div, but I can\'t seem to center it using margin: 0 auto without having to specify a fixed width for the

8条回答
  •  温柔的废话
    2021-01-06 08:35

    if the div has set width all you need is

    .myDiv { text-align:center; }

    Also listen to garry's comment. under no circumstances use inline css.

    Also another dirty fix for this in case you have other stuff in the div to centre:

    you can always:

    $('.youParagraph or .otherContent').wrap('');

    Obviously do not practice this if you work within a large team and separation of concerns is an issue.

    I hope this helped.

提交回复
热议问题