Align Center Issue in CSS

Hi,

This is my code, Please check and revert to me.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div style="width:500px;border:1px solid red">
	<div style="margin:0 auto;text-align:center;border:1px solid green;width:auto">
	    <div style="width:100px;float:left;background:yellow;border:1px solid blue;">First Div</div>
	    <div style="width:100px;float:left;background:yellow;border:1px solid blue;">Second Div</div>
        <div style="clear:both"></div>
     </div>
     <div style="clear:both"></div>
</div>
</body>
</html>


I need center of the two div(defined float div) in the outside container. But i dont want to define the width in the outside container. I need flexible width depend on the inside container divs.

But i dont want to define the width in the outside container. I need flexible width depend on the inside container divs.
Hi,
Just so you will know, that margin:auto and width:auto; on your inner wrapping div does not do any good unless a width is defined. You might as well remove both of them if you can’t set a width.

Change your floats to inline-block if you want to center the two 100px divs. When you set this code up properly in a css file you will need to give IE6/7 an inline-block trigger by resetting the two innermost divs to display:inline; in another ruleset.

You can find an example of that IE6/7 reset in the source code of this example.

You will also need to give “haslayout” to IE6/7 on your div>div.

No need for the clearing divs now. :wink:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<div style="width:500px;border:1px solid red">
    <div style="text-align:center;border:1px solid green;">
        <div style="width:100px;display:inline-block;background:yellow;border:1px solid blue;">First Div</div>
        <div style="width:100px;display:inline-block;background:yellow;border:1px solid blue;">Second Div</div>
    </div>
</div>
</body>
</html>

Hi Rayzur,

display:inline-block attributes is not working in Firefox 2.0.2. I have given the sample mockup in this thread. Also i have given my code is below, Please check and tell me your idea. In the middle of the button is flexible width. I have crop two images for left side curve and right side curve because of thats button is flexible.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style>
.hbutton{margin:0 10px 0 0;float:left;padding-left:5px;height:20px;background:url(left_btn.gif) no-repeat top left;}
.hbutton a{line-height:18px;border-top:1px solid #D8DFEA;border-bottom:1px solid #D8DFEA;background:#F2F4F7;float:left;}
.hbutton h2{margin:0;padding:0;font-size:11px;height:20px;padding-right:5px;float:left;background:#F2F4F7 url(right_btn.gif) no-repeat top right;} 
.clear{clear:both}
</style>
</head>
<body>
<div style="width:500px;height:100px;border:1px solid red;background:grey">
    <div style="margin-top:20px;height:30px;border:1px solid green;background:yellow">
        <div class="hbutlft">
            <div class="hbutton"><h2><a href="#">Button 1</a></h2></div>
            <div class="hbutton"><h2><a href="#">Button 2</a></h2></div>
        </div>
     <div class="clear"></div>
     </div>
    <div class="clear"></div>
</div>
</body>
</html>

Am waiting for your favorable reply.

FF2 would need display:-moz-inline-box;

If you want to center widthless blocks then you only have one of two choices. The method that I have already linked to with inline-block or Paul’s centered widthless float method.

http://www.pmob.co.uk/pob/centred-float.htm

I’m not sure why you are not doing this with a ul and list items as it appears to be a navbar you are building. I’ll go along with the code your using since the basic set up is similar anyways.

It will take a while for the images to be approved but I think this code should work for you in FF2.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Centered widthless blocks</title>
<style type="text/css">
#wrap{
width:500px;
height:100px;
border:1px solid red;
background:[COLOR=Blue]#808080;[/COLOR]
}
[B]#inner[/B]{
height:30px;
margin-top:20px;
border:1px solid green;
background:yellow;
[B]text-align:center;[/B] [COLOR=Blue]/*center the inline-blocks*/[/COLOR]
}
.hbutlft{
/*=== ????? ====*/
}
[B].hbutton[/B]{
[COLOR=Red]/*float:left;*/[/COLOR]
[COLOR=Blue]display: -moz-inline-box;/* for FF2 (inline-box must be used for shrink wrapping)*/
display:inline-block;/* for modern browsers */[/COLOR]
height:20px;
margin:0;
padding-left:5px;
background:lime url(left_btn.gif) no-repeat top left;
}
[COLOR=Blue]* html .hbutton {display:inline; margin:0 2px;}/* inline-block trigger for IE6 */
*+html .hbutton {display:inline; margin:0 2px;}/* inline-block trigger for IE7 */[/COLOR]

[B].hbutton h2[/B]{
[COLOR=Blue]float:left;
height:20px;[/COLOR]
margin:0;
padding:0 5px 0 0;
background:red url(right_btn.gif) no-repeat top right;
}
[B].hbutton a[/B]{
[COLOR=Blue]float:left;[/COLOR]
[COLOR=Blue]height:18px;[/COLOR]
line-height:18px;
font-size:11px;
[COLOR=Blue]border-top:1px[/COLOR] solid #D8DFEA;
[COLOR=Blue]border-bottom:1px [/COLOR]solid #D8DFEA;
background:#F2F4F7;
}
</style>

</head>
<body>
<div id="wrap">
    <div id="inner">
        <div class="hbutlft">
            <div class="hbutton"><h2><a href="#">Button 1</a></h2></div>
            <div class="hbutton"><h2><a href="#">Button 2</a></h2></div>
        </div>
     </div>
</div>
</body>
</html>

Just to complete Rayzurs’ example: display: -moz-inline-box; needs to come before display:inline-block; otherwise modern browsers will not ignore it, so don’t alter the sequence :smiley:

Edited :slight_smile:

Lol, there’re other modern browsers then FF3 i forgot :smiley:

No Luki, FF3 is a modern browser, but only FF3+ needs display:-moz-inline-box before display:inline-block because FF3 will read the latter display:inline-block (which it should use) but other browsers won’t even read -moz-inline-box because it has the -moz extention :).

I know buddy, just wanted to be funny lol

Off Topic:

Oh ok lol I love sarcasm on the internet. It can be picked up so easily :wink:

Hi Rayzur,

Thanks for your help. :slight_smile:

Regards,
Dinesh