Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Issues with centering a html button horizontally?

    Hello I'm having issues with my website my normal html buttons <button> hello </button> are not positioned horizontally in the center of the webpage there just vertically placed in the center even tho there is nothing that says vertical or horizontal in the CSS or HTML file ??? is there any way to make html buttons be in the center and be horizontally placed ?

    Code:
    button {
    	   display: block; 
    	   margin: 0 auto;  
    	   background-color:darkgrey;
    	   position:relative;   
    }

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    Answered hundreds of times. Use search near top of page and enter "horizontal centering" and see what you get.

  • #3
    Registered User
    Join Date
    Jun 2011
    Posts
    1,063
    Thanks
    12
    Thanked 241 Times in 240 Posts
    Warm welcome to the forums theblizzfreak,

    This question is literally answered everywhere.

    With margin: 0 auto, the browser calculates an equal margin on both sides of the button with the equation:
    (parent width - button width) / 2 = margin on one sides.

    Code:
    <!-- Parent (browser knows this width) -->
    <div>
        <!-- Button -->
        <button></button>
    </div>
    The browser cannot find the auto margin without the width of the button.

    Code:
    button {
    	   display: block;
               width: #px; /* use the width of your button */
    	   margin: 0 auto;  
    	   background-color:darkgrey;
    	   position:relative;   
    }
    Last edited by Sammy12; 09-27-2012 at 05:54 PM.


  •  

    Tags for this Thread

    Posting Permissions

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