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

≯℡__Kan透↙ 提交于 2019-12-19 03:40:33

问题


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 paragraph. I don't want to specify a fixed width, because I will have dynamic text coming into the paragraph tag and it will always be a different width based on how much text it is.

Does anyone know how I can center the paragraph tag within the div without having to specify a fixed width for the paragraph or without using tables?


回答1:


Here's how to do it using a style sheet.

Style sheet:

div.center-content
{
    text-align: center;
}

div.center-content p
{
    text-align: left;
    margin-left: auto;
    margin-right: auto;
}

HTML:

<div class="center-content">
    <p>Lorem ipsum dolor sit amet</p>
</div>



回答2:


Found this: Centering Block-level Content With Unknown Width.




回答3:


Besides "text-align" property

for centering elements inside block elements like div

use css like


 margin:auto

something like what is posted below

When vertically-centering, its better to use Tables (this in most cases is the only the cross-browser compatible solution )

you can use

 "text-align:center"  

 "vertical-align:middle" 



回答4:


I think the best method is to contain the element in a block level element and do:

.innerElement {margin:0 auto}

Given they are both block level elements that don't have the float parameter, it should work great!




回答5:


here a nice workaround for centering a div with no width.

is tableless and is working in any browser!




回答6:


Try this using inline CSS:

<p style="text-align: center;">Lorem ipsum dolor sit amet</p>

Or using just HTML

<p align="center">Lorem ipsum dolor sit amet</p>



回答7:


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.




回答8:


Eh, auto margins need set width since by default block-level element, such as

would expand onto whole available width.

If you're not supporting IE < 8 you could just set { display: table; margin: 0 auto; }

Otherwise, if your element is surrounded by block-level elements, you could do p { display: inline-block; } p { display: inline; } html > /**/ body p { display: inline-block; } (last two rules are for IE and resetting IE fix for sane browsers) after that, apply { text-align: center; } on the container.

As someone mentioned already, see http://haslayout.net/css-tuts/Horizontal-Centering for more info.

Cheers! Zoffix Znet



来源:https://stackoverflow.com/questions/963823/how-can-i-center-something-if-i-dont-know-ahead-of-time-what-the-width-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!