Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-27-2012, 12:23 PM   PM User | #1
theblizzfreak
New to the CF scene

 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
theblizzfreak is an unknown quantity at this point
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;   
}
theblizzfreak is offline   Reply With Quote
Old 09-27-2012, 04:01 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
Answered hundreds of times. Use search near top of page and enter "horizontal centering" and see what you get.
sunfighter is offline   Reply With Quote
Old 09-27-2012, 05:46 PM   PM User | #3
Sammy12
Registered User

 
Join Date: Jun 2011
Posts: 1,063
Thanks: 12
Thanked 241 Times in 240 Posts
Sammy12 is on a distinguished road
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..
Sammy12 is offline   Reply With Quote
Reply

Bookmarks

Tags
button, css, horizontal, html, vertical

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:01 AM.


Advertisement
Log in to turn off these ads.