www.webdeveloper.com
Results 1 to 9 of 9

Thread: div not centered

  1. #1
    Join Date
    Apr 2012
    Posts
    110

    div not centered

    I am trying to center a div. Here is my code for it:
    Code:
    #rect {
        border: 2px solid #a1a1a1;
        padding: 10px; 
        background: #dddddd;
        border-radius: 1px;
    	font-size:24px;
    	display:inline-block;
    	margin-right:auto;
    	margin-left:auto;
    }
    and
    Code:
    <div id="rect" style="text-align:center; margin-right:auto; margin-left:auto;"><? echo $code; ?></div>
    Any ideas on why it won't center, and how to fix this?

  2. #2
    Join Date
    Aug 2006
    Posts
    1,909
    You need to give that div a width, something less than 100%, to see any effect from auto margins.

  3. #3
    Join Date
    Jul 2014
    Location
    Dubai, United Arab Emirates
    Posts
    15
    Simple give width in css for example width:900px; so that margin auto can work there.

  4. #4
    Join Date
    Apr 2012
    Posts
    110
    I tried adding a width, but it seems to have no effect.

  5. #5
    Join Date
    Mar 2012
    Posts
    1,559
    What width did you set? How wide is the div's parent? You are not providing enough information for us to debug it.

  6. #6
    Join Date
    Apr 2012
    Posts
    110
    I have tried both a percentage and px. (50% and 200px).

  7. #7
    Join Date
    Aug 2006
    Posts
    1,909
    Get rid of the display:inline-block; css, that's forcing it to the left.

  8. #8
    Join Date
    May 2014
    Posts
    936
    yup, auto-margins do jack on inline-block elements.

    Though you've got a perfectly good id, why the inlined STYLE attribute?
    Java is to JavaScript as Ham is to Hamburger.

  9. #9
    Join Date
    Apr 2012
    Posts
    110
    Quote Originally Posted by tracknut View Post
    Get rid of the display:inline-block; css, that's forcing it to the left.
    Thanks- this worked!

    I used this because I had an issue, and it fixed it, but I guess I don't need it anymore.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles