Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Center a container 1

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
This one seems simple, but it's leaving me scratching my head, and google is only turning up some convoluted examples.

Basically I have a div
Code:
#login_box
{
	background-color: #778899;
	width: 600px;
	height: 250px;
	border: 2px outset;
	padding: 10px;
	text-align: center;
	font-size: large;
}

And I want this centered on the page... I can't figure out what to put in the css to do this though.

If I just wrap the div in center HTML tags it does what I want, but I'd rather just do it in the CSS.

Am I just missing something obvious?
 
Code:
margin:0 auto;

And for IE, put
Code:
text-align:center;

in the surrounding block level element.
(you'll need to reset the text align to left within your box then)

Foamcow Heavy Industries - Web design and ranting
Target Marketing Communications - Advertising, Direct Marketing and Public Relations
I wonder what possesses people to make those animated gifs. Do you just get up in the morning and think, "You know what web design r
 
There is no surrounding block level element... it's the only div on the page.... but the first margin part alone seems to work in firefox & ie.

Huh, I'm giving CSS a -2 points for lack of intuitivness on such a simple idea.

Thanks.
 
There is no surrounding block level element

Yes there is. The body is a block level element. Your div is within that.

I don't think it's unintuitive to set margin:auto.

Code:
margin:0 auto;

What that says is set the top and bottom margin to 0 and set the left/right margins to be equal, occupying the available area on each side of the element.

Foamcow Heavy Industries - Web design and ranting
Target Marketing Communications - Advertising, Direct Marketing and Public Relations
I wonder what possesses people to make those animated gifs. Do you just get up in the morning and think, "You know what web design r
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top