Jump to content

margin:auto, why does it work only sometimes?


Green_Tea

Recommended Posts

Hi,

 

I realized that margin:auto only works sometimes for me in centering an item. Other times, it does nothing. Margin:auto especially doesn't work for me a lot when I try to center texts in <p> </p>. But in those times that it doesn't work, I can still use margin-left, margin-top, etc. 

 

Are there certain cases where margin:auto doesn't work? Or, are there only specific cases where it works? 

 

Best,

GreenTea

Link to comment
Share on other sites

Also margin: 0 auto is for centering a block-level element, such as a <div> or a <p>. If you want to center text (or an inline element) within it's block, then you use text-align: center; to accomplish that.

 

Eg:

<p style="border: 1px solid black; width: 50%; margin: 0px auto;">This is left-aligned text inside a centered half-width paragraph.</p>

<p style="border: 1px solid black; text-align: center;">This is center-aligned text inside a full-width paragraph.</p>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.