The Tokenizer
The Tokenizer

Reputation: 1564

Can't center in html/css

This code should work (in my opinion) but, I ca't figure out why it won't. Thanks in advance

<body>
<div id="wrap">
<div style="margin:auto;"><img src="logo.png" alt="logo"/></div>
</div>
</body>
</html>

EDIT: I'm trying to center it. It stays in the upper left hand corner. Adding a height and width of auto to both divs also did not work.

Upvotes: 0

Views: 643

Answers (5)

Rajiv Pingale
Rajiv Pingale

Reputation: 1015

you need to check 2 things before you set your division's to center.

  1. Doctype : check whether it is not in quirks mode.

  2. Width of inner div must me less that width of parent div.

do check these things you will get your answer...All the best.

Upvotes: 0

john
john

Reputation: 1354

I imagine that the wrap has a set width, the div holding the image will need a set width as well.

Fiddle so you can see that it does work. (a bit overkill for this question, I know :) )

Upvotes: 0

Scott
Scott

Reputation: 21882

The inner div must have a set width for the margin auto to work.

Upvotes: 1

Virendra
Virendra

Reputation: 2553

If you want to horizontally center the logo then you have to also specify the width of the containing div. Here is a sample code.

<html>
<body>
<div id="wrap">
<div style="width:100px; margin:0 auto;"><img src="logo.png" alt="logo"/></div>
</div>
</body>
</html?

Upvotes: 2

Paul
Paul

Reputation: 36319

Is the width of the inner div less than the width of the #wrap div?

Upvotes: 0

Related Questions